有关vending machine (自动售货机) 的模拟题,需要吐槽的是:能够转的自动售货机?你找一个出来给我看看
总之,虽然现在觉得意思明白,但是在写代码之前只觉得题目莫名其妙
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class VendingMachine: 2 def motorUse(self, prices, purchases): 3 machine = Machine(prices) 4 lastBuyTime = 0 5 6 machine.moveToMaxPrice() 7 for i in range(0, len(purchases)): 8 pur = processPurchase(purchases[i]) 9 if pur[2] - lastBuyTime >= 5: 10 machine.moveToMaxPrice() 11 lastBuyTime = pur[2] 12 if machine.pick(pur[0], pur[1]) == -1: 13 return -1 14 machine.moveToMaxPrice() 15 16 return machine.motoUse() 17 18 19 def processPurchase(string): 20 s1 = string.find(',') 21 s2 = string.find(':') 22 shelf = int(string[0:s1]) 23 column = int(string[s1+1:s2]) 24 time = int(string[s2+1:]) 25 return (shelf, column, time) 26 27 28 class Machine: 29 def __init__(self, prices): 30 self._column = [] 31 self._motoUse = 0 32 self._curPlace = 0 33 34 totalColumn = len(prices[0].split(' ')) 35 for i in range(0, totalColumn): 36 self._column.append(Column()) 37 38 for shelfIndex in range(0, len(prices)): 39 string = prices[shelfIndex] 40 p = string.split(' ') 41 p = [int(numStr) for numStr in p] 42 for columnIndex in range(0, len(p)): 43 self._column[columnIndex].append(p[columnIndex]) 44 45 46 def motoUse(self): 47 return self._motoUse 48 49 50 # 返回-1表示无效 51 def pick(self, shelfIndex, columnIndex): 52 price = self._column[columnIndex].pickShelf(shelfIndex) 53 if price == -1: 54 return -1 55 self._moveTo(columnIndex) 56 return True 57 58 def moveToMaxPrice(self): 59 place = self._findMaxPrice() 60 self._moveTo(place) 61 62 def _moveTo(self, place): 63 self._motoUse += self._countMinMove(place, self._curPlace) 64 self._curPlace = place 65 66 def _countMinMove(self, place1, place2): 67 offset = abs(place1 - place2) 68 return min(offset, len(self._column) - offset) 69 70 def _findMaxPrice(self): 71 maxNum = -999 72 index = -999 73 for i in range(0, len(self._column)): 74 if self._column[i].totalPrice() > maxNum: 75 maxNum = self._column[i].totalPrice() 76 index = i 77 return index 78 79 80 class Column: 81 def __init__(self): 82 self._totalPrice = 0 83 self._shelf = [] 84 85 def totalPrice(self): 86 return self._totalPrice 87 88 def pickShelf(self, index): 89 price = self._shelf[index] 90 if price == -1: 91 return -1 92 else: 93 self._totalPrice -= price 94 self._shelf[index] = -1 95 return price 96 97 def append(self, price): 98 self._totalPrice += price 99 self._shelf.append(price) 100 101 102 103 104 105 # test 106 o = VendingMachine() 107 m = Machine(("100 100 100", )) 108 assert(len(m._column) == 3) 109 110 111 # test case 112 assert(o.motorUse(("100 100 100", ), ("0,0:0", "0,2:5", "0,1:10")) == 4) 113 assert(o.motorUse(("100 200 300 400 500 600", ), 114 ("0,2:0", "0,3:5", "0,1:10", "0,4:15")) == 17) 115 assert(o.motorUse(("100 100 100", ), ("0,0:10", "0,0:11")) == -1) 116 assert(o.motorUse(("100 200 300", 117 "600 500 400"), ("0,0:0", "1,1:10", "1,2:20", 118 "0,1:21", "1,0:22", "0,2:35")) == 6)
感觉代码写的有点太多了,比赛的时候肯定没时间写