# encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Enumeration Algorithm # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/7/14 23:26 # User : geovindu # Product : PyCharm # Project : PyAlgorithms # File : Enumeration.py # class Jewelry: """ 珠宝商品数据模型 """ def __init__(self, j_id, category, material, price, stock): """ 珠宝商品 :param j_id: 商品编号 :param category: 品类:项链/戒指/手镯/耳饰 :param material: 材质:黄金/铂金/钻石/银饰/K金 :param price: 单价(元) :param stock: 库存数量 """ self.j_id = j_id self.category = category self.material = material self.price = price self.stock = stock def __repr__(self): return f"【{self.j_id}】{self.category} | {self.material} | 售价:{self.price}元 | 库存:{self.stock}件" # ====================== 枚举算法核心:遍历所有珠宝筛选符合条件商品 ====================== def enumeration_filter_jewelry(jewelry_list, max_budget, target_material=None, target_category=None, min_stock=1): """ 枚举算法:遍历全部珠宝,筛选满足约束的商品 :param jewelry_list: 全店珠宝列表 :param max_budget: 顾客最高预算 :param target_material: 指定材质,None不限制 :param target_category: 指定品类,None不限制 :param min_stock: 最低库存要求 :return: 筛选后的可选珠宝列表(全部可行解) """ match_result = [] # 枚举:逐个遍历店内所有珠宝(全部候选解) for item in jewelry_list: # 约束1:价格不超过预算 if item.price > max_budget: continue # 约束2:库存充足 if item.stock < min_stock: continue # 约束3:材质匹配(有指定才校验) if target_material is not None and item.material != target_material: continue # 约束4:品类匹配(有指定才校验) if target_category is not None and item.category != target_category: continue # 全部条件满足,加入可行解集合 match_result.append(item) return match_result def enum_jewelry_combination(jewelry_list, total_budget): """ 枚举两件首饰组合,总价不超过预算 :param jewelry_list: :param total_budget: :return: """ combo_list = [] # 第一层枚举项链 for necklace in [x for x in jewelry_list if x.category == "项链"]: # 第二层枚举戒指 for ring in [x for x in jewelry_list if x.category == "戒指"]: total_price = necklace.price + ring.price if total_price <= total_budget and necklace.stock > 0 and ring.stock > 0: combo_list.append((necklace, ring, total_price)) return combo_list def init_jewelry_store(): """ 初始化门店珠宝库存数据 :return: """ store_goods = [ Jewelry("N001", "项链", "黄金", 5280, 12), Jewelry("N002", "项链", "铂金", 7600, 3), Jewelry("N003", "项链", "钻石", 12800, 5), Jewelry("N004", "项链", "K金", 3680, 8), Jewelry("R001", "戒指", "黄金", 2150, 15), Jewelry("R002", "戒指", "钻石", 9999, 2), Jewelry("R003", "戒指", "银饰", 599, 30), Jewelry("B001", "手镯", "黄金", 8600, 4), Jewelry("B002", "手镯", "银饰", 1280, 22), Jewelry("E001", "耳饰", "K金", 1680, 18), Jewelry("E002", "耳饰", "铂金", 4200, 6), Jewelry("E003", "耳饰", "钻石", 6500, 0), # 无库存,会被过滤 ] return store_goods # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Enumeration Algorithm # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/7/14 23:28 # User : geovindu # Product : PyCharm # Project : PyAlgorithms # File : EnumerationBll.py from Enumeration.Enumeration import init_jewelry_store,enumeration_filter_jewelry class EnumerationBll(object): """ """ def demo(self): """ :return: """ all_jewelry = init_jewelry_store() print("===== 珠宝门店全部商品(枚举全集) =====") for goods in all_jewelry: print(goods) print("-" * 70) # 场景1:顾客1 预算6000内,不限材质、不限品类,有货即可 print("\n【顾客需求1】预算≤6000元,任意品类任意材质,有库存") customer1_result = enumeration_filter_jewelry(all_jewelry, max_budget=6000) if customer1_result: for res in customer1_result: print(res) else: print("无符合条件首饰") # 场景2:顾客2 预算10000内,只想要黄金手镯 print("\n【顾客需求2】预算≤10000元,仅黄金手镯") customer2_result = enumeration_filter_jewelry( all_jewelry, max_budget=10000, target_material="黄金", target_category="手镯" ) for res in customer2_result: print(res) # 场景3:顾客3 预算5000内,铂金耳饰 print("\n【顾客需求3】预算≤5000元,铂金耳饰") customer3_result = enumeration_filter_jewelry( all_jewelry, max_budget=5000, target_material="铂金", target_category="耳饰" ) for res in customer3_result: print(res) # 场景4:顾客4 预算8000,钻石戒指(库存不足/超预算测试) print("\n【顾客需求4】预算≤8000元,钻石戒指") customer4_result = enumeration_filter_jewelry( all_jewelry, max_budget=8000, target_material="钻石", target_category="戒指" ) print(customer4_result if customer4_result else "无匹配首饰")输出: