1

我正在学习 python 并且正在做这些练习,特别关于回文产品的这个练习。

我有两个数字,我想减 1,但以交替的方式:

999, 999
998, 999
998, 998
997, 998
997, 997
...

我已经用 4 个 if 语句解决了这个问题,但我认为这有点草率。有没有更好的办法?下面是代码:

palN1 = 999
palN2 = 999


def palin(palN1, palN2):
pal = palN1 * palN2
    while str(pal)[::-1] != str(pal):
        if palN1%2 == 0 and palN2%2 == 0:
            palN1 += -1
            pal = palN1 * palN2
            elif palN1%2 != 0 and palN2%2 == 0:
            palN2 += -1
            pal = palN1 * palN2        
        elif palN1%2 != 0 and palN2%2 != 0:
            palN1 += -1
            pal = palN1 * palN2
        elif palN1%2 == 0 and palN2%2 != 0:
            palN2 += -1
            pal = palN1 * palN2  
    print [palN1, palN2]
    print str(pal)

if __name__=="__main__":
    palin(palN1, palN2)

顺便说一句,我得到的练习结果是 [836,836],它是产品 698896。这是我的第一篇文章,如果我做错了什么,请见谅。

4

4 回答 4

4

您可以使用itertools.cycle

def solve(*args):
    args = list(args)
    le = len(args)
    c = cycle(range(le))
    print ("{} "*le).format(*args)
    for _ in xrange( le*args[0] ):
        i = next(c)
        args[i] -= 1;
        print ("{} "*le).format(*args)
...         
>>> solve(999,999)
999 999 
998 999 
998 998 
997 998 
997 997 
996 997 
...
...
>>> solve(999,999,999)
999 999 999 
998 999 999 
998 998 999 
998 998 998 
997 998 998 
997 997 998 
...
...
于 2013-06-12T16:50:57.280 回答
2

从两个数字中减去当前步数(整数除以 2),如果步数为奇数,则从第一个数字中额外减去 1:

>>> p = [999,999]
>>> for i in range(9):
...     print( [p[0]-(i//2)-(i&1), p[1]-(i//2)] )

结果:

[999, 999]
[998, 999]
[998, 998]
[997, 998]
[997, 997]
[996, 997]
[996, 996]
[995, 996]
[995, 995]
于 2013-06-12T17:02:53.133 回答
1

在生成器函数中创建两个计数器itertools.count()并在它们之间交替:

from itertools import count

def count_two_alternate(start):
    count1, count2 = count(start, -1), count(start, -1)
    count_val1, count_val2 = next(count1), next(count2)
    while count_val1 or count_val2:
        yield count_val1, count_val2
        count_val1 = next(count1)
        yield count_val1, count_val2
        count_val2 = next(count2)
    yield count_val1, count_val2

或者,具有可变数量计数器的版本:

from itertools import cycle, counter

def count_alternate(start, counters=2):
    counts = [count(start, -1) for _ in xrange(counters)]
    values = [next(c) for c in counts]
    indices = cycle(range(counters))
    while any(values):
        yield tuple(values)
        index = next(indices)
        values[index] = next(counts[index])
    yield tuple(values)

简短演示:

>>> for c1, c2 in count_two_alternate(5):
...     print c1, c2
... 
5 5
4 5
4 4
3 4
3 3
2 3
2 2
1 2
1 1
0 1
0 0

或对于可变数字版本:

>>> for counts in count_alternate(5, 5):
...     print ' '.join(map(str, counts))
... 
5 5 5 5 5
4 5 5 5 5
4 4 5 5 5
4 4 4 5 5
4 4 4 4 5
4 4 4 4 4
3 4 4 4 4
3 3 4 4 4
3 3 3 4 4
3 3 3 3 4
3 3 3 3 3
2 3 3 3 3
2 2 3 3 3
2 2 2 3 3
2 2 2 2 3
2 2 2 2 2
1 2 2 2 2
1 1 2 2 2
1 1 1 2 2
1 1 1 1 2
1 1 1 1 1
0 1 1 1 1
0 0 1 1 1
0 0 0 1 1
0 0 0 0 1
0 0 0 0 0
于 2013-06-12T16:50:39.103 回答
0

你可以减少最大的项目:

palN1 = 999
palN2 = 999
while palN1 and palN2:
    if palN1 > palN2:
        palN1 -= 1
    else:
        palN2 -= 1
于 2013-06-12T17:23:07.220 回答