I have been given a task to create a code for. The task is as follows:
You are the captain of a sailing vessel and you and your crew have
been captured by pirates. The pirate captain has all of you standing
in a circle on the deck of his ship trying to decide in which order
you should walk the plank. Eventually he decides on the following
method:(a) The pirate captain asks you to pick a number N.
(b) The
first person to walk the plank will be the Nth person (starting from
you).(c) The captain will then continue around the circle forcing
every Nth person to walk the plank.(d) Once there is only one person
left, that person will be given freedom.For example: The crew
consists of: Andrew, Brenda, Craig, Deidre, Edward, Felicity, Greg and
Harriet. Andrew selects N=2. The crew will walk the plank in the order:
Brenda, Deidre, Felicity, Harriet, Craig, Greg, Edward. Andrew will be
given freedom.
The code i have so far is:
def survivor(names, step):
names = ["Andrew", "Brenda", "Craig", "Deidre", "Edward", "Felicity", "Greg", "Harriet"]
Next = step - 1
names.pop(Next)
print names
This will remove the first nth person from the list but I’m not sure how to loop through the list to keep removing the nth person.
I need it so lets assume step = 3, then i need it to remove craig and then count from craig onwards and remove the next 3rd element which is felicity and so on until there is one person left.
How can I do this?
This seems to work:
It prints the names of the pirate’s victims and returns the name of the survivor: