2012-11-20 11 views
6

मुझे कोड बनाने के लिए एक कार्य दिया गया है। कार्य इस प्रकार है के रूप में:पायथन में एक सूची से प्रत्येक एनएच तत्व को हटा देना 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 मान लें, फिर मुझे क्रेग को हटाने के लिए इसकी आवश्यकता है और फिर क्रेग से आगे की गणना करें और अगले तीसरे तत्व को हटा दें जो कि फेलिसिटी है और तब तक जब तक कोई व्यक्ति नहीं छोड़ा जाता है।

मैं यह कैसे कर सकता हूं?

+0

क्या कोई मेरी मदद कर सकता है ????????/ – user1839493

+0

तो जो व्यक्ति चुनाव करता है वह हमेशा सूची में पहला होता है? –

+0

मैंने पहले भाग के लिए अपना कोड इस्तेमाल किया है जो है: अगला = चरण - 1 जबकि लेन (नाम)> 1: names.pop (अगला) अगला = अगला + चरण अगला = (अगला - 1)% लेन (नाम) प्रिंट नाम रिटर्न नाम [0] जो उत्तरजीवी लौटने के लिए काम करता है लेकिन जब मैं दूसरे भाग को लागू करने की कोशिश करता हूं, तो यह \t पर काम करने की प्रतीत नहीं होता है। मैंने इसका उपयोग करने की कोशिश की है: उत्तरजीवी में कदम के लिए नामों का नाम दें (नाम, चरण): यदि उत्तरजीवी == नाम: लेकिन यह काम नहीं करता है यह कहते हैं कि UnboundLocalError: असाइनमेंट रिटर्न चरण – user1839493

उत्तर

6

यह काम करने के लिए लगता है:

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] 

यह समुद्री डाकू के पीड़ितों के नाम प्रिंट और उत्तरजीवी के नाम देता है:

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' 
+0

से पहले स्थानीय चर 'चरण' संदर्भित लेव धन्यवाद, यह पूरी तरह से काम करता है। आप नहीं जानते कि मैं इसकी कितनी सराहना करता हूं, मैं इस पर उम्र के लिए फंस गया हूं। धन्यवाद – user1839493

+0

@ user1839493 यह केवल तभी मदद मिलेगी जब आप समझें कि यह शिक्षक को सौंपने के बजाय कैसे काम करता है (क्षमा करें अगर मैंने गलत अनुमान लगाया है)। साथ ही, यदि आप समस्या हल कर लेते हैं तो आप [उत्तर के रूप में उत्तर को चिह्नित कर सकते हैं] (http://meta.stackexchange.com/a/5235/181223)। –

+0

क्या आप कृपया मुझे डेक भाग समझा सकते हैं? – user1839493

1

निम्नलिखित कोड सब कुछ है कि आप के लिए कहा क्या करना चाहिए, को लागू करने सहित safeN समारोह:

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 

संपादित करें: विंडोज में आईडीएलई के साथ काम करते समय ऊपर दिए गए कोड का कुछ उदाहरण उपयोग यहां दिया गया है।

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') 
>>> 
+0

आपकी मदद के लिए धन्यवाद नोक्टीस स्काईवाटर, अब मैं कार्य के दूसरे भाग को लागू कर सकता हूं – user1839493

+0

मैंने पहले भाग के लिए अपना कोड इस्तेमाल किया है: अगला = चरण - 1 जबकि लेन (नाम)> 1: नाम .pop (अगला) अगला = अगला + कदम अगला = (अगला - 1)% लेन (नाम) प्रिंट नाम वापसी के नाम [0] कि उत्तरजीवी वापस जाने के लिए काम करता है लेकिन जब मैं लागू करने के लिए कोशिश दूसरे भाग का सुझाव दिया इसके अलावा यह काम नहीं करता – user1839493

+0

मैंने इसका उपयोग करने की कोशिश की है: उत्तर में नाम नाम में नाम के लिए उत्तर दें (नाम, चरण): यदि उत्तरजीवी == नाम: लेकिन यह काम नहीं करता है यह कहते हैं कि UnboundLocalError: स्थानीय चर 'चरण' असाइनमेंट से पहले संदर्भित वापसी चरण – user1839493

संबंधित मुद्दे