Advertisement
Dantenerosas

NAMs

Apr 24th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.00 KB | None | 0 0
  1. def main():
  2.     # Входной алфавит
  3.     A = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
  4.     # Список левых частей правил
  5.     to_replace = ('*0', '*1', '*2', '*3', '*4', '*5', '*6', '*7', '*8', '*9',
  6.                   '9!', '8!', '7!', '6!', '5!', '4!', '3!', '2!', '1!', '0!',
  7.                   '9#', '8#', '7#', '6#', '5#', '4#', '3#', '2#', '1#', '0#',
  8.                   '!3', '!4', '!5', '!6', '!7', '!8', '!9',
  9.                   '_?0.', '?', '*', '#', '_.', '')
  10.     # Список правых частей правил
  11.     replacement = ('0*','1*','2*', '3*', '4*', '5*', '6*', '7*', '8*', '9*',
  12.                    '8', '7', '6', '5', '4', '3', '2', '1', '?0', '!9',
  13.                    '2','1','0','!9','!8','!7', '!6', '!5', '!4', '!3',
  14.                    '', '', '', '', '', '', '',
  15.                    '', '', '#', '', '', '_*')
  16.     # Кол-во правил
  17.     n = len(to_replace)
  18.     # Символ для определения конечных замен
  19.     end_symbol = '.'
  20.     print("A =", A)
  21.     print("Введите слово содержащее символы алфавита:")
  22.     word = input()
  23.     if (not set(word) - set(A)) == False:
  24.         return
  25.     if len(word) == 0:
  26.         word = ' '
  27.     is_end = False
  28.     #print("Rules")
  29.     #for i in range(n):
  30.     #    print(to_replace[i], '->',replacement[i])
  31.     print('Alg')
  32.     while is_end == False:
  33.         prev_word = word[::]
  34.         for i in range(n):
  35.             left_p = to_replace[i]
  36.             right_p = replacement[i]
  37.             if left_p.find(end_symbol) != -1:
  38.                 left_p = left_p.replace(end_symbol, '', 1)
  39.                 word = word.replace(left_p, right_p, 1)
  40.                 if word == prev_word:
  41.                     continue
  42.                 is_end = True
  43.                 break
  44.             word = word.replace(left_p, right_p, 1)
  45.             if word != prev_word:
  46.                 break
  47.         print("->", word)
  48.  
  49. if __name__ == '__main__':
  50.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement