Advertisement
Guest User

Table Compression Challenge

a guest
May 7th, 2014
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.34 KB | None | 0 0
  1. from itertools import product
  2. import string
  3. import random
  4. from sys import argv
  5.  
  6. cols = int(argv[1])        # number of columns
  7. chars = int(argv[2])       # average number of chars per column
  8. variation = int(argv[3])   # max +/- variation on # chars per column
  9. answer_rows = int(argv[4]) # number of answer-key rows
  10. complexity = int(argv[5])  # maximum complexity (length of any given group of characters in answer)
  11.  
  12. printables = [c for c in string.printable if c not in string.whitespace]
  13.  
  14. col_options = []
  15. for col in range(cols):
  16.     random.shuffle(printables)
  17.     nchars_this_column = chars + random.choice(range(-variation, variation+1))
  18.     col_options.append(printables[0:nchars_this_column])
  19.  
  20. # generate answer key, from which input will be created
  21. answer_key = []
  22. for r in range(answer_rows):
  23.     col_groups = [] # each of the compressed groups, e.g. ['AB', 'XY', 'I']
  24.     for col in range(cols):
  25.         size = min(random.randrange(1,complexity), len(col_options[col]))
  26.         random.shuffle(col_options[col])
  27.         col_groups.append(''.join(col_options[col][:size]))
  28.     answer_key.append(col_groups)
  29.  
  30. inputs = []
  31. for a in answer_key:
  32.     inputs.extend(product(*a))
  33. random.shuffle(inputs)
  34.  
  35. with open('answer.txt', 'w') as f:
  36.     for a in answer_key:
  37.         f.write(' '.join(a) + '\n')
  38.  
  39. with open('input.txt', 'w') as f:
  40.     for i in inputs:
  41.         f.write(' '.join(i) + '\n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement