2016-04-16 6 views
-1

笑い駐車場には、最大10台の車を収容する1つの車線があります。車はガレージの南端に到着し、北端から出る。顧客が最北端でない車 をピックアップするように到着した場合、車の北にあるすべての車は移動し、車は追い出され、 と他の車は本来の順序と同じ順序で復元されます。 車が出るたびに、南にあるすべての車が前方に移動します。したがって、すべて 回、すべての空きスペースがガレージの南側にあります。 pythonプログラムを書くと、入力行のグループが読み込まれます。各行には、「a」到着または「 出発」とナンバープレート番号が含まれています。車は、入力によって指定された の順序で到着し、出発すると仮定される。プログラムは、車が到着するか、または が出発するたびにメッセージを出力する必要があります。車が到着したら、マッサージは車に余裕があるかどうかを指定する必要があります。 ガレージ。車のための余裕がない場合、車は部屋があるか、または出発ラインが 車のために読むまで待つ。部屋が利用可能になると、別のマッサージを印刷する必要があります。車 が出発した場合、マッサージは車がガレージ (到着ではなく出発自体を含む)内で移動された回数を含める必要があります。車が 待ち行列から出発する場合、この数は0です。駐車場に関するコード

これは私のコードです。私はコードの途中で立ち往生した。私は車を駐車するために待ち行列を作った。私は途中の車が離れるときに車を再組み立てする方法はありません。車で公園に行く前に移動回数を印刷する方法が欲しい。誰でも私を助けることができますか? '

class Stack: 
    def __init__(self): 
     self.items =[] 

    def isEmpty(self): 
     return self.items ==[] 

    def push(self,item): 
     self.items.append(item) 

    def pop(self): 
     return self.items.pop() 

    def peek(self): 
     return self.items[len(self.items)-1] 

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

class Queue: 
    def __init__(self,maxSize): 
     self.items =[] 
     self._count = 0 
     self._front = 0 
     self._back = maxSize - 1 


    def isEmpty(self): 
     return self.items ==[] 

    def enqueue(self, item): 
     self.items.insert(0,item) 

    def dequeue(self): 
     return self.items.pop() 

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

    def index(self,item): 
     return self.items.index(item) 



q1park = Queue(maxSize=10) 
q2wait= Queue() 
q3assemble= Queue() 

x =raw_input("Car number: ") 

def cararrival(): 
    if x[0]=="a": 
     while q1park.size ==10: 
      q1park.enqueue(x[1:len(x)]) 
      print(x + "car is arrived") 

      if q1park.size ==10: 
       print("No room available in the garage") 

       x1=raw_input("do you want to wait: ") 
       if x1=="yes": 
        q2wait.enqueue(x[1:len(x)]) 
       elif x1=="no": 
        print("see you next time") 
       else: 
        print("Enter yes or no") 


def cardepart(): 
    if x[0]=="d": 
     if x[1:len(x)] in q1park: 
      while not q1park.index(x[1:len(x)])==0: 
       q3assemble.enqueue(q1park.dequeue()) 
       q3assemble.dequeue() 

      while not q3assemble.isEmpty: 

答えて

1

私の上記の質問に対する答えはここにあります。

#creating a class for car 
class Car: 

    #initialization and creating properties 
    def __init__(self, licenseNum): 
     self.licenseNum = licenseNum 
     self.moveCount = 0 

    #increase the moveCount by 1 
    def move(self): 
     self.moveCount += 1 

    #get information to a string 
    def toString(self): 
     return "Car {0}\tmoves:{1}".format(self.licenseNum, self.moveCount) 


#creating queue class for cars 
class CarQueue: 

    #initialization 
    def __init__(self): 
     self.carList = [] 

    #add car to the queue 
    def enqueue(self, car): 
     self.carList.append(car) 

    #remove car from the queue 
    def dequeue(self): 
     return self.carList.pop(0) 

    #return the car by license plate number 
    def getCar(self, licenseNum): 
     for car in self.carList: 
      if car.licenseNum == licenseNum: 
       return car 

    #check whether license plate number is in the car list or not 
    def contains(self, licenseNum): 
     return self.getCar(licenseNum)!=None 

    #remove car from the list 
    def remove(self, car): 
     if self.contains(car.licenseNum): 
      self.carList.remove(car) 

    #return the number of cars in the list 
    def size(self): 
     return len(self.carList) 

    #check whether list is empty of not 
    def isEmpty(self): 
     return self.size()==0 

    #return the index of the car in the list 
    def positionOf(self, car): 
     return self.carList.index(car) 

    #move forward all the cars in southern side of the position. 
    def moveCarsFrom(self, position): 
     for c in self.carList[position:]: 
      c.move() 

    #get information to a string 
    def toString(self): 
     carNameList = [] 
     for car in self.carList: 
      carNameList.append(car.licenseNum) 
     return "[{0}]".format(" ".join(carNameList)) 


#creating class for the garage 
class Garage: 

    #initialization with the given number of rooms in the garage 
    def __init__(self, maxSize): 
     self.carQueue = CarQueue() 
     self.maxSize = maxSize 

    #add car to the list, when arrive 
    def arrive(self, car): 
     self.carQueue.enqueue(car) 

    #remove car from the list and record movements of other cars to the southern side, when depart 
    def depart(self, car): 
     position = self.carQueue.positionOf(car) 
     self.carQueue.remove(car) 
     self.carQueue.moveCarsFrom(position) 

    #check whether the garage is full or not 
    def isFull(self): 
     return self.carQueue.size()==self.maxSize 



#create new garage which can hold up to 10 cars 
garage = Garage(10) 

#create new CarQueue to store waiting cars 
waiting = CarQueue() 

header = """ 
############################################## 
       PARKING GARAGE 
############################################## 
Welcome! 
""" 

help = """ 
you have to input your command and press enter. 
[a/d] [LICENSE PLATE NUMBER], for car arrival or departure 
    a, arrival 
    d, departure 

help, show instructions 
exit, quit the programme 
show, show the queues in the garage and the waiting line 
""" 

#display the header 
print("{0}Instructions:{1}\nPlease enter the command...".format(header, help)) 

#infinity loop to get user inputs again and again 
while True: 

    #get user command 
    command = raw_input("\nlaughs> ") 

    #excecuting general commands 
    if command=="exit": 
     print("Good bye!\n") 
     break 
    elif command=="help": 
     print(help) 
     continue 
    elif command=="show": 
     print("Garage  {0}: {1}".format(garage.carQueue.size(), garage.carQueue.toString())) 
     print("Waiting line {0}: {1}".format(waiting.size(), waiting.toString())) 
     continue 

    #get seperately the action character ('a' or 'd') and the license plate number from the command 
    action = "" 
    licenseNum = "" 
    try: 
     action,licenseNum = command.split() 
    except: 
     #show error message for invalid inputs 
     print("Invalid input! Try again. Enter 'help' for instructions") 
関連する問題