Advertisement
Guest User

Untitled

a guest
May 20th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.52 KB | None | 0 0
  1. def eca(cells, rule):
  2.     lencells = len(cells)
  3.     c = "0" + cells + "0"    # Zero pad the ends
  4.     rulebits = '{0:08b}'.format(rule)
  5.     neighbours2next = {'{0:03b}'.format(n):rulebits[::-1][n] for n in range(8)}
  6.     yield c[1:-1]
  7.     while True:
  8.         c = ''.join(['0',
  9.                      ''.join(neighbours2next[c[i-1:i+2]]
  10.                              for i in range(1,lencells+1)),
  11.                      '0'])
  12.         yield c[1:-1]
  13.  
  14. if __name__ == '__main__':
  15.  
  16.     print("Генератор элементарных клеточных автоматов v0.3")
  17.     #print("Привет!")
  18.  
  19.     a=-1;
  20.     outputType=2
  21.  
  22.  
  23. #Проверка на корректность ввода 2
  24.     while True:
  25.         print("Введите начальные условия в виде 0 и 1.")
  26.         a = str(input())
  27.         correct = 1
  28.         for i in range(0,len(a)):
  29.             if a[i] != '1' and a[i]!='0':
  30.                 correct=0
  31.         if correct==0 or len(a) ==0:
  32.             print("Ошибка, повторите ввод.")
  33.         else:
  34.             break
  35.  
  36.  
  37.  
  38.    # print("Введите номер правила в целочисленном виде")
  39.     rule = 60
  40.     n=100
  41.     i1 = '0'
  42.     i2 = '1'
  43.  
  44.     lines, start, rules = n, a, rule
  45.     zipped = [range(lines)] + [eca(start, rule)]
  46.     print('\n   Правило: %r' % (rules,))
  47.     for data in zip(*zipped):
  48.         i = data[0]
  49.         cells = data[1:]
  50.         print('%s' % ('    '.join(cells).replace('0',i1).replace('1', i2)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement