mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
reorganize dir
Signed-off-by: Mia Steinkirch <mia.steinkirch@gmail.com>
This commit is contained in:
parent
1b6f705e7c
commit
a8e71c50db
276 changed files with 23954 additions and 0 deletions
0
ebook_src/USEFUL/dynamic_programming/__init__.py
Normal file
0
ebook_src/USEFUL/dynamic_programming/__init__.py
Normal file
42
ebook_src/USEFUL/dynamic_programming/memo.py
Normal file
42
ebook_src/USEFUL/dynamic_programming/memo.py
Normal file
|
@ -0,0 +1,42 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
from functools import wraps
|
||||
from do_benchmark import benchmark
|
||||
|
||||
def memo(func):
|
||||
''' an example of dynamic programming using a memoizing decorator '''
|
||||
cache = {}
|
||||
@wraps(func)
|
||||
def wrap(*args):
|
||||
if args not in cache:
|
||||
cache[args] = func(*args)
|
||||
return cache[args]
|
||||
return wrap
|
||||
|
||||
@memo
|
||||
def find_fibonacci_seq_rec(n):
|
||||
''' implements the nth fibonacci value in a recursive exponential runtime '''
|
||||
if n < 2: return n
|
||||
return find_fibonacci_seq_rec(n - 1) + find_fibonacci_seq_rec(n - 2)
|
||||
|
||||
def test_memo():
|
||||
n = 50
|
||||
# find_fibonacci_seq_rec = memo(find_fibonacci_seq_rec)
|
||||
# @benchmark
|
||||
print(find_fibonacci_seq_rec(n))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_memo()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,85 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
from itertools import combinations
|
||||
from bisect import bisect
|
||||
from memo import memo
|
||||
from do_benchmark import benchmark
|
||||
|
||||
def naive_longest_inc_subseq(seq):
|
||||
''' naive (exponential) solution to the longest increasing subsequence problem '''
|
||||
for length in range(len(seq), 0, -1):
|
||||
for sub in combinations(seq, length):
|
||||
if list(sub) == sorted(sub):
|
||||
return len(sub)
|
||||
|
||||
|
||||
def longest_inc_subseq1(seq):
|
||||
''' an iterative algorithm for the longest increasing subsequence problem '''
|
||||
end = []
|
||||
for val in seq:
|
||||
idx = bisect(end, val)
|
||||
if idx == len(end): end.append(val)
|
||||
else: end[idx] = val
|
||||
return len(end)
|
||||
|
||||
|
||||
def longest_inc_subseq2(seq):
|
||||
''' another iterative algorithm for the longest increasing subsequence problem '''
|
||||
L = [1] * len(seq)
|
||||
for cur, val in enumerate(seq):
|
||||
for pre in range(cur):
|
||||
if seq[pre] <= val:
|
||||
L[cur] = max(L[cur], 1 + L[pre])
|
||||
return max(L)
|
||||
|
||||
|
||||
def memoized_longest_inc_subseq(seq):
|
||||
''' a memoized recursive solution to find the longest increasing subsequence problem '''
|
||||
@memo
|
||||
def L(cur):
|
||||
res = 1
|
||||
for pre in range(cur):
|
||||
if seq[pre] <= seq[cur]:
|
||||
res = max(res, 1 + L(pre))
|
||||
return res
|
||||
return max(L(i) for i in range(len(seq)))
|
||||
|
||||
|
||||
@benchmark
|
||||
def test_naive_longest_inc_subseq():
|
||||
print(naive_longest_inc_subseq(s1))
|
||||
|
||||
benchmark
|
||||
def test_longest_inc_subseq1():
|
||||
print(longest_inc_subseq1(s1))
|
||||
|
||||
@benchmark
|
||||
def test_longest_inc_subseq2():
|
||||
print(longest_inc_subseq2(s1))
|
||||
|
||||
@benchmark
|
||||
def test_memoized_longest_inc_subseq():
|
||||
print(memoized_longest_inc_subseq(s1))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from random import randrange
|
||||
s1 = [randrange(100) for i in range(25)]
|
||||
print(s1)
|
||||
test_naive_longest_inc_subseq()
|
||||
test_longest_inc_subseq1()
|
||||
test_longest_inc_subseq2()
|
||||
test_memoized_longest_inc_subseq()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue