Advertisement
BanyRule

Ege16

Jan 26th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.10 KB | None | 0 0
  1. OPERATORS = {'+': (1, lambda x, y: x + y), '-': (1, lambda x, y: x - y),
  2.              '*': (2, lambda x, y: x * y), '/': (2, lambda x, y: x / y)}
  3.  
  4.  
  5. def eval_(formula):
  6.     def parse(formula_string):
  7.         number = ''
  8.         for s in formula_string:
  9.             if s in '1234567890.':
  10.                 number += s
  11.             elif number:
  12.                 yield float(number)
  13.                 number = ''
  14.             if s in OPERATORS or s in "()":
  15.                 yield s
  16.         if number:
  17.             yield float(number)
  18.  
  19.     def shunting_yard(parsed_formula):
  20.         stack = []
  21.         for token in parsed_formula:
  22.             if token in OPERATORS:
  23.                 while stack and stack[-1] != "(" and OPERATORS[token][0] <= OPERATORS[stack[-1]][0]:
  24.                     yield stack.pop()
  25.                 stack.append(token)
  26.             elif token == ")":
  27.                 while stack:
  28.                     x = stack.pop()
  29.                     if x == "(":
  30.                         break
  31.                     yield x
  32.             elif token == "(":
  33.                 stack.append(token)
  34.             else:
  35.                 yield token
  36.         while stack:
  37.             yield stack.pop()
  38.  
  39.     def calc(polish):
  40.         stack = []
  41.         for token in polish:
  42.             if token in OPERATORS:
  43.                 y, x = stack.pop(), stack.pop()
  44.                 stack.append(OPERATORS[token][1](x, y))
  45.             else:
  46.                 stack.append(token)
  47.         return stack[0]
  48.  
  49.     return calc(shunting_yard(parse(formula)))
  50.  
  51. def splitting( s ):
  52.     sout = ''
  53.     boof = s[0]
  54.     count = 1
  55.     for i in s[1:]:
  56.         if i == boof:
  57.             count += 1
  58.         else:
  59.             if count == 1:
  60.                 sout += str(boof)
  61.             else:
  62.                 sout += ( '[' + str(boof) + ':' +str(count) + ']')
  63.             count = 1
  64.         boof = i
  65.     sout += ( '[' + str(boof) + ':' +str(count) + ']')
  66.     return sout
  67.  
  68.  
  69. x = bin(eval(input()))
  70. print( '...[цифра:кол-во]...')
  71. print( splitting(x[2:]) )
  72.  
  73. print('0 => ' + str(x.count('0')-1))
  74. print('1 => ' + str(x.count('1')))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement