Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding: utf-8
- class Line:
- def __init__(self, n):
- if n > 4:
- raise TypeError # 'it does not support the number greater than 4'
- self.n = n
- self.k = 2 ** (n - 1)
- self.default_k = self.k
- self.bottom_text = '{0}_'.format(self.k) * (self.k-1) + str(self.k)
- self.line_width = len(self.bottom_text)
- self.stock_texts = []
- def stock(self):
- if self.k == 1:
- return False
- current_text = ''
- self.k = 2 ** (self.n - 1)
- if self.k == self.default_k:
- current_text = self.bottom_text
- else:
- i = bottom_hit_cnt = last_hit_pos = 0
- insertion_pos = []
- for c in self.bottom_text:
- if c != '_':
- bottom_hit_cnt += 1
- if bottom_hit_cnt % 2 == 0:
- insertion_pos.append((last_hit_pos + i) / 2)
- last_hit_pos = i
- i += 1
- for i in range(self.line_width):
- current_text += str(self.k) if i in insertion_pos else '_'
- self.stock_texts.append(current_text)
- self.n -= 1
- self.bottom_text = current_text
- return True
- def write(self):
- for text in self.stock_texts[::-1]:
- print(text)
- n = 3
- line = Line(n)
- while line.stock():
- pass
- line.write()
Add Comment
Please, Sign In to add comment