title: Evaluating expressions of random length
body: <p>This is based on a problem <a href="https://www.hackerrank.com/challenges/arithmetic-expressions/problem" rel="nofollow noreferrer">here</a>:</p>
  
  <blockquote>
    <p><strong>Input Format</strong></p>
    
    <p>The first line contains a single integer <em>n</em> denoting the number of
    elements in the list. The second line contains <em>n</em> space-separated
    integers <em>a1, a2, a3, ...</em> denoting the elements of the list.</p>
    
    <p><strong>Output Format</strong></p>
    
    <p>Print a single line containing the required expressoin. You may insert
    spaces between operators and operands.</p>
    
    <p><strong>Note</strong></p>
    
    <ul>
    <li>You are not allowed to permute the list.</li>
    <li>All operators have the same precedence and are left-associative</li>
    </ul>
  </blockquote>
  
  <p>In my code below I can make it work using nested <code>for</code> loops when I know the length of the input, but my input may have 5 or 50 or 500 elements and I won't know that until run-time. The depth of the nested <code>for</code> loops would be <code>len(arr)-1</code>, but maybe there's a better way to approach this problem. </p>
  
  <p>As I see it, I need to:</p>
  
  <ul>
  <li>generate <code>ops_combos</code> for <code>input_ints</code> of random length (without using the <code>permutations</code> lib, per hackerrank instructions)</li>
  <li>evaluate the expressions</li>
  <li>and return the first expression that evaluates <code>True</code>. </li>
  </ul>
  
  <p>Any suggestions?</p>
  
  <pre><code>import operator
  ops = { "+": operator.add, "*": operator.mul, "-": operator.sub } # etc.
  
  opers = ['+','*','-']
  arr = [55, 3, 45, 33, 25] # needs to work for any length of arr
  
  # Generate every len(arr)-1 combo of the 3 symbols (include repetition)
  for i in range(3):
      for j in range(3):
          for k in range(3):
              for l in range(3):                
                  x = ops[ opers[i] ]( arr[0], arr[1] )   # first number (0), first operator  (i), second number (1)
                  x = ops[ opers[j] ]( x,      arr[2] )   # x,                second operator (j), third number  (2)
                  x = ops[ opers[k] ]( x,      arr[3] )   # x,                third operator  (k), fourth number (3)
                  x = ops[ opers[l] ]( x,      arr[4] )   # x,                fourth operator (l), fifth number  (4)
  
                  if x % 101 == 0:
                      print (arr[0], opers[i], arr[1], opers[j], arr[2], opers[k], arr[3], opers[l], arr[4])
  </code></pre>