我被赋予了创建代码的任务.任务如下:
你是一艘帆船的船长,你和你的船员被海盗俘虏了.海盗船长让你们所有人在他的船甲板上站成一圈,试图决定你应该走哪条木板.最终他决定采用以下方法:
(a)海盗船长要你挑一个号码N.
(b)第一个走板的人是第N人(从你开始).
(c)然后船长将继续围绕圆圈,迫使每个第N个人走板.
(d)一旦只剩下一人,该人将获得自由.
例如:剧组包括:Andrew,Brenda,Craig,Deidre,Edward,Felicity,Greg和Harriet.安德鲁选择N = 2.船员将按顺序走板:Brenda,Deidre,Felicity,Harriet,Craig,Greg,Edward.安德鲁将获得自由.
我到目前为止的代码是:
def survivor(names, step):
names = ["Andrew", "Brenda", "Craig", "Deidre", "Edward", "Felicity", "Greg", "Harriet"]
Next = step - 1
names.pop(Next)
print names
Run Code Online (Sandbox Code Playgroud)
这将从列表中删除第一个第n个人,但我不知道如何循环列表以继续删除第n个人.
我需要它,所以让我们假设步骤= 3,然后我需要它去除克雷格然后从克雷格开始计算并删除下一个第三个元素,这是幸福等等,直到有一个人离开.
我怎样才能做到这一点?
这就是我所拥有的,评论描述了我想要做的事情
在文本文件中放置了一些单词拼写错误的单词以及测试文本文件以及用于拼写检查的单词.
例如>>> spellCheck("test1.txt"){'exercsie':1,'finised':1}
from string import ascii_uppercase, ascii_lowercase
def spellCheck(textFileName):
# Use the open method to open the words file.
# Read the list of words into a list named wordsList
# Close the file
file=open("words.txt","r")
wordsList = file.readlines()
file.close()
# Open the file whos name was provided as the textFileName variable
# Read the text from the file into a list called wordsToCheck
# Close the file
file=open(textFileName, "r")
wordsToCheck = file.readlines()
file.close()
for i in range(0,len(wordsList)): wordsList[i]=wordsList[i].replace("\n","") …Run Code Online (Sandbox Code Playgroud)