मुझे कोड बनाने के लिए एक कार्य दिया गया है। कार्य इस प्रकार है के रूप में:पायथन में एक सूची से प्रत्येक एनएच तत्व को हटा देना 2.7
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.
कोड मैं अब तक है:
def survivor(names, step):
names = ["Andrew", "Brenda", "Craig", "Deidre", "Edward", "Felicity", "Greg", "Harriet"]
Next = step - 1
names.pop(Next)
print names
इस सूची में से पहला वें व्यक्ति निकल जाएगा लेकिन मुझे यकीन नहीं कर रहा हूँ कैसे सूची लूप करने के लिए एनएच व्यक्ति को हटाने के लिए।
मुझे इसकी आवश्यकता है इसलिए चरण = 3 मान लें, फिर मुझे क्रेग को हटाने के लिए इसकी आवश्यकता है और फिर क्रेग से आगे की गणना करें और अगले तीसरे तत्व को हटा दें जो कि फेलिसिटी है और तब तक जब तक कोई व्यक्ति नहीं छोड़ा जाता है।
मैं यह कैसे कर सकता हूं?
क्या कोई मेरी मदद कर सकता है ????????/ – user1839493
तो जो व्यक्ति चुनाव करता है वह हमेशा सूची में पहला होता है? –
मैंने पहले भाग के लिए अपना कोड इस्तेमाल किया है जो है: अगला = चरण - 1 जबकि लेन (नाम)> 1: names.pop (अगला) अगला = अगला + चरण अगला = (अगला - 1)% लेन (नाम) प्रिंट नाम रिटर्न नाम [0] जो उत्तरजीवी लौटने के लिए काम करता है लेकिन जब मैं दूसरे भाग को लागू करने की कोशिश करता हूं, तो यह \t पर काम करने की प्रतीत नहीं होता है। मैंने इसका उपयोग करने की कोशिश की है: उत्तरजीवी में कदम के लिए नामों का नाम दें (नाम, चरण): यदि उत्तरजीवी == नाम: लेकिन यह काम नहीं करता है यह कहते हैं कि UnboundLocalError: असाइनमेंट रिटर्न चरण – user1839493