2013-02-01 69 views
0
def getPrimeList(check): 
    storedprimes = [] 
    i = 2 
    while i <= check: 
     if isPrime(check): 
      storedprimes = storedprimes + [i] 
     i = i + 1 
    return storedprimes 
def getPrimeFact(check): 
    primelist = getPrimeList(check) 
    prime_fact = [] 
    i = 0 
    while check !=1: 
     if check%primelist[i]==0: 
      prime_fact=prime_fact+[primelist[i]] 
      check = check/primelist[i] 
     i = i + 1 
     if i == len(primelist): 
      i = 0 
    return prime_fact 
def getGCF(checks): 
    a=0 
    listofprimefacts=[] 
    while a<len(checks): 
     listofprimefacts=listofprimefacts+[getPrimeFact(checks[a])] 
     a=a+1 
    b=0 
    storedprimes=[] 
    while b<len(primefactlist): 
     c=0 
     while c<len(listofprimefacts[b]): 
      if listofprimefacts[b][c] not in storedprimes: 
       storedprimes=storedprimes+[listofprimefacts[b][c]] 
      c=c+1 
     b=b+1 
    prime_exp=[] 
    d=0 
    while d<len(storedprimes): 
     prime_exp=prime_exp+[0] 
     d=d+1 

    e=0 
    while e<len(storedprimes): 
     f=0 
     while f<len(listofprimefacts): 
      if f==0: 
       prime_exp[e]=listofprimefacts[f].count(storedprimes[e]) 
      elif prime_exp[e]-(listofprimefacts[f].count(storedprimes[e]))>0: 
       prime_exp[e]=listofprimefacts[f].count(storedprimes[e])     
      f=f+1 
     e=e+1 
    g=0 
    GCF=1 
    while g<len(primelist): 
     GCF=GCF*(storedprime[g]**prime_exp[g]) 
     g=g+1 
    return GCF 

我正在創建一個程序,它將使用這些函數來計算分數;然而,在shell中測試我的GCF函數後,我一直在收到列表索引錯誤。我不知道,錯誤來自於考慮我99%確定我的索引沒有問題,通常我不會在SO中發佈這樣的「可修復」問題,但是這次我不知道問題是什麼,再次感謝。Python列表索引錯誤

哦,繼承人確切的錯誤

File "<pyshell#1>", line 1, in <module> 
    getGCF(checks) 
    File "E:\CompProgramming\MidtermFuncts.py", line 31, in getGCF 
    listofprimefacts=listofprimefacts+[getPrimeFact(checks[a])] 
    File "E:\CompProgramming\MidtermFuncts.py", line 20, in getPrimeFact 
    if check%primelist[i]==0: 
IndexError: list index out of range 
+0

閱讀'for'循環。 – Blender

+0

攪拌你的名字和諷刺但嚴肅的權利風格讓我覺得像以前見過你。你好再次陌生人:D – Alvaro

+0

這是一個學習練習,還是你試圖解決一個問題?我很確定有預先存在的Python解決方案可以解決您的分數需求,但是如果您正在學習,那麼就繼續下去。 – steveha

回答

0

你在你的getPrimeList()功能混合起來icheck;你測試是否check是素數,不是i;這裏是正確的函數:

def getPrimeList(check): 
    storedprimes = [] 
    i = 2 
    while i <= check: 
     if isPrime(i): # *not* `check`! 
      storedprimes = storedprimes + [i] 
     i = i + 1 
    return storedprimes 

primelist將被設置爲空列表(如getPrimeList(check)返回一個空列表),您primelist[i](用於任何i)將失敗,並索引錯誤。

primelist爲空的另一種方式是isPrime()永不返回True;你不會向我們展示該功能來驗證它。

您的下一個錯誤是在getGCF();您首先定義一個listofprimefacts變量(一個列表),但稍後將引用一個不存在的primefactlist變量,從而導致NameError。該函數中的下一個名稱錯誤將進一步變爲primelist

你真的想重讀Python tutorial;你在你的代碼中錯過了很多python成語;特別是關於如何在序列上創建循環(提示:for check in checks:比具有索引變量的while循環更易於使用)以及如何將項追加到列表。

我個人的工具定義的:

from math import sqrt 

def prime_factors(num, start=2): 
    """Return all prime factors (ordered) of num in a list""" 
    candidates = xrange(start, int(sqrt(num)) + 1) 
    factor = next((x for x in candidates if (num % x == 0)), None) 
    return ([factor] + prime_factors(num/factor, factor) if factor else [num]) 

它不需要isPrime()測試。

+0

事情就是使用python作爲學習其他語言的門戶,這就是爲什麼python convetional'for'不在我的任何代碼中,我知道它是什麼,儘管我不使用它,也用於測試目的,我打開shell定義檢查前:檢查= 125,525,325然後我只是打電話給getGCF(檢查) – Alvaro

+2

@Alvaro如果你故意忽略語言功能,我不知道你是什麼應該期待。 'for'習語是通過某種迭代器或者拼寫爲'foreach'或'for each'或某種其他合成結構來加載語言的,不知道你是否正在使用這種方法進行任何好的「學習」: ) –

1

你可能要重新思考如何攻擊這個問題。在目前的形式下,你的代碼真的很難處理。

以下是我會做:

def is_prime(n): 
    for i in range(2, int(n ** 0.5) + 1): 
     if n % i == 0: 
      return False 

    return True 

def prime_factors(number): 
    factors = [] 

    for i in range(2, number/2): 
     if number % i == 0 and is_prime(i): 
      factors.append(i) 

    return factors 

def gcf(numbers): 
    common_factors = prime_factors(numbers[0]) 

    for number in numbers[1:]: 
     new_factors = prime_factors(number) 
     common_factors = [factor for factor in common_factors if factor in new_factors] 

    return max(common_factors) 

這條線就在這裏:

common_factors = [factor for factor in common_factors if factor in new_factors] 

是一個列表理解。你可以把它打開到另一個for循環:

temp = [] 

for factor in common_factors: 
    if factor in new_factors: 
     temp.append(factor) 

common_factors = list(temp) # Pass by value, not by reference