Flyer test

博客可能围绕Flyer测试展开,但因信息有限,具体测试内容不明。

你看这个代码对吗?import heapq class Flyer: def __init__(self, name, flyer_id, time, status): self.name = name self.id = flyer_id self.time = time self.status = status self.cancelled = False def __str__(self): return "({}, {}, {}, {})".format(self.name, self.id, self.time, self.status) def __repr__(self): return self.__str__() def __lt__(self, other): # For max heap, we reverse the comparison status_rank = {"Super": 4, "Platinum": 3, "Gold": 2, "Silver": 1} self_rank = status_rank[self.status] other_rank = status_rank[other.status] if self_rank != other_rank: return self_rank > other_rank # Higher status has higher priority else: return self.time < other.time # Earlier time has higher priority class NUAUpgradeSystem: def __init__(self): self.heap = [] self.flyer_map = {} def add_flyer(self, name, flyer_id, time, status): """Add a flyer to the system""" flyer = Flyer(name, flyer_id, time, status) self.flyer_map[flyer_id] = flyer heapq.heappush(self.heap, flyer) def cancel_flyer(self, flyer_id): """Cancel a flyer's upgrade request""" if flyer_id in self.flyer_map: self.flyer_map[flyer_id].cancelled = True def get_top_k(self, k): """Get k highest priority non-cancelled flyers using lazy deletion""" result = [] temp_heap = [] # Use lazy deletion: extract until we find k valid flyers while len(result) < k and self.heap: current = heapq.heappop(self.heap) if not current.cancelled: result.append(current) # Push to temp heap to restore later temp_heap.append(current) # Restore the original heap for flyer in temp_heap: heapq.heappush(self.heap, flyer) return result def process_input(self, input_lines): """Process input lines and return results""" if not input_lines: return [] # Parse first line first_line = input_lines[0].split() if len(first_line) < 3: return [] n = int(first_line[0]) k = int(first_line[1]) c = int(first_line[2]) # Process flyer information for i in range(1, 1 + n): if i >= len(input_lines): break parts = input_lines[i].split() if len(parts) < 4: continue name = parts[0] flyer_id = int(parts[1]) time = int(parts[2]) status = parts[3] self.add_flyer(name, flyer_id, time, status) # Process cancellations for i in range(1 + n, 1 + n + c): if i >= len(input_lines): break try: flyer_id = int(input_lines[i]) self.cancel_flyer(flyer_id) except ValueError: continue # Get top k flyers return self.get_top_k(k) def main(): import sys # Read input from stdin input_lines = [] try: for line in sys.stdin: if line.strip(): # Skip empty lines input_lines.append(line.strip()) except: pass # If no input from stdin, use test data if not input_lines: # Default test case input_lines = [ "5 2 1", "A 1 120 Platinum", "B 5 60 Super", "C 3 140 Super", "D 2 130 Gold", "E 4 150 Silver", "5" ] # Create system and process input system = NUAUpgradeSystem() result = system.process_input(input_lines) # Output results for flyer in result: print(flyer) if __name__ == "__main__": main()
08-25
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值