需求: 在你的面前有一个n阶的台阶,你一步只能上1级或者2级,请计算出你可以采用多少种不同的方法爬完这个楼梯?输入一个正整数表示这个台阶的级数,输出一个正整数表示有多少种方法爬完这个楼梯。
分析:提炼出题干的意思:用1和2产生不同组合,使得他们的和等于台阶的级数,输出有多少种组合方式。
解决: 主要的问题就是如何利用1和2产生不同的组合,查阅了python关于排列组合相关的资料
最后发现了一个强大的python库 itertools
介绍一下常用的几个函数:
itertools.product(sequence,repeat) #从sequence中拿出repeat个数做排列(repeat关键字传参) 有放回的拿出 repeat与sequence的长度无关。
demo: 输出为类型为元组,
In [2]: import itertools In [3]: for i in itertools.product([1,2],repeat= 1): ...: print(i) ...: ...: (1,) (2,) In [4]: for i in itertools.product([1,2],repeat= 2): ...: print(i) ...: ...: (1, 1) (1, 2) (2, 1) (2, 2) In [5]: for i in itertools.product([1,2],repeat= 3): ...: print(i) ...: ...: (1, 1, 1) (1, 1, 2) (1, 2, 1) (1, 2, 2) (2, 1, 1) (2, 1, 2) (2, 2, 1) (2, 2, 2)
itertools.permutations(sequence,n) # 从sequence中拿出n个数做排列, 不放回的拿出, n 只能小于等于sequence的长度 否则没有输出。
demo:
In [6]: for i in itertools.permutations([1,2], 1): ...: print(i) ...: ...: (1,) (2,) In [7]: for i in itertools.permutations([1,2], 2): ...: print(i) ...: ...: (1, 2) (2, 1) In [8]: for i in itertools.permutations([1,2],3): ...: print(i) ...: ...: In [9]:
itertools.combinations(sequence, n ) # 从sequence中选n个数做组合,相当于不放回, 当n 大于sequence的长度自然没有数据。
demo:
In [10]: for i in itertools.combinations([1,2],1): ...: print(i) ...: ...: (1,) (2,) In [11]: for i in itertools.combinations([1,2],2): ...: print(i) ...: ...: (1, 2) In [12]: for i in itertools.combinations([1,2],3): ...: print(i) ...: ...: In [13]:
itertools.combinations_with_replacement(sequence, n ) # 从sequence中选n个数做组合,允许元素重复,repeat与sequence的长度无关。
demo:
In [14]: for i in itertools.combinations_with_replacement([1,2],1): ...: print(i) ...: ...: (1,) (2,) In [15]: for i in itertools.combinations_with_replacement([1,2],2): ...: print(i) ...: ...: (1, 1) (1, 2) (2, 2) In [16]: for i in itertools.combinations_with_replacement([1,2],3): ...: print(i) ...: ...: (1, 1, 1) (1, 1, 2) (1, 2, 2) (2, 2, 2) In [17]: for i in itertools.combinations_with_replacement([1,2],4): ...: print(i) ...: ...: (1, 1, 1, 1) (1, 1, 1, 2) (1, 1, 2, 2) (1, 2, 2, 2) (2, 2, 2, 2)
回到咱们的问题, 在这几个函数中,选择一个,很明显 itertools.product(sequence,repeat) 符合我们的要求:
code:
1 import itertools 2 n = int(input("输入台阶数:")) 3 l = [1,2] # 或者"12",序列即可 4 m = 0 # 组合数 5 for i in range(1,n+1): 6 for j in itertools.product(l,repeat = i): 7 asum = 0 8 for k in j: 9 asum += k # 累加步数 10 if asum == n: # 判断条件 步数等于台阶数 11 m += 1 # 组合数加1 12 print("总的组合数:{}".format(m)) 13
bash:
kali@Deepin:~$ python3 demo.py 输入台阶数:1 总的组合数:1 kali@Deepin:~$ python3 demo.py 输入台阶数:2 总的组合数:2 kali@Deepin:~$ python3 demo.py 输入台阶数:3 总的组合数:3 kali@Deepin:~$ python3 demo.py 输入台阶数:4 总的组合数:5 kali@Deepin:~$ python3 demo.py 输入台阶数:5 总的组合数:8 kali@Deepin:~$ python3 demo.py 输入台阶数:6 总的组合数:13 kali@Deepin:~$ python3 demo.py 输入台阶数:7 总的组合数:21 kali@Deepin:~$
功能倒是实现了, 但是效率确实不咋地, 台阶数上20就得花点时间了。
import requests import itertools import json import threading import time class IDCMinterface(): def getassetsrecordpagelist(self,p): url='' header = {'Content-Type': 'application/json; charset=UTF-8', 'Accept': 'application/json;text/plain', 'Accept-Encoding':'gzip', 'Connection':'keep-alive', 'Content-Length':'400'} #arg={"AssetsRecordType":'',"AssetsID":'',"StartDate":'',"EndDate":'',"BillNO":'',"PageSize":10, "PageIndex":1, "clientType":"undefined"} dat=requests.post(url,headers=header,data=p) dat=dat.json() #print(dat['status']) if dat['status']!=105: print(dat) print(p) if __name__=='__main__': #Requestss().gettoken() num=0 A = [{"serial_number":num,"phrase":"junior"},{"serial_number":num,"phrase":"onion"},{"serial_number":num,"phrase":"detail"}] B = [{"serial_number": num, "phrase": "emerge"}, {"serial_number": num, "phrase": "weapon"}, {"serial_number": num, "phrase": "robust"}] C = [{"serial_number": num, "phrase": "isolate"}, {"serial_number": num, "phrase": "volcano"}, {"serial_number": num, "phrase": "rally"}] D = [{"serial_number": num, "phrase": "private"}, {"serial_number": num, "phrase": "detect"}, {"serial_number": num, "phrase": "become"}] hw=[A,B,C,D] helpword = ['license', 'agent','close', 'wine', 'field', 'wide', 'steak', 'tell', 'text', 'deal', 'pole','purity'] print(time.strftime("%Y-%m-%d %H:%M:%S")) p=0 for i in itertools.permutations(hw,4): # i=str(i) # print(i) # i=i.replace('], [',',').replace("([",'[').replace("])",']').replace('}, {','}, ,{').replace('[','').replace(']','') # print(type(i),i) # i=i.split(', ,') # print(type(i),i) num=0 word=[] for m in range(0,4): for n in range(1,4): num=num+1 #print(i[m][n-1]['serial_number']) #print(i[0]) i[m][n-1]['serial_number']=num word.append(i[m][n-1]) #print(i) #print(help) i=list(word) i=json.dumps(i) #或者post方法的data直接改为json #print(i[0]) p = p + 1 IDCMinterface().getassetsrecordpagelist(i)