deleting every nth element from a list in python 2.7

2024/10/17 18:11:41

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 havebeen captured by pirates. The pirate captain has all of you standingin a circle on the deck of his ship trying to decide in which orderyou should walk the plank. Eventually he decides on the followingmethod:

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

(b) Thefirst person to walk the plank will be the Nth person (starting fromyou).

(c) The captain will then continue around the circle forcingevery Nth person to walk the plank.

(d) Once there is only one personleft, that person will be given freedom.

For example: The crewconsists of: Andrew, Brenda, Craig, Deidre, Edward, Felicity, Greg andHarriet. Andrew selects N=2. The crew will walk the plank in the order:Brenda, Deidre, Felicity, Harriet, Craig, Greg, Edward. Andrew will begiven freedom.

The code i have so far is:

def survivor(names, step):names =  ["Andrew", "Brenda", "Craig", "Deidre", "Edward", "Felicity", "Greg", "Harriet"]Next = step - 1names.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?

Answer

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'
https://en.xdnf.cn/q/73062.html

Related Q&A

Bradley-Roth Adaptive Thresholding Algorithm - How do I get better performance?

I have the following code for image thresholding, using the Bradley-Roth image thresholding method. from PIL import Image import copy import time def bradley_threshold(image, threshold=75, windowsize=5…

How to display all images in a directory with flask [duplicate]

This question already has answers here:Reference template variable within Jinja expression(1 answer)Link to Flask static files with url_for(2 answers)Closed 6 years ago.I am trying to display all image…

Reindex sublevel of pandas dataframe multiindex

I have a time series dataframe and I would like to reindex it by Trials and Measurements.Simplified, I have this:value Trial 1 0 131 32 42 3 NaN4 123…

How to publish to an Azure Devops PyPI feed with Poetry?

I am trying to set up Azure Devops to publish to a PyPI feed with Poetry. I know about Twine authentication and storing credentials to an Azure Key Vault. But is there any more straightforward method?…

Python Regex Match Before Character AND Ignore White Space

Im trying to write a regex to match part of a string that comes before / but also ignores any leading or trailing white space within the match.So far Ive got ^[^\/]* which matches everything before the…

Python Twisted integration with Cmd module

I like Pythons Twisted and Cmd. I want to use them together.I got some things working, but so far I havent figured out how to make tab-completion work, because I dont see how to receive tab keypres ev…

Read .pptx file from s3

I try to open a .pptx from Amazon S3 and read it using the python-pptx library. This is the code: from pptx import Presentation import boto3 s3 = boto3.resource(s3)obj=s3.Object(bucket,key) body = obj.…

PIL image display error It looks like the image was moved or renamed

Here is a bit of my code:from PIL import Imageimage = Image.open(fall-foliage-1740841_640.jpg) image.show()The error is when the default photo viewer is started and shows the error "It looks like …

How to delete numpy nan from a list of strings in Python?

I have a list of strings x = [A, B, nan, D]and want to remove the nan.I tried:x = x[~numpy.isnan(x)]But that only works if it contains numbers. How do we solve this for strings in Python 3+?

Pasting data into a pandas dataframe

This is the same question as this question, which is marked as a duplicate of this one. The problem, and the reason Im still asking, is that the solution provided (using pandas.read_clipboard()) curren…