I have a homework question where it asks to make an algorithm which would go through every element in the list and see if the number beside it is greater than the current number or not. If it is not greater than the current number, then the counter will go up by 1, if it is greater the counter will stay where it is. I got it to work but it only works if there are more than 3 items in the list.
Here's what I have so far:
def count(items):
if len(items) == 0:
return 0
else:
count = 0
for i in items:
count += 1
items1 = items[i:]
items2 = items[i+1:]
if items1 > items2:
return count - 1
print (count([42, 7, 12, 9, 2, 5])) #returns 4. works but does not work if len < 3
print (count([])) #returns 0. works
print (count(list(range(10**7)))) # supposed to print out 1 since it's only 1 number, but it just gets stuck and doesn't finish the rest of the program
print (count([99])) # prints out none
print (count(list(range(10**7,0,-1))))# supposed to print out 10000000 but prints 1 instead
Any kind of help or suggestions would be greatly appreciated.
Aucun commentaire:
Enregistrer un commentaire