Update permute and add dft.py

This commit is contained in:
mbinary 2020-01-01 15:51:32 +08:00
parent e20afa6485
commit 36ebaecf18
5 changed files with 26 additions and 18 deletions

View File

@ -74,8 +74,8 @@ Some pictures and ideas are from `<<Introduction to Algotithm>>`
* [isBipartGraph.py](./graph/isBipartGraph.py)
* [math](./math)
* [README.md](./math/README.md)
* [dft.py](./math/dft.py)
* [fastPow.py](./math/fastPow.py)
* [fft.py](./math/fft.py)
* [fibonacci](./math/fibonacci)
* [numWeight](./math/numWeight)
* [numberTheory](./math/numberTheory)

View File

@ -9,15 +9,19 @@
# Description:
#########################################################################
'''
def permute(n):
def _util(lst,i):
if i==n:print(lst)
else:
for j in range(i,n):
lst[i],lst[j]=lst[j],lst[i]
_util(lst,i+1)
lst[i],lst[j]=lst[j],lst[i]
_util([i for i in range(n)],0)
if __name__=='__main__':
def permute(n):
def _util(lst, i):
if i == n:
print(lst)
else:
for j in range(i, n):
lst[i], lst[j] = lst[j], lst[i]
_util(lst, i+1)
lst[i], lst[j] = lst[j], lst[i]
_util([i for i in range(n)], 0)
if __name__ == '__main__':
permute(5)

View File

@ -9,15 +9,19 @@
# Description:
#########################################################################
'''
def permute(lst,n):
def permute(lst, n):
''' O(n!), optimal'''
if n==1:print(lst)
if n == 1:
print(lst)
else:
for i in range(n):
lst[i],lst[n-1] = lst[n-1],lst[i]
permute(lst,n-1)
lst[i],lst[n-1] = lst[n-1],lst[i]
lst[i], lst[n-1] = lst[n-1], lst[i]
permute(lst, n-1)
lst[i], lst[n-1] = lst[n-1], lst[i]
if __name__=='__main__':
if __name__ == '__main__':
n = 3
permute([i for i in range(n)],n)
permute([i for i in range(n)], n)

0
poly.c Normal file
View File