Skip to content

Commit 74808f5

Browse files
committed
add 算法
1 parent 7848bf5 commit 74808f5

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,38 @@ Sorry, The item not in file dict
5454
如果查找到了返回一个list,list中item类型为tuple, 并且包含了在树中匹配的起,终点位置index
5555
```
5656

57+
### calc24.py 算24游戏小程序
58+
```
59+
游戏规则:给定4个数,可以执行的运算有 + - * / , 求出算的结果是24的算法过程
60+
61+
get help:
62+
➜ Py git:(master) ✗ py calc24.py -h
63+
Usage: usage -n 1,2,3,4
64+
65+
Options:
66+
-h, --help show this help message and exit
67+
-n NUMS specify num list
68+
69+
exp:
70+
➜ Py git:(master) ✗ py calc24.py -n 10,8,9,4
71+
[10, 8, 9, 4]
72+
9 - 10 = -1
73+
4 + -1 = 3
74+
8 * 3 = 24
75+
Success
76+
77+
or random test:
78+
➜ Py git:(master) ✗ py calc24.py
79+
[9, 10, 3, 6]
80+
10 - 9 = 1
81+
3 + 1 = 4
82+
6 * 4 = 24
83+
Success
84+
85+
~~~python轮子很强大~~~
86+
```
87+
88+
5789
### dispatch.py 轮转队列
5890
```
5991
你的手头上会有多个任务,每个任务耗时很长,而你又不想同步处理,而是希望能像多线程一样交替执行。

calc24.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# -*- coding: utf-8 -*-
2+
# @Author: lock
3+
# @Date: 2017-06-09 22:48:14
4+
# @Last Modified by: lock
5+
# @Last Modified time: 2017-06-10 01:48:16
6+
# -*- coding: utf-8 -*-
7+
import optparse
8+
import itertools
9+
import random
10+
11+
12+
# 洗牌
13+
def shuffle(n, m=-1):
14+
if m == -1:
15+
m = n
16+
l = range(n)
17+
for i in range(len(l) - 1):
18+
x = random.randint(i, len(l) - 1)
19+
l[x], l[i] = l[i], l[x]
20+
if i == m - 1:
21+
break
22+
return [l[idx] for idx in range(n) if idx >= 0 and idx < m]
23+
24+
25+
# 生成4张牌
26+
def Get4Card():
27+
card = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10] * 4
28+
cardidxs = shuffle(52, 4)
29+
return [card[idx] for idx in cardidxs]
30+
31+
32+
def GenAllExpr(card_4, ops_iter):
33+
try:
34+
while True:
35+
l = list(ops_iter.next()) + card_4
36+
its = itertools.permutations(l, len(l))
37+
try:
38+
while True:
39+
yield its.next()
40+
except StopIteration:
41+
pass
42+
except StopIteration:
43+
pass
44+
45+
46+
def CalcRes(expr, isprint=False):
47+
opmap = {'+': lambda a, b: a + b, '-': lambda a, b: a - b, '*': lambda a, b: a * b,
48+
'/': lambda a, b: a / (b + 0.0)}
49+
expr_stack = []
50+
while expr:
51+
t = expr.pop(0)
52+
if type(t) == int:
53+
expr_stack.append(t)
54+
else:
55+
if len(expr_stack) < 2:
56+
return False
57+
else:
58+
a = expr_stack.pop()
59+
b = expr_stack.pop()
60+
if isprint:
61+
print a, t, b, '=', opmap[t](a, b)
62+
try:
63+
expr_stack.append(opmap[t](a, b))
64+
except ZeroDivisionError:
65+
return False
66+
return expr_stack[0]
67+
68+
69+
if __name__ == "__main__":
70+
parser = optparse.OptionParser('usage -n 1,2,3,4')
71+
parser.add_option('-n', dest='nums', type='string', help='specify num list')
72+
(options, args) = parser.parse_args()
73+
nums = options.nums
74+
if nums is None:
75+
input_card = Get4Card()
76+
else:
77+
input_card = [int(x) for x in nums.split(',')]
78+
card = input_card
79+
if len(input_card) != 4:
80+
print(parser.usage)
81+
exit(0)
82+
print card
83+
ops = itertools.combinations_with_replacement('+-*/', 3) # 一个24点的计算公式可以表达成3个操作符的形式
84+
allexpr = GenAllExpr(card, ops) # 数和操作符混合,得到所有可能序列
85+
for expr in allexpr:
86+
res = CalcRes(list(expr))
87+
if res and res == 24:
88+
CalcRes(list(expr), True) # 输出计算过程
89+
print "Success"
90+
break

0 commit comments

Comments
 (0)