Advertisement
faubiguy

bracketinfo.py

Nov 5th, 2014
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.11 KB | None | 0 0
  1. #!/usr/bin/python3
  2. import sys, bracket, itertools
  3. from collections import defaultdict
  4.  
  5. class Info:
  6.     pass
  7.  
  8. def get_info(n, notations=True, predicates=True, stats=True):
  9.     info = Info()
  10.     brackets, is_prime = bracket.get_brackets(n, include_primality=True)
  11.    
  12.     info.bracket_notation = brackets
  13.    
  14.     abc = brackets.replace('<>', 'a')
  15.     for letter in 'abcdefghij':
  16.         abc = abc.replace('<'+letter+'>', chr(ord(letter)+1))
  17.     info.abc_notation = abc
  18.    
  19.     p = abc.replace('<', 'p<')
  20.     for letter in 'abcdefghij':
  21.         p = p.replace(letter, 'p'*(ord(letter)-96)+'1')
  22.     info.p_notation = p
  23.  
  24.     digits_list = []
  25.     last_bracket = ''
  26.     for char in brackets:
  27.         if char == last_bracket:
  28.             digits_list[-1] += 1
  29.         else:
  30.             digits_list.append(1)
  31.         last_bracket = char
  32.     digits = ''.join(map(str,digits_list))
  33.     info.digit_notation = digits
  34.    
  35.     depth = []
  36.     current_depth = 0
  37.     max_depth = 0
  38.     add = True
  39.     for digit in digits_list[:-1]:
  40.         if add:
  41.             current_depth += digit
  42.         else:
  43.             current_depth -= digit
  44.         depth.append('*' * current_depth)
  45.         add = not add
  46.         if current_depth > max_depth:
  47.             max_depth = current_depth
  48.     info.depth_notation = '\n'.join(''.join(row) for row in itertools.zip_longest(*depth, fillvalue=' '))
  49.     info.is_prime = is_prime
  50.  
  51.     symmetrical = True
  52.     b = 0
  53.     numbers = {abc}
  54.     while numbers:
  55.         nonabc = set()
  56.         for number in numbers:
  57.             factors = defaultdict(int)
  58.             factor = ''
  59.             depth = 0
  60.             for symbol in number:
  61.                 if symbol == '<':
  62.                     depth += 1
  63.                 elif symbol == '>':
  64.                     depth -= 1
  65.                 if depth > max_depth:
  66.                     max_depth = depth
  67.                 factor += symbol
  68.                 if depth == 0:
  69.                     factors[factor] += 1
  70.                     if '<' in factor:
  71.                         nonabc.add(factor[1:-1])
  72.                     factor = ''
  73.             found_single = False
  74.             for factor, count in factors.items():
  75.                 if count % 2 == 1:
  76.                     if found_single:
  77.                         symmetrical = False
  78.                         break
  79.                     else:
  80.                         found_single = True
  81.             if not symmetrical:
  82.                 break
  83.         if not symmetrical:
  84.             break
  85.         numbers = nonabc
  86.     info.is_symmetrical = symmetrical
  87.    
  88.     info.is_alphabetical = not ('<' in abc)
  89.  
  90.     info.length = len(brackets)
  91.     info.reversals= len(digits) - 1
  92.     info.max_depth = max_depth
  93.     factors = bracket.factor(n)
  94.     info.factors = len(factors)
  95.     info.smoothness = max(factors)
  96.     info.necessary_brackets = abc.count('<')*2
  97.    
  98.     return info
  99.  
  100. if __name__ == '__main__':
  101.     n = int(sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read())
  102.     print(n)
  103.     info = get_info(n)
  104.     print(info.bracket_notation)
  105.     print(info.abc_notation)
  106.     print(info.p_notation)
  107.     print(info.digit_notation)
  108.     print('[code]')
  109.     print(info.depth_notation)
  110.     print('[/code]')
  111.     print()
  112.     print('Prime: {0}'.format('Yes' if info.is_prime else 'No'))
  113.     print('Symmetrical: {0}'.format('Yes' if info.is_symmetrical else 'No'))
  114.     print('Alphabetical: {0}'.format('Yes' if info.is_alphabetical else 'No'))
  115.     print()
  116.     print('Length: {0}'.format(info.length))
  117.     print('Reversals: {0}'.format(info.reversals))
  118.     print('Max Depth: {0}'.format(info.max_depth))
  119.     print('Factors: {0}'.format(info.factors))
  120.     print('Smoothness: {0}'.format(info.smoothness))
  121.     print('Necessary Brackets: {0}'.format(info.necessary_brackets))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement