展开全部
草稿版# coding: utf-8
'''
* * *
x * * *
------------
* * * *
* * * *
* * * *
------------
* * * * * *
each start is a number;
sum for each line shall be same
there is no duplicated numbers at each line
'''
import random as RND
def NumberListFactory(width=3):
standard = range(10)
ret = []
for i in xrange(0, width):
if i == 0:
value = standard.pop(RND.randint(1, len(standard)-1))
else:
value = standard.pop(RND.randint(0, len(standard)-1))
ret.append(value)
return ret
def Number2List(num):
ret = []
tmp = 0
for i in xrange(len(str(num))-1, 0, -1):
key = (num-tmp) / (10**i)
ret.append(key)
tmp = tmp+key*(10**i)
ret.append(num-tmp)
return ret
def List2Number(lst):
ret = 0
for i in xrange(0, len(lst)):
ret += lst[i]*(10**(len(lst)-i-1))
return ret
def List2String(lst):
return ' '.join([str(i) for i in lst])
def IsUniq(lst):
for i in lst:
tmp = lst[:]
tmp.remove(i)
if i in tmp:
return False
return True
counter = 0
result = None
while result is None:
counter+=1
p1 = NumberListFactory()
p2 = NumberListFactory()
standard = sum(p1)
if standard !5261= sum(p2):
continue
else:
multiResult = []
for i in xrange(len(p2)-1, -1, -1):
tmp = Number2List(p2[i]*List2Number(p1))
if len(tmp) != 4: # filter calculation when width of it is not 4
continue
else:
if IsUniq(tmp):
multiResult.append(tmp)
# filter result when zero
if len(multiResult) != 3:
continue
# filter result when SUM is not equal to standard
flag = True
for j in multiResult:
if standard != sum(j):
flag = False
break
if flag is False:
continue
# filter result when width of result is not 6
result = Number2List(List2Number(p1)*List2Number(p2))
if len(result) != 6 or standard != sum(result) or IsUniq(result) is False:
result = None
continue
else:
print 'SUM of Line -> {}'.format(standard)
print 'Loop ID: {}\n'.format(counter)
print ' {}'.format(List2String(p1))
print ' x {}'.format(List2String(p2))
print '-'*12
for k in xrange(len(multiResult)-1, -1, -1):
print '{}{}'.format(' '*k*2, List2String(multiResult[len(multiResult)-1-k]))
print '-'*12
print List2String(result)
print '\nOver'
可以的话,做4102一些1653优化。