Advertisement
Guest User

Simulator of voting in the Council of the European Union, v1

a guest
Dec 21st, 2012
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.04 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import sys
  4.  
  5. states = [
  6.         ("Austria", 10, 1.67),
  7.         ("Belgium", 12, 2.17),
  8.         ("Bulgaria", 10, 1.49),
  9.         ("Cyprus", 4, 0.16),
  10.         ("Czech Republic", 12, 2.10),
  11.         ("Denmark", 7, 1.11),
  12.         ("Estonia", 4, 0.27),
  13.         ("Finland", 7, 1.07),
  14.         ("France", 29, 12.95),
  15.         ("Germany", 29, 16.27),
  16.         ("Greece", 12, 2.25),
  17.         ("Hungary", 12, 1.99),
  18.         ("Ireland", 7, 1.07),
  19.         ("Italy", 29, 12.07),
  20.         ("Latvia", 4, 0.44),
  21.         ("Lithuania", 7, 0.65),
  22.         ("Luxembourg", 4, 0.10),
  23.         ("Malta", 3, 0.08),
  24.         ("Netherlands", 13, 3.31),
  25.         ("Poland", 27, 7.6),
  26.         ("Portugal", 12, 2.12),
  27.         ("Romania", 14, 4.26),
  28.         ("Slovakia", 7, 1.08),
  29.         ("Slovenia", 4, 0.41),
  30.         ("Spain", 27, 9.18),
  31.         ("Sweden", 10, 1.87),
  32.         ("United Kingdom", 29, 12.43)
  33.     ]
  34.  
  35. def count_votes(votes):
  36.     v = 0;
  37.     p = 0;
  38.    
  39.     i = 0;
  40.     while i < 27:
  41.         if votes & 1:
  42.             v += states[i][1]
  43.             p += states[i][2]
  44.         votes >>= 1
  45.         i += 1;
  46.    
  47.     return v, p
  48.  
  49. def interest(state, votes):
  50.     return ((1 << states.index(state)) & votes) != 0
  51.  
  52. def nice(states, votes):
  53.     return states >= 14 and votes >= 255
  54.  
  55. def nicepop(states, votes, pop):
  56.     return nice(states, votes) and pop >= 62
  57.  
  58. def lisbon(states, pop, blocking):
  59.     return (blocking < 4) or (states >= 15 and pop >= 65)
  60.  
  61. def vote(votes):
  62.     states = bin(votes).count("1");
  63.     v, p = count_votes(votes)
  64.    
  65.     return nice(states, v), nicepop(states, v, p), lisbon(states, p, 27 - states)
  66.  
  67. def round(t):
  68.     print(t[0])
  69.     votes = 0;
  70.    
  71.     nice_win = 0;
  72.     nicepop_win = 0;
  73.     lisbon_win = 0;
  74.    
  75.     while votes <= 134217727:
  76.         nice_r, nicepop_r, lisbon_r = vote(votes)
  77.         intr = interest(t, votes)
  78.         nice_win += (nice_r == intr)
  79.         nicepop_win += (nicepop_r == intr)
  80.         lisbon_win += (lisbon_r == intr)
  81.         votes += 1
  82.    
  83.     print("-----------------------")
  84.     print("Nice wins: ", nice_win)
  85.     print("Nice with population criterion wins: ", nicepop_win)
  86.     print("Lisbon wins: ", lisbon_win)
  87.     print("-----------------------")
  88.     print()
  89.  
  90. def main():
  91.     if (len(sys.argv) == 2):
  92.         round(states[int(sys.argv[1])])
  93.     else:
  94.         for t in states:
  95.             round(t)
  96.  
  97. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement