Reputation: 321
I have written some code but would like to make it more simple.
How can I extract the desired integer from my list without all the extra lines of code which turn it into an integer so I can use it in my if statement?
def nextRound(k,scores):
count = 0
integers = scores[k-1:k]
strings = [str(integer) for integer in integers]
a_string = "".join(strings)
an_integer = int(a_string)
for i in scores:
if i > 0 and i >= an_integer:
count += 1
return count
print(nextRound(2,[1,1,1,1]))
here are the instructions for the question:
“If a contestant earns a score equal to or greater than the k-th place finisher, they will advance to the next round if their own score is greater than 0”. So write a function nextRound(k,scores) which will count how many contestants will progress to the next round.
Upvotes: 0
Views: 80
Reputation: 175
This accomplishes the same thing, I think you may be overthinking it by confusing a list of ints as also having the datatype list for its members:
def nextRound(k,scores):
count = 0
integer = scores[k]
for i in scores:
if i > 0 and i >= integer:
count += 1
return count
print(nextRound(2,[1,1,1,1]))
Running this gives the expected output of 4. This does assume that the scores are given presorted.
Upvotes: 1