Reputation: 93
class Tests():
def genes():
t=int(input())
for i in range(t):
num = int(input())
numbers = list(map(int,input().strip().split()))
return num, numbers
def solve(num, numbers):
while num>0:
profit = 0
while numbers:
pos = numbers.index(max(numbers))
for i in range(pos):
profit += (max(numbers)-numbers[i])
numbers = numbers[pos+1::]
num -= 1
return profit
if __name__ == "__main__":
for num, numbers in genes():
print('#{0} {1}'.format(i+1, solve(num, numbers)))
I don't know why "for num, numbers in genes():" causes an error and how to fix this
Upvotes: 0
Views: 374
Reputation: 155438
genes()
is returning the first tuple
of num, numbers
and nothing else. I suspect you intended it to generate all pairings, so you could actually iterate over multiple results, unpacking each as you go to num
and numbers
in the caller. To do so, change return
to yield
to make it a generator function:
def genes():
t=int(input())
for i in range(t):
num = int(input())
numbers = list(map(int,input().strip().split()))
yield num, numbers # Only change
The problem was caused because you effectively said:
for num, numbers in (genes_num, genes_numbers):
causing it to try to unpack the single num
from genes
into num
and numbers
.
You may want to check your solve
function as well; the while num > 0:
line will only ever be evaluated once, because you unconditionally return
at the bottom of said loop. It's unclear why this should be a while
loop at all, given numbers
will be exhausted in the inner loop, and without it, any subsequent runs of the outer loop will unconditionally set profit
to 0
.
I'll also note that defining methods on a class that aren't @staticmethod
s and don't take self
is an issue; you're getting away with it because you run your "main" code in the scope of the class definition (so the class isn't actually involved), but it's distinctly odd. You probably don't need a class at all.
Upvotes: 2