Guest User

Untitled

a guest
Feb 24th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. # coding: utf-8
  2.  
  3. class Line:
  4. def __init__(self, n):
  5. if n > 4:
  6. raise TypeError # 'it does not support the number greater than 4'
  7. self.n = n
  8. self.k = 2 ** (n - 1)
  9. self.default_k = self.k
  10. self.bottom_text = '{0}_'.format(self.k) * (self.k-1) + str(self.k)
  11. self.line_width = len(self.bottom_text)
  12. self.stock_texts = []
  13.  
  14. def stock(self):
  15. if self.k == 1:
  16. return False
  17.  
  18. current_text = ''
  19. self.k = 2 ** (self.n - 1)
  20. if self.k == self.default_k:
  21. current_text = self.bottom_text
  22. else:
  23. i = bottom_hit_cnt = last_hit_pos = 0
  24. insertion_pos = []
  25. for c in self.bottom_text:
  26. if c != '_':
  27. bottom_hit_cnt += 1
  28. if bottom_hit_cnt % 2 == 0:
  29. insertion_pos.append((last_hit_pos + i) / 2)
  30. last_hit_pos = i
  31. i += 1
  32. for i in range(self.line_width):
  33. current_text += str(self.k) if i in insertion_pos else '_'
  34.  
  35. self.stock_texts.append(current_text)
  36. self.n -= 1
  37. self.bottom_text = current_text
  38. return True
  39.  
  40. def write(self):
  41. for text in self.stock_texts[::-1]:
  42. print(text)
  43.  
  44. n = 3
  45. line = Line(n)
  46. while line.stock():
  47. pass
  48.  
  49. line.write()
Add Comment
Please, Sign In to add comment