Advertisement
_eremec_

Untitled

Nov 2nd, 2017
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.68 KB | None | 0 0
  1. from copy import deepcopy
  2.  
  3.  
  4. def partition(coll, n):
  5.     p = []
  6.     for i in range(0, len(coll), n):
  7.         p.append(coll[i:i+n])
  8.     return p
  9.  
  10. def gen_coords(side):
  11.     h = int(side / 2)
  12.     s = [cell for cell in range(-1 * h, h + 1) if cell != 0]
  13.     return [(y, x) for x in s for y in s]
  14.  
  15. def make_square(side, letters):
  16.     ks = gen_coords(side)
  17.     return {k : {'letter' : l, 'marked' : False}
  18.             for k, l in zip(ks, letters)}
  19.  
  20. def turn(clockwise, coords):
  21.     y = coords[0]
  22.     x = coords[1]
  23.     return (-x, y) if clockwise else (x, -y)
  24.  
  25. def turn_lattice(cwise, s):
  26.     new_s = deepcopy(s)
  27.     for k in s.keys():
  28.         new_k = turn(cwise, k)
  29.         new_s[new_k]['marked'] = s[k]['marked']
  30.     return new_s
  31.  
  32. def cell2str(v):
  33.     return '▓' if v['marked'] else str(v['letter'])
  34.  
  35. def square2str(s, side):
  36.     symbs = [cell2str(v) for v in s.values()]    
  37.     matrix = reversed(partition(symbs, side))
  38.     return '\n'.join([' '.join(line) for line in matrix])
  39.  
  40. def handle_input():
  41.     uinp = input().split(',')
  42.     return uinp[0] if len(uinp) == 1 else (int(uinp[0]), int(uinp[1]))
  43.  
  44. def invert_mark(s, coords):
  45.     new_s = deepcopy(s)
  46.     new_s[coords]['marked'] = not new_s[coords]['marked']
  47.     return new_s
  48.  
  49. def main(letters, side):
  50.     square = (make_square(side, letters))
  51.     while True:
  52.         u = handle_input()
  53.         if u == 't':
  54.             square = turn_lattice(True, square)
  55.         elif u == 'T':
  56.             square = turn_lattice(False, square)
  57.         else:
  58.             square = invert_mark(square, u)
  59.         print(square2str(square, side))
  60.  
  61. letters = sum([list(range(10)) for _ in range(10)], [])
  62. main(letters, 10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement