news 2026/7/15 1:20:01

python: Enumeration Algorithm

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
python: Enumeration Algorithm
# 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 "无匹配首饰")

输出:

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/15 1:18:53

终极PS4存档管理神器Apollo:免费跨平台解决方案完整指南

终极PS4存档管理神器Apollo&#xff1a;免费跨平台解决方案完整指南 【免费下载链接】apollo-ps4 Apollo Save Tool (PS4) 项目地址: https://gitcode.com/gh_mirrors/ap/apollo-ps4 Apollo Save Tool 是一款专为PlayStation 4设计的开源存档管理工具&#xff0c;让玩家…

作者头像 李华
网站建设 2026/7/15 1:18:05

3分钟掌握音乐解锁技术:Unlock Music浏览器解密工具完全指南

3分钟掌握音乐解锁技术&#xff1a;Unlock Music浏览器解密工具完全指南 【免费下载链接】unlock-music 在浏览器中解锁加密的音乐文件。原仓库&#xff1a; 1. https://github.com/unlock-music/unlock-music &#xff1b;2. https://git.unlock-music.dev/um/web 项目地址:…

作者头像 李华
网站建设 2026/7/15 1:11:54

CSAPP:BombLab 逆向工程实战指南——从汇编指令到密码破解

1. BombLab实验环境搭建与工具准备逆向工程就像侦探破案&#xff0c;而BombLab就是你的第一个犯罪现场。工欲善其事必先利其器&#xff0c;我们先来准备办案工具包。推荐使用Ubuntu 20.04 LTS作为基础环境&#xff0c;这个版本对新手最友好。安装GDB调试器只需一条命令&#xf…

作者头像 李华
网站建设 2026/7/15 1:10:34

PMSM核心结构与旋转磁场原理剖析

1. PMSM的基本结构解析永磁同步电机&#xff08;Permanent Magnet Synchronous Motor, PMSM&#xff09;是现代工业中高效能电机的代表&#xff0c;其核心结构主要由定子、转子和端盖三部分组成。定子部分装有精心设计的三相交流绕组&#xff0c;这些绕组环绕着定子铁芯排列。当…

作者头像 李华