Skip to content

Instantly share code, notes, and snippets.

@fabiogaluppo
Last active July 20, 2018 01:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabiogaluppo/66d0563227c9bcb9915233ee438b31c3 to your computer and use it in GitHub Desktop.
Save fabiogaluppo/66d0563227c9bcb9915233ee438b31c3 to your computer and use it in GitHub Desktop.
Built-in Types in Python (Elementary Data Structures)
# http://bit.ly/py-eds-1
import hashlib
"""
>>> cut ("HelloWorld", 5)
('Hello', 'World')
"""
def cut(msg, cut_point):
return (msg[:cut_point], msg[cut_point:])
"""
>>> hash_cut ("HelloWorld", 5)
(('Hello', b'185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969'), ('World', b'78ae647dc5544d227130a0682a51e30bc7777fbb6d8a8f17007463a3ecd1d524'))
"""
#def hash_cut(msg, cut_point):
# left, right = cut(msg, cut_point)
# return (left, hashlib.sha256(left.encode()).hexdigest().encode()), (right, hashlib.sha256(right.encode()).hexdigest().encode())
def hash_cut(msg, cut_point):
def make_pair(s):
return (s, hashlib.sha256(s.encode()).hexdigest().encode())
left, right = cut(msg, cut_point)
return make_pair(left), make_pair(right)
"""
>>> hash_cut_list ("HelloWorld", 5)
['Hello', '185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969', 'World', '78ae647dc5544d227130a0682a51e30bc7777fbb6d8a8f17007463a3ecd1d524']
"""
def hash_cut_list(msg, cut_point):
(a, b), (c, d) = hash_cut(msg, cut_point)
return [a, b.decode(), c, d.decode()]
"""
>>> hash_cut_list ("HelloWorld", 5) + hash_cut_list("ABCDEFG", 3)
['Hello', '185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969', 'World', '78ae647dc5544d227130a0682a51e30bc7777fbb6d8a8f17007463a3ecd1d524', 'ABC', 'b5d4045c3f466fa91fe2cc6abe79232a1a57cdf104f7a26e716e0a1e2789df78', 'DEFG', '52e77c7a73727a254b68f0aab2cac2926d685f8b4f49ff795ca72c5c93480a2c']
>>> lst = hash_cut_list ("HelloWorld", 5) + hash_cut_list("ABCDEFG", 3)
>>> size_of_items (lst)
[5, 64, 5, 64, 3, 64, 4, 64]
>>> set(size_of_items (lst))
{64, 3, 4, 5}
>>> frozenset(size_of_items (lst))
frozenset({64, 3, 4, 5})
>>> s1 = set(size_of_items (lst))
>>> s1.add(10)
>>> s1
{64, 3, 4, 5, 10}
>>> s1.remove(10)
>>> s1
{64, 3, 4, 5}
>>> s2 = frozenset(size_of_items (lst))
>>> s2.add(10)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'frozenset' object has no attribute 'add'
>>> s1 == s2
True
>>> s1.add(99)
>>> s1.intersection(s2)
{64, 3, 4, 5}
>>> s1.difference(s2)
{99}
>>> s1.union({4, 5, 6, 7})
{64, 3, 4, 5, 99, 6, 7}
"""
def size_of_items(lst):
return [len(item) for item in lst]
titles = ['Algorithms in a Nutshell: A Practical Guide 2nd Edition',
'Understanding Machine Learning: From Theory to Algorithms 1st Edition',
'Guide to NumPy: 2nd Edition',
'Networks: An Introduction 1st Edition',
'Data Science from Scratch: First Principles with Python 1st Edition',
'How to Count: An Introduction to Combinatorics and Its Applications 2015th Edition']
isbns = ['1491948922', '1107057132', '151730007X', '0199206651', '149190142X', '331913843X']
"""
>>> list(zip(titles, isbns))
[('Algorithms in a Nutshell: A Practical Guide 2nd Edition', '1491948922'), ('Understanding Machine Learning: From Theory to Algorithms 1st Edition', '1107057132'), ('Guide to NumPy: 2nd Edition', '151730007X'), ('Networks: An Introduction 1st Edition', '0199206651'), ('Data Science from Scratch: First Principles with Python 1st Edition', '149190142X'), ('How to Count: An Introduction to Combinatorics and Its Applications 2015th Edition', '331913843X')]
>>> dict(zip(titles, isbns))
{'Algorithms in a Nutshell: A Practical Guide 2nd Edition': '1491948922', 'Understanding Machine Learning: From Theory to Algorithms 1st Edition': '1107057132', 'Guide to NumPy: 2nd Edition': '151730007X', 'Networks: An Introduction 1st Edition': '0199206651', 'Data Science from Scratch: First Principles with Python 1st Edition': '149190142X', 'How to Count: An Introduction to Combinatorics and Its Applications 2015th Edition': '331913843X'}
>>> d1 = dict(zip(isbns, titles))
>>> d1['149190142X']
'Data Science from Scratch: First Principles with Python 1st Edition'
>>> d1.keys()
dict_keys(['1491948922', '1107057132', '151730007X', '0199206651', '149190142X', '331913843X'])
>>> d1.values()
dict_values(['Algorithms in a Nutshell: A Practical Guide 2nd Edition', 'Understanding Machine Learning: From Theory to Algorithms 1st Edition', 'Guide to NumPy: 2nd Edition', 'Networks: An Introduction 1st Edition', 'Data Science from Scratch: First Principles with Python 1st Edition', 'How to Count: An Introduction to Combinatorics and Its Applications 2015th Edition'])
"""
prices = [46.99, 45.99, 47.45, 57.70, 27.30, 36.93]
"""
>>> d2 = dict(zip(isbns, prices))
>>> d1['149190142X']
'Data Science from Scratch: First Principles with Python 1st Edition'
>>> d2['149190142X']
27.3
>>> d3 = dict(zip(isbns, zip(titles, prices)))
>>> d3['149190142X']
('Data Science from Scratch: First Principles with Python 1st Edition', 27.3)
>>> d3.items()
dict_items([('1491948922', ('Algorithms in a Nutshell: A Practical Guide 2nd Edition', 46.99)), ('1107057132', ('Understanding Machine Learning: From Theory to Algorithms 1st Edition', 45.99)), ('151730007X', ('Guide to NumPy: 2nd Edition', 47.45)), ('0199206651', ('Networks: An Introduction 1st Edition', 57.7)), ('149190142X', ('Data Science from Scratch: First Principles with Python 1st Edition', 27.3)), ('331913843X', ('How to Count: An Introduction to Combinatorics and Its Applications 2015th Edition', 36.93))])
>>> list(d3.items())
[('1491948922', ('Algorithms in a Nutshell: A Practical Guide 2nd Edition', 46.99)), ('1107057132', ('Understanding Machine Learning: From Theory to Algorithms 1st Edition', 45.99)), ('151730007X', ('Guide to NumPy: 2nd Edition', 47.45)), ('0199206651', ('Networks: An Introduction 1st Edition', 57.7)), ('149190142X', ('Data Science from Scratch: First Principles with Python 1st Edition', 27.3)), ('331913843X', ('How to Count: An Introduction to Combinatorics and Its Applications 2015th Edition', 36.93))]
>>> lst = list(d3.items())
>>> lst.sort()
>>> lst
[('0199206651', ('Networks: An Introduction 1st Edition', 57.7)), ('1107057132', ('Understanding Machine Learning: From Theory to Algorithms 1st Edition', 45.99)), ('149190142X', ('Data Science from Scratch: First Principles with Python 1st Edition', 27.3)), ('1491948922', ('Algorithms in a Nutshell: A Practical Guide 2nd Edition', 46.99)), ('151730007X', ('Guide to NumPy: 2nd Edition', 47.45)), ('331913843X', ('How to Count: An Introduction to Combinatorics and Its Applications 2015th Edition', 36.93))]
"""
def by_price (book):
isbn, (title, price) = book
return price
"""
>>> by_price (lst[0])
57.7
>>> lst.sort(key = by_price)
>>> lst
[('149190142X', ('Data Science from Scratch: First Principles with Python 1st Edition', 27.3)), ('331913843X', ('How to Count: An Introduction to Combinatorics and Its Applications 2015th Edition', 36.93)), ('1107057132', ('Understanding Machine Learning: From Theory to Algorithms 1st Edition', 45.99)), ('1491948922', ('Algorithms in a Nutshell: A Practical Guide 2nd Edition', 46.99)), ('151730007X', ('Guide to NumPy: 2nd Edition', 47.45)), ('0199206651', ('Networks: An Introduction 1st Edition', 57.7))]
>>> lst.sort(key = by_price, reverse = True)
>>> lst
[('0199206651', ('Networks: An Introduction 1st Edition', 57.7)), ('151730007X', ('Guide to NumPy: 2nd Edition', 47.45)), ('1491948922', ('Algorithms in a Nutshell: A Practical Guide 2nd Edition', 46.99)), ('1107057132', ('Understanding Machine Learning: From Theory to Algorithms 1st Edition', 45.99)), ('331913843X', ('How to Count: An Introduction to Combinatorics and Its Applications 2015th Edition', 36.93)), ('149190142X', ('Data Science from Scratch: First Principles with Python 1st Edition', 27.3))]
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment