Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.36 KB | None | 0 0
  1. class Node():
  2.  
  3.     def __init__(self, char=None, copy=None):
  4.  
  5.         if char:
  6.             self.powers = [ [int(ord(char) - ord('a') == i) for i in range(26)] ]
  7.             self.consts = [ 1 ]
  8.  
  9.         if copy:
  10.             self.powers = copy.powers[:]
  11.             self.consts = copy.consts[:]
  12.  
  13.     def __pos__(self):
  14.  
  15.         return self
  16.  
  17.     def __neg__(self):
  18.  
  19.         res = Node(copy=self)
  20.         res.consts = [-i for i in res.consts]
  21.  
  22.         return res
  23.  
  24.     def __add__(self, val):
  25.  
  26.         res = Node(copy=self)
  27.  
  28.         res.powers += val.powers
  29.         res.consts += val.consts
  30.  
  31.         return res
  32.  
  33.     def __sub__(self, val):
  34.  
  35.         val = val.__neg__()
  36.         return self.__add__(val)
  37.  
  38.     def __mul__(self, val):
  39.  
  40.         res = Node(copy=self)
  41.         res.powers = [[i[k] + j[k] for k in range(26)] for i in self.powers for j in val.powers]
  42.         res.consts = [i*j for i in self.consts for j in val.consts]
  43.  
  44.         return res
  45.  
  46.  
  47.     def __str__(self):
  48.  
  49.         #return "POWS: " + str(self.powers) + "; CONSTS: " + str(self.consts)
  50.         arr_sorted = sorted(tuple([(self.consts[i], self.powers[i]) for i in range(len(self.consts))]), key=lambda x:x[1])
  51.  
  52.         self.consts = [i[0] for i in arr_sorted]
  53.         self.powers = [i[1] for i in arr_sorted]
  54.  
  55.         i = 0
  56.         j = 1
  57.         while i < len(self.powers):
  58.            
  59.             j = i+1
  60.             const = self.consts[i]
  61.  
  62.             while j < len(self.powers) and self.powers[i] == self.powers[j]:
  63.                 const += self.consts[j]
  64.                 self.consts = self.consts[:j] + self.consts[j+1:]
  65.                 self.powers = self.powers[:j] + self.powers[j+1:]
  66.  
  67.             self.consts[i] = const
  68.             i = j
  69.  
  70.         res = ""
  71.         if self.consts.count(0) == len(self.consts):
  72.             res = "0"
  73.         else:
  74.  
  75.             for const_id in range(len(self.consts)):
  76.  
  77.                 const = self.consts[const_id]
  78.                 if const != 0:
  79.  
  80.                     if len(res) > 0 and const > 0:
  81.                         res += "+"
  82.  
  83.                     visited = False
  84.                     if const != 1:
  85.                         if const == -1:
  86.                             res += "-"
  87.                         else:
  88.                             res += str(const)
  89.                             visited = True
  90.  
  91.                     for char_id in range(26):
  92.                         power = self.powers[const_id][char_id]
  93.                         if power != 0:
  94.  
  95.                             if visited:
  96.                                 res += "*"
  97.                             else:
  98.                                 visited = True
  99.                            
  100.                             res += chr(ord("a")+char_id)
  101.                             if power != 1:
  102.                                 res += "^"+str(power)
  103.  
  104.         return res
  105.  
  106.  
  107. for i in range(26):
  108.     char = chr(ord("a") + i)
  109.     exec(char + " = Node('" + char +"')")
  110.  
  111. i = Node("i")
  112.  
  113. with open("input.txt", "r") as fin, open("output.txt", "w") as fout:
  114.     x = str(eval(fin.read()))
  115.     print(x)
  116.     fout.write(x)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement