我不明白为什么“palindrom”的价值会发生变化?

时间:2015-06-09 00:44:41

标签: python variables if-statement

我无法理解为什么“palindrom”的值在条件

时会发生变化
if(palindrome(i*j) ): 

替换为

if (palindrome(i*j) and i*j>palindrom)):

在此代码中

 def palindrome(n):
     return str(n)==str(n)[::-1];

 def ndigitPalindrome(n):
     lbound=10**(n-1);
     ubound=10**(n);
     palindrom=0;
     for i in range(lbound,ubound):
              for j in range(lbound,ubound):
                  if(palindrome(i*j) ): 
                      palindrom=i*j;
     return palindrom;

 print "Largest n digit palindrome : ",ndigitPalindrome(3); 

2 个答案:

答案 0 :(得分:2)

if (palindrome(i*j) and i*j>palindrom))只会在新的回文较大时更新palindrom,使得该程序产生最大的回文,在这种情况下为906609;

if(palindrome(i*j) )使得程序通过在有新的回文时更新palindrom来生成最新的回文,并且顺序只是循环的顺序,因此在这种情况下返回{{1} }由580085生成。

答案 1 :(得分:2)

添加打印声明可以帮助您了解正在发生的事情:

def ndigitPalindrome(n):
    lbound=10**(n-1)
    ubound=10**(n)
    palindrom=0
    for i in range(lbound,ubound):
        for j in range(lbound,ubound):
            if palindrome(i*j):
                palindrom=i*j
                print i, j, palindrom
    return palindrom
993 913 906609
995 517 514415
995 583 580085
580085

没有按升序发现回文。检查i*j>palindrom是否阻止您将回文改为较小的值。

基本上你的i*j>palindrom条件与你写的一样:

if is_palindrome(i*j):
    palindrom = max(palindrom, i*j)

关于代码的几点说明:

def n_digit_product_palindrome(n):
    lbound = 10**(n-1)
    ubound = 10**n
    answer = -1
    for i in range(lbound, ubound):
        for j in range(lbound, ubound):
            product = i * j
            if product > answer and is_palindrome(product):
                answer = product
    return answer
​
%timeit n_digit_product_palindrome(3)
print n_digit_product_palindrome(3)

10 loops, best of 3: 138 ms per loop
906609

您可以通过将内部循环的lbound更改为当前值i来减少产品数量:

def n_digit_product_palindrome(n):
    lbound = 10**(n-1)
    ubound = 10**n
    answer = -1
    for i in range(lbound, ubound):
        for j in range(i, ubound):
            product = i * j
            if product > answer and is_palindrome(product):
                answer = product
    return answer

%timeit n_digit_product_palindrome(3)
print n_digit_product_palindrome(3)

10 loops, best of 3: 83.8 ms per loop
906609

i用于内循环的lbound可以防止您必须检查已经遇到的值。随着i递增,在外部循环中,j小于i的所有值都已经过检查。

例如:

lbound = 3
ubound = 6

1st outer loop: i = 3
    inner loop: i * j = 3 * 3, 3 * 4, 3 * 5
2nd outer loop: i = 4
    inner loop: i * j = 4 * 3, 4 * 4, 4 * 5
3rd outer loop: i = 5
    inner loop: i * j = 5 * 3, 5 * 4, 5 * 5

变为:

lbound = 3
ubound = 6

1st outer loop: i = 3
    inner loop: i * j = 3 * 3, 3 * 4, 3 * 5
2nd outer loop: i = 4
    inner loop: i * j = 4 * 4, 4 * 5
3rd outer loop: i = 5
    inner loop: i * j = 5 * 5

通过允许您提前退出来反转搜索可以大大改善运行时间:

def n_digit_product_palindrome(n):
    ubound = 10**n-1
    lbound = 10**(n-1)-1
    answer = -1
    for i in range(ubound, lbound, -1):
        if i * i < answer:
            break
        for j in range(i, lbound, -1):
            product = i * j
            if product < answer:
                break
            if product > answer and is_palindrome(product):
                answer = product
    return answer

%timeit n_digit_product_palindrome(3)
print n_digit_product_palindrome(3)

100 loops, best of 3: 5.47 ms per loop
906609

当您找到i * i < answer时中断外部循环,因为当i的值下降时,如果当前i * i小于您找到的回文的值,则不会ij的后续值会产生更大的值。

对于内部循环,中断逻辑是类似的,因为当i * j小于答案时,j乘以当前i的后续值将产生大于答案的值。< / p>