title: how to find two expressions equivalent after transformation (e.g. commutative law etc.)
body: <p>I am making a 24-point game for my cousin and the program I design is to output all the possible solutions for the given four or five numbers. The underlying mechanism is basically finding all the possible bracket positions and fit in numbers to see whether the result is 24.
  The program is now able to find all answers but there is one big problem.</p>
  <p>For example, given the numbers</p>
  <p>3,3,6,7,9</p>
  <p>one possible answer is</p>
  <p>3*((9+7)/6*3)</p>
  <p>However, the program will also give answers like</p>
  <p>3*(9+7)/(6/3)</p>
  <p>3*((9+7)/(6/3))</p>
  <p>These are actually equivalent equations and should not be considered as different answers. I have managed to eliminate some of the equivalent answers by removing all the answers containing '+(', '-(', ')-', ')+' and ')*'. Is there any way to eliminate all the equivalent expressions? Thank you!</p>
  <p>I have attached my current code below.</p>
  <pre><code>
  from itertools import permutations
  
  while True:
      numnum = input('Please choose number of cards (enter anything other than 4 and 5 to quit the program):')
      if numnum == '4':
          try:
              a = int(input(&quot;Please enter the first number:&quot;))
              b = int(input(&quot;Please enter the second number:&quot;))
              c = int(input(&quot;Please enter the third number:&quot;))
              d = int(input(&quot;Please enter the fourth number:&quot;))
          except:
              print('WARNING: Please enter a real number!')
              continue
  
          print('Processing...')
          my_list = [a, b, c, d]
          # Randomly arrange a list of 4 integers
          result = [c for c in permutations(my_list, 4)]
  
          symbols = [&quot;+&quot;, &quot;-&quot;, &quot;*&quot;, &quot;/&quot;]
  
          list2 = []  # Calculate the list of 24 permutations and combinations
  
          flag = False
  
          for one, two, three, four in result:
              for s1 in symbols:
                  for s2 in symbols:
                      for s3 in symbols:
                          if s1 + s2 + s3 in ['+++', '***', '+--', '++-', '**/', '*//', '///']:
                              express = [&quot;{0}{1}{2}{3}{4}{5}{6}&quot;.format(one, s1, two, s2, three, s3, four)]
                          elif s1 + s2 + s3 in ['+-+', '-++', '-+-', '--+', '*/*', '/**', '/*/', '//*']:
                              continue
                          else:
                              express = [&quot;({0}{1}{2}){3}{4}{5}{6}&quot;.format(one, s1, two, s2, three, s3, four),
                                         &quot;({0}{1}{2}{3}{4}){5}{6}&quot;.format(one, s1, two, s2, three, s3, four),
                                         &quot;{0}{1}({2}{3}{4}){5}{6}&quot;.format(one, s1, two, s2, three, s3, four),
                                         &quot;{0}{1}({2}{3}{4}{5}{6})&quot;.format(one, s1, two, s2, three, s3, four),
                                         &quot;{0}{1}{2}{3}({4}{5}{6})&quot;.format(one, s1, two, s2, three, s3, four),
                                         &quot;(({0}{1}{2}){3}{4}){5}{6}&quot;.format(one, s1, two, s2, three, s3, four),
                                         &quot;({0}{1}{2}){3}({4}{5}{6})&quot;.format(one, s1, two, s2, three, s3, four),
                                         &quot;({0}{1}({2}{3}{4})){5}{6}&quot;.format(one, s1, two, s2, three, s3, four),
                                         &quot;{0}{1}(({2}{3}{4}){5}{6})&quot;.format(one, s1, two, s2, three, s3, four),
                                         &quot;{0}{1}({2}{3}({4}{5}{6}))&quot;.format(one, s1, two, s2, three, s3, four)]
  
                          for e in express:
                              try:
                                  if eval(e) == 24:
                                      list2.append(e)
                                      flag = True
                              except ZeroDivisionError:
                                  pass
  
          for c in list2:
              if '+(' in c or '-(' in c or ')-' in c or ')+' in c or ')*' in c:
                  continue
              print(c)
  
          if not flag:
              print(&quot;NO solution...&quot;)
  
      elif numnum == '5':
          try:
              a = int(input(&quot;Please enter the first number:&quot;))
              b = int(input(&quot;Please enter the second number:&quot;))
              c = int(input(&quot;Please enter the third number:&quot;))
              d = int(input(&quot;Please enter the fourth number:&quot;))
              e = int(input('Please enter the fifth number:'))
          except:
              print('WARNING: Please enter a real number!')
              continue
  
          print('Processing...')
          my_list = [a, b, c, d, e]
          # Randomly arrange a list of 4 integers
          result = [c for c in permutations(my_list, 5)]
  
          symbols = [&quot;+&quot;, &quot;-&quot;, &quot;*&quot;, &quot;/&quot;]
  
          list2 = []  # Calculate the list of 24 permutations and combinations
  
          flag = False
  
          for one, two, three, four, five in result:
              for s1 in symbols:
                  for s2 in symbols:
                      for s3 in symbols:
                          for s4 in symbols:
                              if s1 + s2 + s3 + s4 in ['++++', '****', '+---', '+++-', '++--', '**//', '*///', '///',
                                                       '***/']:
                                  express = [&quot;{0}{1}{2}{3}{4}{5}{6}{7}{8}&quot;.format(one, s1, two, s2, three, s3, four, s4, five)]
                              elif (s1, s2, s3, s4) in permutations(['*', '/', '/', '/']) \
                                      or (s1, s2, s3, s4) in permutations(['*', '*', '/', '/']) \
                                      or (s1, s2, s3, s4) in permutations(['*', '*', '*', '/']) \
                                      or (s1, s2, s3, s4) in permutations(['+', '-', '-', '-']) \
                                      or (s1, s2, s3, s4) in permutations(['+', '+', '-', '-']) \
                                      or (s1, s2, s3, s4) in permutations(['+', '+', '+', '-']):
                                  continue
                              else:
                                  express = [&quot;({0}{1}{2}){3}{4}{5}{6}{7}{8}&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;({0}{1}{2}{3}{4}){5}{6}{7}{8}&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;({0}{1}{2}{3}{4}{5}{6}){7}{8}&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;{0}{1}({2}{3}{4}){5}{6}{7}{8}&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;{0}{1}({2}{3}{4}{5}{6}){7}{8}&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;{0}{1}({2}{3}{4}{5}{6}{7}{8})&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;{0}{1}{2}{3}({4}{5}{6}){7}{8}&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;{0}{1}{2}{3}({4}{5}{6}{7}{8})&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;{0}{1}{2}{3}{4}{5}({6}{7}{8})&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;(({0}{1}{2}){3}{4}){5}{6}{7}{8}&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;(({0}{1}{2}){3}{4}{5}{6}){7}{8}&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;({0}{1}{2}){3}({4}{5}{6}){7}{8}&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;({0}{1}{2}){3}({4}{5}{6}{7}{8})&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;({0}{1}{2}){3}{4}{5}({6}{7}{8})&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;({0}{1}{2}{3}{4}){5}({6}{7}{8})&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;(({0}{1}{2}{3}{4}){5}{6}){7}{8}&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;({0}{1}({2}{3}{4})){5}{6}{7}{8}&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;({0}{1}({2}{3}{4}){5}{6}){7}{8}&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;({0}{1}{2}{3}({4}{5}{6})){7}{8}&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;({0}{1}({2}{3}{4}{5}{6})){7}{8}&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;{0}{1}(({2}{3}{4}){5}{6}){7}{8}&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;{0}{1}(({2}{3}{4}){5}{6}{7}{8})&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;{0}{1}({2}{3}{4}){5}({6}{7}{8})&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;{0}{1}({2}{3}({4}{5}{6})){7}{8}&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;{0}{1}(({2}{3}{4}{5}{6}){7}{8})&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;{0}{1}({2}{3}({4}{5}{6}){7}{8})&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;{0}{1}({2}{3}({4}{5}{6}{7}{8}))&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;{0}{1}({2}{3}{4}{5}({6}{7}{8}))&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;{0}{1}{2}{3}(({4}{5}{6}){7}{8})&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;{0}{1}{2}{3}({4}{5}({6}{7}{8}))&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;((({0}{1}{2}){3}{4}){5}{6}){7}{8}&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;(({0}{1}{2}){3}({4}{5}{6})){7}{8}&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;({0}{1}{2}){3}(({4}{5}{6}){7}{8})&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;({0}{1}{2}){3}({4}{5}({6}{7}{8}))&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;(({0}{1}({2}{3}{4})){5}{6}){7}{8}&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;({0}{1}({2}{3}{4})){5}({6}{7}{8})&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;({0}{1}(({2}{3}{4}){5}{6})){7}{8}&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;({0}{1}({2}{3}({4}{5}{6}))){7}{8}&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;{0}{1}((({2}{3}{4}){5}{6}){7}{8})&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;{0}{1}(({2}{3}{4}){5}({6}{7}{8}))&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;{0}{1}(({2}{3}({4}{5}{6})){7}{8})&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;{0}{1}({2}{3}(({4}{5}{6}){7}{8}))&quot;.format(one, s1, two, s2, three, s3, four, s4, five),
                                             &quot;{0}{1}({2}{3}({4}{5}({6}{7}{8})))&quot;.format(one, s1, two, s2, three, s3, four, s4, five)]
  
                              for e in express:
                                  try:
                                      if eval(e) == 24:
                                          list2.append(e)
                                          flag = True
                                  except ZeroDivisionError:
                                      pass
  
          for c in list2:
              if '+(' in c or '-(' in c or ')-' in c or ')+' in c or ')*' in c:
                  continue
              print(c)
  
          if not flag:
              print(&quot;NO solution...&quot;)
      else:
          break
  </code></pre>