2013-05-11 42 views
0

我今天的問題是,如果我在歐拉145的正確道路上走下去,如果它是一種有點高效。我的大部分時間都沒有完成,只是我的一個Defs給了我一個int(str(numb)[:i])%2 == 0的麻煩來進行偶校驗。我的代碼在下面。第10行是問題點Python:Project Euler 145

def reversed(reg): # to flip the number around 
    fliped = str(reg)[::-1]; 
    return(int(fliped)); # Return it as a int. 

def allEvenDigits(numb): # This is the issue one 
    hasEvenNumb = False; 
    for i in range(0, len(str(numb))): 
     if int(str(numb)[:i])%2 == 0: # if int of the string numb's char at i is even 
      hasEvenNumb = True; ## return that it is true 
      break; # why go on if we found a even. 
    return(hasEvenNumb); 


for i in range(1, 1000): # its 1000 to save a few minutes 
    revNumb = reversed(i); 
    total = revNumb+i; 
    if(allEvenDigits(total)): 
     print(i, "+" , revNumb, "=",Total); 
+1

PLE ase從不在Python中使用';'s。除此之外,看'str(1234)[:0]'是 – 2013-05-11 03:46:08

+1

另外,'revers'是一個內置函數 - 你不應該覆蓋它。 – Volatility 2013-05-11 03:53:09

回答

1

您可以使用內置函數all()並使用一組來保留已經解決的數字的軌跡;例如,如果你已經解決了36那麼就沒有理由來解決63

seen = set() 

def allEvenDigits(numb): # This is the issue one 
    return all(int(n)%2 == 0 for n in str(numb)) 

for i in range(1, 1000): # its 1000 to save a few minutes 
    revNumb = reversed(i); 
    total = revNumb+i; 

    if i not in seen and revNumb not in seen: 
     if (allEvenDigits(total)): 
      print(i, "+" , revNumb, "=",total); 
      seen.add(i) 
      seen.add(revNumb) 

輸出:

(1, '+', 1, '=', 2) 
(2, '+', 2, '=', 4) 
(3, '+', 3, '=', 6) 
(4, '+', 4, '=', 8) 
(11, '+', 11, '=', 22) 
(13, '+', 31, '=', 44) 
(15, '+', 51, '=', 66) 
(17, '+', 71, '=', 88) 
(22, '+', 22, '=', 44) 
(24, '+', 42, '=', 66) 
(26, '+', 62, '=', 88) 
(33, '+', 33, '=', 66) 
(35, '+', 53, '=', 88) 
(44, '+', 44, '=', 88) 
... 

幫助all

>>> all? 
Type:  builtin_function_or_method 
String Form:<built-in function all> 
Namespace: Python builtin 
Docstring: 
all(iterable) -> bool 

Return True if bool(x) is True for all values x in the iterable. 
If the iterable is empty, return True. 
+0

雖然這確實起作用,但我的一個問題是我還沒有掌握的問題的其他部分,如果反轉的數字以0開頭,我們不想計算它們。 – Scott 2013-05-15 17:49:41

+0

IE:100反轉爲001,不可接受。 360反轉到036,我們不想數這個。我們的問題在這裏:http://projecteuler.net/problem=145 – Scott 2013-05-15 18:07:01

0

當範圍是range(0, len(str(numb)))時,您將從空字符串開始。

def allEvenDigits(numb): # This is the issue one 
    hasEvenNumb = False; 
    for i in range(1, len(str(numb))): 
     if int(str(numb)[:i])%2 == 0: # if int of the string numb's char at i is even 
      hasEvenNumb = True; ## return that it is true 
      break; # why go on if we found a even. 
    return(hasEvenNumb); 

>>> allEvenDigits(52) 
False 

看來,然而,越容易的事是檢查如果每個數字是偶數:

def allEvenDigits(numb): 
    hasEvenNumb = True 
    for char in str(numb): 
     if int(char) % 2 == 0: 
      hasEvenNumb = False 
      break 
    return hasEvenNumb 

allEvenDigits(52) 

使得它多了幾分率性,並檢查您可以用解決問題只有個別數字而不是子字符串。

+0

我很愚蠢的XD。謝謝! – Scott 2013-05-11 03:46:30

0
def sumrevers(x): 
    summation = x + int(str(x)[::-1]) 
    if summation % 2 != 0: return summation 


def checknum(x): 
    if not (str(x)[-1] == "0") or (str(x)[0] == "0"): 
     if type(sumrevers(x)) == int: 
      num = str(sumrevers(x)) 
      checklis = [k for k in str(num)] 
      if all(int(i) % 2 != 0 for i in checklis): return True 


cnt = 0 
for i in xrange(1, 1000000001): 
    if checknum(i): 
     cnt += 1 
print cnt