python竖着输出_关于用python计算竖乘的问题,具体要求和希望输出格式如下

展开全部

草稿版# 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优化。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值