mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
cleaning up and organizing old problems (builtin)
This commit is contained in:
parent
6afe96fa4d
commit
3fdbc2a605
106 changed files with 480 additions and 1472 deletions
18
src/USEFUL/advanced/lru_cache.py
Normal file
18
src/USEFUL/advanced/lru_cache.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
from functools import lru_cache
|
||||
|
||||
|
||||
@lru_cache(maxsize=20)
|
||||
def fib(n):
|
||||
if n < 2:
|
||||
return n
|
||||
return fib(n-1) + fib(n-2)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print([fib(n) for n in range(10)])
|
||||
print(fib.cache_info())
|
31
src/USEFUL/basic_examples/example_OrderedDict.py
Normal file
31
src/USEFUL/basic_examples/example_OrderedDict.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
from collections import OrderedDict
|
||||
|
||||
def OrderedDict_example():
|
||||
''' show some examples for OrderedDict '''
|
||||
''' keep the order of insertion.
|
||||
maintains a doubly linked list, so size is more than twice than normal dict'''
|
||||
|
||||
|
||||
pairs = [('a', 1), ('b',2), ('c',3)]
|
||||
|
||||
d1 = {}
|
||||
for key, value in pairs:
|
||||
if key not in d1:
|
||||
d1[key] = []
|
||||
d1[key].append(value)
|
||||
for key in d1:
|
||||
print(key, d1[key])
|
||||
|
||||
d2 = OrderedDict(pairs)
|
||||
for key in d2:
|
||||
print(key, d2[key])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
OrderedDict_example()
|
||||
|
||||
|
32
src/USEFUL/basic_examples/example_counter.py
Normal file
32
src/USEFUL/basic_examples/example_counter.py
Normal file
|
@ -0,0 +1,32 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
from collections import Counter
|
||||
|
||||
def Counter_example():
|
||||
''' it is a dictionary that maps the items to the number of occurrences '''
|
||||
seq1 = [1, 2, 3, 5, 1, 2, 5, 5, 2, 5, 1, 4]
|
||||
seq_counts = Counter(seq1)
|
||||
print(seq_counts)
|
||||
|
||||
''' we can increment manually or use the update() method '''
|
||||
seq2 = [1, 2, 3]
|
||||
seq_counts.update(seq2)
|
||||
print(seq_counts)
|
||||
|
||||
seq3 = [1, 4, 3]
|
||||
for key in seq3:
|
||||
seq_counts[key] += 1
|
||||
print(seq_counts)
|
||||
|
||||
''' also, we can use set operations such as a-b or a+b '''
|
||||
seq_counts_2 = Counter(seq3)
|
||||
print(seq_counts_2)
|
||||
print(seq_counts + seq_counts_2)
|
||||
print(seq_counts - seq_counts_2)
|
||||
|
||||
if __name__ == '__main__':
|
||||
Counter_example()
|
||||
|
||||
|
27
src/USEFUL/basic_examples/example_defaultdict.py
Normal file
27
src/USEFUL/basic_examples/example_defaultdict.py
Normal file
|
@ -0,0 +1,27 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
from collections import defaultdict
|
||||
|
||||
def defaultdict_example():
|
||||
''' show some examples for defaultdicts '''
|
||||
pairs = {('a', 1), ('b',2), ('c',3)}
|
||||
|
||||
d1 = {}
|
||||
for key, value in pairs:
|
||||
if key not in d1:
|
||||
d1[key] = []
|
||||
d1[key].append(value)
|
||||
print(d1)
|
||||
|
||||
d2 = defaultdict(list)
|
||||
for key, value in pairs:
|
||||
d2[key].append(value)
|
||||
print(d2)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
defaultdict_example()
|
||||
|
||||
|
38
src/USEFUL/basic_examples/example_setdefault.py
Normal file
38
src/USEFUL/basic_examples/example_setdefault.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
def usual_dict(dict_data):
|
||||
newdata = {}
|
||||
for k, v in dict_data:
|
||||
if k in newdata:
|
||||
newdata[k].append(v)
|
||||
else:
|
||||
newdata[k] = [v]
|
||||
return newdata
|
||||
|
||||
|
||||
def setdefault_dict(dict_data):
|
||||
newdata = {}
|
||||
for k, v in dict_data:
|
||||
newdata.setdefault(k, []).append(v)
|
||||
return newdata
|
||||
|
||||
|
||||
def test_setdef(module_name='this module'):
|
||||
dict_data = (('key1', 'value1'),
|
||||
('key1', 'value2'),
|
||||
('key2', 'value3'),
|
||||
('key2', 'value4'),
|
||||
('key2', 'value5'),)
|
||||
print(usual_dict(dict_data))
|
||||
print(setdefault_dict(dict_data))
|
||||
|
||||
s = 'Tests in {name} have {con}!'
|
||||
print(s.format(name=module_name, con='passed'))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_setdef()
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue