Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

deleting every nth element from a list in python 2.7

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?

like image 615
user1839493 Avatar asked Nov 20 '12 16:11

user1839493


People also ask

How do you remove all of a certain value from a list Python?

How to Remove an Element from a List Using the remove() Method in Python. To remove an element from a list using the remove() method, specify the value of that element and pass it as an argument to the method. remove() will search the list to find it and remove it.

How do I remove multiple elements from a set in Python?

Given a set, the task is to write a Python program remove multiple elements from set. Example: Input : test_set = {6, 4, 2, 7, 9}, rem_ele = [2, 4, 8] Output : {9, 6, 7} Explanation : 2, 4 are removed from set.


2 Answers

This seems to work:

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]

It prints the names of the pirate's victims 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'
like image 174
Lev Levitsky Avatar answered Sep 20 '22 20:09

Lev Levitsky


The following code should do everything that you asked for, including implementing 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 some example usage of the code up above while working with IDLE in 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')
>>> 
like image 23
Noctis Skytower Avatar answered Sep 19 '22 20:09

Noctis Skytower