This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| from pprint import pprint | |
| from random import randint | |
| from datetime import datetime | |
| from uuid import uuid4 | |
| import pandas as pd | |
| NUMS = 10000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| from timeit import timeit | |
| def naive(values, k): | |
| for i, v1 in enumerate(values): | |
| for v2 in values[i + 1:]: | |
| if v1 + v2 == k: | |
| return True | |
| return False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from collections import defaultdict | |
| def trie(): | |
| return defaultdict(trie) | |
| class Trie(object): | |
| TERMINATOR = "<END>" | |
| def __init__(self): | |
| self.t = trie() | |
| def insert_word(self, word): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import collections | |
| import itertools | |
| import random | |
| import sys | |
| import timeit | |
| pop = [ | |
| (b := random.randint(1900, 2000), random.randint(b + 1, b + 100)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # Validation of code in Mewdium article: | |
| # https://medium.com/the-modern-scientist/make-pandas-code-120x-faster-a-forbidden-mathematical-jutsu-87103030eb9c | |
| import timeit | |
| import pandas as pd | |
| import numpy as np | |
| sizes = [1_000, 10_000, 100_000, 1_000_000] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| from collections import deque | |
| def possible(pos, legal_pos): | |
| r, c = pos | |
| can = { | |
| (r + 2, c - 1), (r + 2, c + 1), | |
| (r + 1, c - 2), (r + 1, c + 2), | |
| (r - 1, c - 2), (r - 1, c + 2), | |
| (r - 2, c - 1), (r - 2, c + 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Sample output | |
| ~/twenty.py preventionthionalist | |
| process dictionary 0:00:00.057675 | |
| lookup tables 0:00:00.354845 | |
| search solutions 0:00:00.004037 | |
| [(304, 'trephinations', 'violent'), | |
| (304, 'interventional', 'pithos'), | |
| (304, 'interpolative', 'tonnish'), | |
| (292, 'trephination', 'violents'), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # O(n) alternative to: | |
| # https://elisabethirgens.github.io/notes/2021/12/step-away/ | |
| from collections import defaultdict | |
| mylist = [ | |
| {'thing': 'A', 'count': 4}, | |
| {'thing': 'B', 'count': 2}, | |
| {'thing': 'A', 'count': 6}, | |
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| AZ: (0.3804329085751007, 0.6195670914248993, 332493.15068493155, 541493.1506849315, -209000.00000000003) | |
| FL: (0.6779600278494492, 0.3220399721505508, 727637.3626373624, 345637.36263736244, 381999.99999999994) | |
| GA: (0.5798898071625344, 0.42011019283746565, 1219448.2758620693, 883448.2758620695, 335999.99999999977) | |
| IA: (0.43084455324357407, 0.5691554467564259, 352000.0, 465000.0, -112999.99999999997) | |
| MI: (0.5424654351109562, 0.45753456488904376, 1628720.9302325586, 1373720.9302325582, 255000.0000000003) | |
| NC: (0.6079855936413314, 0.3920144063586686, 208319.14893617015, 134319.14893617015, 74000.00000000003) | |
| OH: (0.6330845907837039, 0.36691540921629606, 711172.8395061726, 412172.8395061726, 299000.0) | |
| PA: (0.5496743418035043, 0.4503256581964957, 1997333.333333333, 1636333.333333333, 361000.0) | |
| TX: (0.5517922189231381, 0.4482077810768619, 2002944.444444445, 1626944.444444445, 375999.99999999994) | |
| WI: (0.5267461143892939, 0.47325388561070614, 718842.105263158, 645842.105263158, 73000.00000000007) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> def chunk(items, size): | |
| ... i, count = 0, len(items) | |
| ... for j in reversed(range(1, size + 1)): | |
| ... m, n = divmod(count, j) | |
| ... k = m + int(bool(n)) | |
| ... yield items[i:i + k] | |
| ... i += k | |
| ... count -= k | |
| ... | |
| >>> list(chunk(range(5), 5)) |