title: Python: How to reduce runtime for four glass/lazy susan algorithm
body: <p>This is a homework question to write a function to solve the lazy susan problem, with the help of python files written by my tutor. </p>
  
  <p>Online explanation of the problem:
  <a href="https://www.interviewchacha.com/puzzles?qid=216" rel="nofollow noreferrer">https://www.interviewchacha.com/puzzles?qid=216</a></p>
  
  <p>These are some functions given to me.</p>
  
  <pre><code>create_table(size)
  </code></pre>
  
  <blockquote>
    <p>returns new Table with size number of coins</p>
  </blockquote>
  
  <pre><code>get_table_size(Table)
  </code></pre>
  
  <blockquote>
    <p>return number of coins on table</p>
  </blockquote>
  
  <pre><code>check_solved(Table)
  </code></pre>
  
  <blockquote>
    <p>returns True if all the coins are facing the same direction,otherwise returns False</p>
  </blockquote>
  
  <pre><code>flip_coins(Table,move=None)
  </code></pre>
  
  <blockquote>
    <p>has no return value. Flips coins on table at specified indices in the boolean move tuple,with length corresponding to number of coins on table.Table is spunned randomly</p>
  </blockquote>
  
  <p>I wrote a function that I would think works(it passed all the tests given in the assignment, however it's rejected as the code took a little too long(5s is considered too long for submission). Can anyone suggest how I can make the algorithm faster?</p>
  
  <pre><code>def solve(table):
      p=0
      x=get_table_size(table)
      while not check_solved(table):
          n=(p+1)%x
          if n==0:
          tup=p*(0,)+(1,)
          else:
              tup=p*(0,)+(1,0)+(x-n)*(0,)
          flip_coins(table,tup)
          p=n
  </code></pre>
  
  <p>Test case</p>
  
  <pre><code>t16_5 = create_table(16)
  solve(t16_5)
  print(check_solved(t16_5))
  </code></pre>
  
  <p>Tell me if any more info is needed</p>