Skip to content

Instantly share code, notes, and snippets.

@ciphergoth
Last active December 25, 2015 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ciphergoth/e35759d56f5c32f0f628 to your computer and use it in GitHub Desktop.
Save ciphergoth/e35759d56f5c32f0f628 to your computer and use it in GitHub Desktop.
How bad are things?
#!/usr/bin/python
# coding=utf8
import random
import re
def do_parse_rates(r):
rexp = re.compile(r'^– About ([\d.]+)% of people (.+)$')
for l in r.split('\n'):
m = rexp.match(l)
if m:
yield (float(m.group(1))/100, m.group(2))
def parse_rates(r):
return list(do_parse_rates(r))
rates_nonex = parse_rates("""
– About 2% of people have dementia
– About 20% of people have chronic pain
– About 7% of people have depression
– About 2% of people are cognitively disabled
– About 1% of people are schizophrenic
– About 1% of people are wheelchair-bound
– About 7% of people are alcoholic
– About 0.5% of people are chronic heroin users
– About 1% of people experience domestic violence
– About 10% of people were sexually abused as children
– About 20% of people were physically abused as children
– About 9% of people have been raped
""")
rates_ex = parse_rates("""
– About 1% of people are in prison
– About 2% of people are on probation
– About 1% of people are in nursing homes or hospices
– About 5% of people are unemployed
– About 3% of people are on disability
– About 20% of people are on food stamps
""")
def gen_person():
problems = []
x = random.random()
for r, p in rates_ex:
if x < r:
problems.append(p)
break
x -= r
for r, p in rates_nonex:
if random.random() < r:
problems.append(p)
return problems
def display_problems(problems):
if len(problems) == 0:
return "NP"
if len(problems) == 1:
return "They {0}".format(problems[0])
if len(problems) == 2:
return "They {0} and {1}".format(problems[0], problems[1])
return "They {0}, and {1}".format(", ".join(problems[:-1]), problems[-1])
for i in range(1, 21):
print "{0}: {1}".format(i, display_problems(gen_person()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment