2013-02-01 115 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