I have a code that determines the survivor from a list of names to walk the plank in this scenario, the last person in the list survives the code i have for this is:
names = ["Andrew", "Brenda", "Craig", "Deidre", "Edward", "Felicity", "Greg", "Harriet"]
def survivor(names, step):
Next = step - 1
while len(names) > 1:
names.pop(Next)
Next = Next + step
Next = (Next - 1) % len(names)
print names
return names[0]
this works to return the survivor based on what put in step but i also need to work out the smallest N for step for a person to survive e.g. 3 for greg and 2 for andrew.
the codes i have tried for this are:
assert name in names
for step in survivor(names, step):
if survivor == name:
return step
but it keeps saying local variable step referenced before assignment or global step is not defined.
and
assert name in names
for step in itertools.count(1):
if survivor(names, step) == name:
return step
but this returns
['Brenda', 'Craig', 'Deidre', 'Edward', 'Felicity', 'Greg', 'Harriet']
['Craig', 'Deidre', 'Edward', 'Felicity', 'Greg', 'Harriet']
['Deidre', 'Edward', 'Felicity', 'Greg', 'Harriet']
['Edward', 'Felicity', 'Greg', 'Harriet']
['Felicity', 'Greg', 'Harriet']
['Greg', 'Harriet']
['Harriet']
which is not what i want
Can anyone help me work this out?