JamesHudson81
JamesHudson81

Reputation: 2273

Can't see error in code

Don't understand what is wrong with the variable contador that returns the max value where it is popped at valormax..

def ArrayAdditionI(arr): 
    valormax=arr.pop(arr.index(max(arr)))
    contador=0
    i=0
    arr1=sorted(arr)
    while contador<valormax:
        contador+=arr1[i]
        i+1
        if i>len(arr1):
            break

    if contador==valormax:
        return "true"
    else: 
        return "false"

By the way does anyone now how to apply the debugger in the powershell in order to see how the loops goes and avoid asking these type of questions through here?

Upvotes: 0

Views: 68

Answers (2)

L&#230;rne
L&#230;rne

Reputation: 3142

Well, firstly,

if contador==valormax:
return "true"
else: 
return "false"

should be

if contador==valormax:
    return "true"
else: 
    return "false"

Second, to avoid fiddling with indexes like i (and writing i+1 instead of i+=1), I would replace

i=0
arr1=sorted(arr)
while contador<valormax:
    contador+=arr1[i]
    i+1
    if i>len(arr1):
        break

By

for a in sorted(arr):
    contador+=a
    if contatdor >= valormax:
        break

By the way, what's the point of this function ? Compute whether max(arr) equals the sum of all the other elements of arr ?

Upvotes: 2

Ahmed Ginani
Ahmed Ginani

Reputation: 6650

You need to change the code might be the issue is in your code format.

From:

 if contador==valormax:
 return "true"
 else: 
 return "false"

into:

if contador==valormax:
    return "true"
else: 
    return "false"

Upvotes: 0

Related Questions