# 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_materialNone, target_categoryNone, min_stock1): 枚举算法遍历全部珠宝筛选满足约束的商品 :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_budget6000) 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_budget10000, 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_budget5000, 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_budget8000, target_material钻石, target_category戒指 ) print(customer4_result if customer4_result else 无匹配首饰)输出