|
发表于 2018-5-27 16:53:30
|
显示全部楼层
有点意思,成功是成功了,但是不好
- ## 代码
- import test
- import itertools
- def get_pins(x):
- form = [[0,8],[1,2,4],[2,1,3,5],[3,2,6],[4,1,5,7],[5,2,4,6,8],[6,3,5,9],[7,4,8],[8,5,7,9,0],[9,6,8]]
- n = len(x)
- y = []
- result = []
- for each in x:
- y.append(int(each))
- if n == 1:
- comb = itertools.product(form[y[0]])
- elif n == 2:
- comb = itertools.product(form[y[0]],form[y[1]])
- elif n == 3:
- comb = itertools.product(form[y[0]],form[y[1]],form[y[2]])
- elif n == 4:
- comb = itertools.product(form[y[0]],form[y[1]],form[y[2]],form[y[3]])
- elif n == 5:
- comb = itertools.product(form[y[0]],form[y[1]],form[y[2]],form[y[3]],form[y[4]])
- else:
- return '排列过大'
- for i in comb:
- z = ''
- for j in i:
- z += str(j)
- result.append(z)
- return result
-
- ## 测试
- expectations = [('8', ['5','7','8','9','0']),
- ('11',["11", "22", "44", "12", "21", "14", "41", "24", "42"]),
- ('369', ["339","366","399","658","636","258","268","669","668","266","369","398","256","296","259","368","638","396","238","356","659","639","666","359","336","299","338","696","269","358","656","698","699","298","236","239"])]
- for tup in expectations:
- test.assert_equals(sorted(get_pins(tup[0])), sorted(tup[1]), 'PIN: ' + tup[0])
- ## 结果
- Success!
- Success!
- Success!
复制代码 |
|