Skip to content

Instantly share code, notes, and snippets.

@XertroV

XertroV/benfords-florida.py Secret

Created Nov 13, 2020
Embed
What would you like to do?
import json
from collections import Counter
def loads_and_parse(nums):
parse_f = lambda s : int(s.replace(',', ''))
return list(map(parse_f, json.loads(nums)))
# data ripped from: https://www.nbcnews.com/politics/2020-elections/florida-president-results
vote_nums = dict(
trump = loads_and_parse("[\"50,891\",\"11,911\",\"66,063\",\"10,334\",\"207,857\",\"333,346\",\"5,274\",\"73,202\",\"65,348\",\"84,470\",\"128,936\",\"23,835\",\"532,734\",\"8,313\",\"6,758\",\"233,687\",\"96,655\",\"43,039\",\"4,675\",\"7,465\",\"7,895\",\"3,781\",\"6,113\",\"3,815\",\"6,122\",\"7,904\",\"70,321\",\"34,846\",\"327,306\",\"8,079\",\"58,870\",\"15,488\",\"4,478\",\"3,128\",\"125,853\",\"233,242\",\"57,446\",\"16,746\",\"2,846\",\"5,576\",\"124,966\",\"127,825\",\"61,152\",\"25,688\",\"42,563\",\"79,771\",\"11,470\",\"245,325\",\"73,467\",\"334,642\",\"179,545\",\"276,191\",\"194,571\",\"25,514\",\"110,886\",\"86,825\",\"77,349\",\"148,323\",\"125,217\",\"62,757\",\"16,410\",\"7,748\",\"5,133\",\"173,797\",\"12,873\",\"32,942\",\"9,876\"]"),
biden = loads_and_parse("[\"89,527\",\"2,037\",\"25,572\",\"3,160\",\"148,479\",\"618,597\",\"1,209\",\"42,240\",\"27,088\",\"38,294\",\"77,595\",\"8,912\",\"617,647\",\"4,259\",\"1,364\",\"252,450\",\"70,893\",\"28,148\",\"2,118\",\"16,152\",\"1,700\",\"1,384\",\"1,985\",\"1,963\",\"2,298\",\"4,929\",\"37,446\",\"16,917\",\"376,196\",\"924\",\"37,843\",\"6,766\",\"3,895\",\"509\",\"83,491\",\"157,672\",\"103,492\",\"6,203\",\"693\",\"3,747\",\"90,127\",\"74,848\",\"36,878\",\"21,864\",\"15,561\",\"34,227\",\"4,389\",\"394,857\",\"97,278\",\"433,392\",\"118,918\",\"277,397\",\"145,022\",\"10,524\",\"63,808\",\"84,115\",\"27,582\",\"120,050\",\"132,486\",\"29,331\",\"4,485\",\"2,299\",\"1,053\",\"130,451\",\"5,348\",\"10,336\",\"2,347\"]"))
def first_digits_kvs(kvs):
k, vs = kvs
print(k , vs)
return (k, Counter(list(map(lambda v: int(str(v)[0]), vs))))
pairs = dict(map(first_digits_kvs, vote_nums.items()))
print(dict(pairs))
# from https://realpython.com/python-histograms/
def ascii_histogram(seq) -> None:
"""A horizontal frequency-table/histogram plot."""
for k in sorted(seq):
print('{0:5d} {1}'.format(k, '+' * seq[k]))
def heading(h):
print(f"\n\n{h} histogram")
def print_graph(h, seq):
heading(h)
ascii_histogram(seq)
for k, v in pairs.items():
print_graph(k, v)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.