1. Roger Pate
  2. nonsense

Commits

Roger Pate  committed f4e9475

praxis: Solve "House of Representatives".

  • Participants
  • Parent commits 5ff6d67
  • Branches default

Comments (0)

Files changed (1)

File praxis/housereps.py

View file
+#!/usr/bin/env python2.6
+"""House of Representatives
+
+- http://programmingpraxis.com/2011/04/12/house-of-representatives/
+"""
+
+
+import math
+import optparse
+import sys
+
+
+pops = {  # 2010 populations
+  "Alabama": 4779736,
+  "Alaska": 710231,
+  "Arizona": 6392017,
+  "Arkansas": 2915918,
+  "California": 37253956,
+  "Colorado": 5029196,
+  "Connecticut": 3574097,
+  "Delaware": 897934,
+  "Florida": 18801310,
+  "Georgia": 9687653,
+  "Hawaii": 1360301,
+  "Idaho": 1567582,
+  "Illinois": 12830632,
+  "Indiana": 6483802,
+  "Iowa": 3046355,
+  "Kansas": 2853118,
+  "Kentucky": 4339367,
+  "Louisiana": 4533372,
+  "Maine": 1328361,
+  "Maryland": 5773552,
+  "Massachusetts": 6547629,
+  "Michigan": 9883640,
+  "Minnesota": 5303925,
+  "Mississippi": 2967297,
+  "Missouri": 5988927,
+  "Montana": 989415,
+  "Nebraska": 1826341,
+  "Nevada": 2700551,
+  "New Hampshire": 1316470,
+  "New Jersey": 8791894,
+  "New Mexico": 2059179,
+  "New York": 19378102,
+  "North Carolina": 9535483,
+  "North Dakota": 672591,
+  "Ohio": 11536504,
+  "Oklahoma": 3751351,
+  "Oregon": 3831074,
+  "Pennsylvania": 12702379,
+  "Rhode Island": 1052567,
+  "South Carolina": 4625364,
+  "South Dakota": 814180,
+  "Tennessee": 6346105,
+  "Texas": 25145561,
+  "Utah": 2763885,
+  "Vermont": 625741,
+  "Virginia": 8001024,
+  "Washington": 6724540,
+  "West Virginia": 1852994,
+  "Wisconsin": 5686986,
+  "Wyoming": 563626,
+  }
+
+
+def main(args):
+  op = optparse.OptionParser(usage="%prog [OPTION..]")
+  op.add_option("-a", dest="alpha", action="store_true", default=False,
+    help="Print states alphabetically")
+  opts, args = op.parse_args(args)
+  if args:
+    op.error("unexpected arguments")
+
+  reps = dict((k, 1) for k in pops)
+
+  def g(state):
+    n = reps[state]
+    return pops[state] / math.sqrt(n * (n + 1))
+
+  for _ in xrange(435 - len(reps)):
+    reps[max(pops, key=g)] += 1
+  assert sum(reps.values()) == 435
+
+  if opts.alpha:
+    key = None
+  else:
+    def key((state, reps)):
+      return -reps, state
+  for row in sorted(reps.items(), key=key):
+    print "%-14s  %2d" % row
+
+
+if __name__ == "__main__":
+  sys.exit(main(sys.argv[1:]))