Application of Permutation and Combination
Reference
https://www.shuxuele.com/combinatorics/combinations-permutations.html
Online Tool
https://gadget.chienwen.net/x/math/percomb
Cracking the Safe!
假设你要暴力破解一个保险箱,密码只有4位,每位有0-9个可能,请思考怎么破解呢?
这个密码排列的特征是:
- 次序重要
- 可重复
那么就选重复排列方式计算即可。
Code:
import itertools
product_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
product = list(itertools.product(product_list, repeat=4))
print("product number :", len(product))
print("product list: ", product)
# out:
product number : 10000
product list: [(1, 1, 1, 1), (1, 1, 1, 2), (1, 1, 1, 3), (1, 1, 1, 4) ... ]
计算比赛前三名有多少种排列方式?
有一场比赛,分别有A,B,C,D,E,F,G,H共8支队伍参加,现在想算到时得前三名的排列情况有多少种?
这个名次排列的特征:
- 次序重要
- 不可以重复
那么就选不重复排列方式计算即可。
from itertools import permutations
itering = permutations(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'], 3)
plist = list(itering)
print("前三名的排列有多少种: ", len(plist))
print("permutations list: ", plist)
# 前三名的排列有多少种: 336
# permutations list: [('A', 'B', 'C'), ('A', 'B', 'D'), ('A', 'B', 'E'), ('A', 'B', 'F'), ... ]
Can you win the lottery?
假设彩票的玩法是从编号为01~33个球中随机抽出6个,顺序不重要。你选6个数组合成一注彩票,如果所选的6个号码都中了,就表示你中了1等奖。
那么有多少种组合情况?
这个中奖号码组合的特征:
- 次序不重要
- 不可重复
用不可重复组合求即可。
from itertools import combinations, permutations
from scipy.special import comb, perm
C = comb(33, 6)
# out: C = 3.0
print("combinations number: ", C)
itering = combinations(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33], 6)
Clist = list(itering)
with open('temp.txt', 'w') as f:
for i in Clist:
f.writelines(str(i) + "\r")
# out:
combinations number: 1107568.0
out:

如何做仙丹?
假设有个神仙要你用5种材料('人参', '萝卜', '火龙果', '鹿茸', '熊掌')做一个仙丹,需要按照特定的配方,但是配方不告诉你,只告诉你做的规则,顺序无所谓,可以有重复,正确的只有一种配方。
你知道你要尝试多少次才能做出仙丹?
这个配方组合的特征:
- 次序不重要
- 可重复
用重复组合求即可。
c_list = ['人参', '萝卜', '火龙果', '鹿茸', '熊掌']
clist = list(itertools.combinations_with_replacement(c_list, 3))
print("一共有多少种组合: ", len(clist))
print("List: ", clist)
#out:
一共有多少种组合: 35
List: [('人参', '人参', '人参'), ('人参', '人参', '萝卜'), ('人参', '人参', '火龙果'), ('人参', '人参', '鹿茸'), ('人参', '人参', '熊掌')...]
Think
Do you know any other application scenarios?
TODO
Permutation Concept
TODO
Combination Concept
TODO
Application of Permutation and Combination的更多相关文章
- Leetcode 46 47 Permutation, 77 combination
Permutation class Solution { List<List<Integer>> res = new ArrayList<List<Integer& ...
- Multiplexing SDIO Devices Using MAX II or CoolRunner-II CPLD
XAPP906 Supporting Multiple SD Devices with CoolRunner-II CPLDs There has been an increasing demand ...
- DDD:小议 BoundexContext 设计
背景 看了这篇文章:Coding for Domain-Driven Design: Tips for Data-Focused Devs,对 BoundedContext 的设计有了一点新的体会,记 ...
- keybd_event函数用法
转自不用winio,直接达到驱动级模拟键盘效果的keybd_event函数用法 键盘模拟技术是在编写游戏外挂时经常使用的技术.但是由于很多游戏采用了directinput的方式,使得发送的一般键盘消息 ...
- Ruby数组方法整理
数组方法整理 方法列表: all().any().none()和one():测试数组中的所有或部分元素是否满足给定条件.条件可以是语句块中决定,也可以是参数决定 append():等价于push() ...
- 算法题思路总结和leecode继续历程
2018-05-03 刷了牛客网的题目:总结思路(总的思路跟数学一样就是化简和转化) 具体启发点: 1.对数据进行预处理排序的思想:比如8皇后问题 2.对一个数组元素进行比较的操作,如果复杂,可以试试 ...
- [Windows Azure] Windows Azure Execution Models
Windows Azure Execution Models Windows Azure provides different execution models for running applica ...
- 【读书笔记】《Computer Organization and Design: The Hardware/Software Interface》(1)
笔记前言: <Computer Organization and Design: The Hardware/Software Interface>,中文译名,<计算机组成与设计:硬件 ...
- Method, apparatus, and system for speculative abort control mechanisms
An apparatus and method is described herein for providing robust speculative code section abort cont ...
- 自然语言处理(NLP) - 数学基础(1) - 排列组合
正如我在<自然语言处理(NLP) - 数学基础(1) - 总述>一文中所提到的NLP所关联的概率论(Probability Theory)知识点是如此的多, 饭只能一口一口地吃了, 我们先 ...
随机推荐
- std常用类型
std::getline 文档 std::reverse 文档 注意事项 reverse()返回值为void,是对原序列进行修改 std::vector 文档 emplace 和 emplace_ba ...
- Seata锁等待超时问题排查
问题描述 生产环境,一个简单的事务方法,提交失败,报 Global lock wait timeout 伪代码如下: @GlobalTransactional(rollbackFor = Except ...
- Service Mesh之Istio部署bookinfo
前文我们了解了service mesh.分布式服务治理和istio部署相关话题,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/17281541.html:今天我 ...
- NextCloud 在lnmp下 nginx 的配置
server { listen 80; listen [::]:80; server_name lnmp.org www.lnmp.org; root /home/wwwroot/nextcloud; ...
- linux网络开发者定位问题常用工具和命令总结
本文章来自我的微信个人技术公众号---网络技术修炼,公众号中总结普及网络基础知识,包括基础原理.网络方案.开发经验和问题定位案例等,欢迎关注. Linux网络开发者面临的问题往往比较复杂,因此需要使用 ...
- 从原理聊JVM(一):染色标记和垃圾回收算法
作者:京东科技 康志兴 1 JVM运行时内存划分 1.1 运行时数据区域 • 方法区 属于共享内存区域,存储已被虚拟机加载的类信息.常量.静态变量.即时编译器编译后的代码等数据.运行时常量池,属于方法 ...
- P1350 车的放置 题解
一.题目描述: 给你一个网格棋盘,a,b,c,d 表示了对应边长度,也就是对应格子数. 例如,当 a=b=c=d=2 时,对应了下面这样一个棋盘: 想要在这个棋盘上放 k 棋子,也就是这 k 个棋子没 ...
- 如何建设一个用于编译 iOS App 的 macOS 云服务器集群?
作者:京东零售 叶萌 现代软件开发一般会借助 CI/CD 来提升代码质量.加快发版速度.自动化重复的事情,iOS App 只能在 mac 机器上编译,CI/CD 工具因此需要有一个 macOS 云服务 ...
- [C++基础入门] 1、C++初识
文章目录 1 C++初识 1.1 第一个C++程序 1.1.1 创建项目 1.1.2 创建文件 1.1.3 编写代码 1.1.4 运行程序 1.2 注释 1.3 变量 1.4 常量 1.5 关键字 1 ...
- day08-优惠券秒杀04
功能03-优惠券秒杀04 4.功能03-优惠券秒杀 4.7Redis优化秒杀 4.7.1优化分析 现在来回顾一下优惠券秒杀业务的两个主要问题: (1)首先是对优惠券的扣减,需要防止库存超卖现象: (2 ...