2017-12-10 5 views
1

リストに3人をエンキューして、すべての人がトップに表示された結果を表示するようにしましたが、名前なしで1つの結果しか得られませんでした:キューのデータ構造リスト(Python)からアイテムを呼び出す

Contacting the following 
Phone answered: Yes 
Booked an appointment: No 
Reshedule an appointment again. 

私は出力が2回表示しないように、すべて自分の名前と3つの出力、'names'内部に格納された情報から、それぞれの人のための1、およびそれぞれの名前を表示するようにしたいです。

リストに基づいてキューの優先順位を設定するキューを使用したいので、それらを順番に入れようとしています。 ifとelifは、ランダムジェネレータに応じていずれかのカテゴリに入る条件です。さて、内部に名前を含める方法は定義されていません。

コード

import random 

class Queue: 
    def __init__(self): 
     self.container = [] 

    def isEmpty(self): 
     return self.size() == 0 

    def enqueue(self, item): 
     self.container.append(item) 

    def dequeue(self): 
     self.container.pop(0) 

    def size(self): 
     return len(self.container) 

    def peek(self) : 
     return self.container[0] 

names = ["Alvin", "James", "Peter"] 

# Enqueuing 

q = Queue() 
q.enqueue(random.choice(names)) 

# Dequeuing and Printing 
print("Contacting the following:\n" + "\n".join(q.container)) # unsure about this 




for i in range(q.size()): 

    answered = random.randint(0,1) 
    booked = random.randint(0, 1) 

    if(answered == 1 and booked == 1): 
     print("Now Calling -" + (q.names)) # unsure about this 
     print("Phone answered: Yes") 
     print("Booked an appointment: Yes") 
     print("Booking successful.") 

    elif(answered==1 and booked==0): 
     print("Now Calling -" + (q.names)) # unsure about this 
     print("Phone answered: Yes") 
     print("Booked an appointment: No") 
     print("Reshedule an appointment again.") 

    elif(answered == 0): 
     print("Now Calling -" + (q.names)) # unsure about this 
     print("Phone answered: No") 
     print("Reshedule a callback.") 

    q.dequeue() 

例所望の出力:

Contacting the following 
Alvin 
James 
Peter 

Now Calling - James 
Phone answered: No 
Reshedule a callback. 
+0

たhttps://stackoverflow.com/questions/47736687/show-names-in-queue-data-structure#47736687)を削除したとき... –

+0

残念です。私は自分自身でそれを理解しようと思った。 – John

答えて

1

私はあなたのキューclassへの変更のカップルを作りました。主に、.dequeueメソッドはポップするアイテムを返さなかったので、デフォルト値のNoneを返します。

また、.sizeメソッドを__len__に変更したので、Queueインスタンスを組み込みのlen関数に渡すことができます。そして、それにiterメソッドを与えて、それを容易にforループで使用するか、.joinに渡すことができます。 .isEmpty.is_emptyに変更し、PythonのPEP-0008スタイルガイドに準拠させました。

各名前を繰り返しなくキューに追加したいので、ここではrandom.choiceは不要です。代わりにrandom.shuffleを使用できます。別のオプションはrandom.sampleを使用することですが、これはリストから部分的に選択したいときに適しています。私はあなたのコード内でその目的のために.joinを使用しているため、名前の全てを印刷する

print('\n'.join(q)) 

を使用上記のコードで

from random import seed, shuffle, randrange 

# Seed the randomizer so we can reproduce results while testing 
seed(9) 

class Queue: 
    def __init__(self): 
     self.container = [] 

    def __len__(self): 
     return len(self.container) 

    def is_empty(self): 
     return len(self) == 0 

    def enqueue(self, item): 
     self.container.append(item) 

    def dequeue(self): 
     return self.container.pop(0) 

    def peek(self) : 
     return self.container[0] 

    def __iter__(self): 
     return iter(self.container) 

names = ["Alvin", "James", "Peter"] 

# Enqueuing 
q = Queue() 

# Make a temporary copy of the names that we can 
# shuffle without affecting the original list 
temp = names.copy() 
shuffle(temp) 

# Put the shuffled names onto the queue 
for name in temp: 
    q.enqueue(name) 

# Dequeuing and Printing 
print("Contacting the following") 
print('\n'.join(q)) 
#for name in q: 
    #print(name) 

while not q.is_empty(): 
    name = q.dequeue() 
    print('\nNow Calling -', name) 

    answered = randrange(2) 
    booked = randrange(2) 

    if answered: 
     print("Phone answered: Yes") 
     if booked: 
      print("Booked an appointment: Yes") 
      print("Booking successful.") 
     else: 
      print("Booked an appointment: No") 
      print("Reshedule an appointment again.") 
    else: 
     print("Phone answered: No") 
     print("Reshedule a callback.") 

出力

Contacting the following 
Alvin 
Peter 
James 

Now Calling - Alvin 
Phone answered: Yes 
Booked an appointment: No 
Reshedule an appointment again. 

Now Calling - Peter 
Phone answered: No 
Reshedule a callback. 

Now Calling - James 
Phone answered: Yes 
Booked an appointment: Yes 
Booking successful. 

。しかし、私はまた、単純なforループを使用して別の方法を示したが、私はそれをコメントアウト:(私はあなたの[前の質問]に答えを書いている途中で

for name in q: 
    print(name) 
+0

を出力すると、リストから 'names = [" Alvin "、" James "、" Peter "]の代わりにAlvin、Peter、Jamesが表示されます。私はそれを整理する方法がありますか? – John

+0

また、私は同じ結果を得続けている、それはそれ自体シャッフルのようだ。 – John

+0

@Johnあなたのコードは 'random.choice'を使用していますので、ランダムな順序で名前をキューに追加したいと思っていました。名前を元の順序でエンキューするには、 'names'をエンキューして' temp'を取り除いてください。私のコードでは 'random.shuffle'を使ってランダムな名前を繰り返さないようにしています。我々は 'seed(9)'で乱数を播種しているので、実行ごとに同じ乱数を選択します。その行をコメントアウトすると、毎回違う順番が選択されます。 –

関連する問題