Не читайте ниже, если вы хотите решить это самостоятельно.
def madstar(s): # s is the list
if all(e % 2 for e in s): # all Stars with odd numbers
return (len(s), sum(s)-len(s)) # just one sheep stolen from each Star
for i,e in enumerate(s):
if e % 2 == 0: # even number found
return (i+1, # Stars are numbered from 0, so i==0 -> 1 Star visited etc.
sum(s) - ( # stolen sheep
2*(i+1) # two for every visited Star
- s[:i].count(1) # except visited Stars with initially only 1 sheep
- (1 if e>0 else 2) # and the final one, where it is either 0 or 1, but never 2
))
for test_list in [[1,3,5,7,11,13,17,19],
[1,3,5,7,11,13,16,19],
[1],
[3,0,2],
[0],
[2]]:
print(test_list, '->', madstar(test_list))