Removing every nth item from a list in python 2.7

I was instructed to create a code for. The task is as follows:

You are the captain of a sailboat, and you and your team were 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 go along the board. He finally solves the following method:

(a) The pirate captain asks you to select number N.

(b) the first person to walk the board will be the Nth person (starting with you).

(c) The captain will continue to move around the circle, each N-th person must walk on the board.

(d) When there is only one person to the left, that person will receive freedom.

For example: The crew consists of: Andrew, Brenda, Craig, Deirdre, Edward, Felicity, Greg and Harriet. Andrew chooses N = 2. The crew will walk on the board in the order: Brenda, Deidre, Felicity, Harriet, Craig, Greg, Edward. Andrew will be given freedom.

The code I still have:

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 scroll through the list to remove the nth person.

I need this, so let's assume that step = 3, then I need to remove it from craig, and then count from craig and then remove the next third element, which is bliss and so on, until there is one person left.

How can i do this?

+6
source share
2 answers

It works:

 from collections import deque def survivor(names, step): circle = deque(names) while len(circle) > 1: circle.rotate(1-step) print circle.popleft() return circle[0] 

He prints the names of the victims of the pirates and returns the name of the survivor:

 In [17]: crew = ["Andrew", "Brenda", "Craig", "Deidre", ....: "Edward", "Felicity", "Greg", "Harriet"] In [18]: survivor(crew, 2) Brenda Deidre Felicity Harriet Craig Greg Edward Out[18]: 'Andrew' In [19]: survivor(crew, 3) Craig Felicity Andrew Edward Brenda Harriet Deidre Out[19]: 'Greg' 
+6
source

The following code should do everything you requested, including the implementation of the safeN function:

 import collections import itertools def walk_plank(names, N): "Walk everyone down the plank." circle = collections.deque(names) while circle: circle.rotate(-N) yield circle.pop() def save_last(names, N): "Save the last person from walking the plank." for name in walk_plank(names, N): pass return name def safeN(names, name): "Find the best N to save someone from walking the plank." assert name in names, 'Name must be in names!' for N in itertools.count(1): if save_last(names, N) == name: return N 

Edit: Here is an example of using the code above when working with IDLE on Windows.

 Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> import collections, itertools >>> def walk_plank(names, N): "Walk everyone down the plank." circle = collections.deque(names) while circle: circle.rotate(-N) yield circle.pop() >>> def save_last(names, N): "Save the last person from walking the plank." for name in walk_plank(names, N): pass return name >>> def safeN(names, name): "Find the best N to save someone from walking the plank." assert name in names, 'Name must be in names!' for N in itertools.count(1): if save_last(names, N) == name: return N >>> names = 'Andrew Brenda Craig Deidre Edward Felicity Greg Harriet'.split() >>> tuple(walk_plank(names, 2)) ('Brenda', 'Deidre', 'Felicity', 'Harriet', 'Craig', 'Greg', 'Edward', 'Andrew') >>> save_last(names, 2) 'Andrew' >>> safeN(names, 'Andrew') 2 >>> safeN(names, 'Brenda') 19 >>> save_last(names, 19) 'Brenda' >>> tuple(walk_plank(names, 19)) ('Craig', 'Harriet', 'Andrew', 'Felicity', 'Deidre', 'Edward', 'Greg', 'Brenda') >>> 
+1
source

All Articles