Exemplo n.º 1
0
def determine_sort( list_orig, data ):
  # Get history for sorts: Selection, Insertion, and Shell
  hSelection, hInsertion, hShell = get_sort_history( list_orig )
  # Append 0 for "Original Sort"
  res = [0] 
  # Determine sorts for test_data 1 to N-1 
  for i,test_data in enumerate(data[1:-1]):
    R = [H.history_contains( hInsertion, test_data ), 
         H.history_contains( hSelection, test_data ), 
         H.history_contains( hShell,     test_data )]
    if   R[0] and not R[1] and not R[2]:
      res.append(1) # Insertion sort
    elif not R[0] and R[1] and not R[2]:
      res.append(2) # Selection sort
    elif not R[0] and not R[1] and R[2]:
      res.append(3) # Shell sort
    else:
      raise Exception("UNKNOWN SORT")
    print test_data
    print i+1, res[-1], R[0]
    print i+1, res[-1], R[1]
    print i+1, res[-1], R[2]
  # Append 4 for "Sorted"
  res.append(4)
  return res
Exemplo n.º 2
0
 def test_wk2_ex_Stacks_489125b(self):
   # (seed = 489125)
   print "\n489125b"
   #o, stk = run("0 1 2 3 4 5 6 7 8 9")
   o, stk = run("0 - 1 - 2 3 - - 4 - 5 6 - 7 8 - - 9")
   #self.failUnless( H.chk(o, "0 1 3 2 4 6 8 5 7 9"))
   o, stk = run("0 1 2 3 4 5 6 - - - - - - - 7 - 8 - 9 -")
   self.failUnless( H.chk(o, "6 5 4 3 2 1 0 7 8 9"))
   o, stk = run("0 1 2 3 - 4 - 5 - 6 7 8 - 9 - - - - - -")
   self.failUnless( H.chk(o, "3 4 5 8 9 7 6 2 1 0"))
   o, stk = run("0 - 1 2 3 - 4 5 6 7 - - - - - 8 9 - - -")
   self.failUnless( H.chk(o, "0 3 7 6 5 4 2 9 8 1"))
   o, stk = run("0 1 - - 2 3 - 4 5 - - 6 7 8 9")
Exemplo n.º 3
0
 def test_wk2_ex_Stacks_634506b(self):
   # (seed = 634506)
   print "\n634506b DONE"
   o, stk = run("0 1 2 3 - - - 4 5 - - 6 7 8 9")
   #self.failUnless( H.chk(o, "3 2 1 5 0 6 7 8 9 4") )
   o, stk = run("0 1 2 - - - 3 - 4 - 5 - 6 - 7 - 8 - 9 -")
   self.failUnless( H.chk(o, "2 1 0 3 4 5 6 7 8 9") )
   o, stk = run("0 - 1 - 2 3 - - 4 - 5 - 6 - 7 8 - - 9 -")
   self.failUnless( H.chk(o, "0 1 3 2 4 5 6 8 7 9") )
   o, stk = run("0 1 2 3 - 4 5 - - 6 7 8 9")
   #self.failUnless( H.chk(o, "3 5 2 4 6 1 0 8 7 9") )
   o, stk = run("0 1 2 - - 3 4 5 6 - - - 7 - - 8 - - 9 -")
   self.failUnless( H.chk(o, "2 1 6 5 4 7 3 8 0 9") )
Exemplo n.º 4
0
 def test_wk2_ex_Stacks_634506(self):
   # (seed = 634506)
   # Suppose that an intermixed sequence of 10 push and 10 pop
   # operations are performed on a LIFO stack. The pushes push
   # the letters 0 through 9 in order; the pops print out the
   # return value. Which of the following output sequence(s)
   # could occur?
   print "\n634506 DONE"
   o, stk = run("0 1 2 3 - - - 4 5 - 6 7 8 9")
   #self.failUnless( H.chk(o, "3 2 1 5 0 6 7 8 9 4") )
   o, stk = run("0 1 2 - - - 3 - 4 - 5 - 6 - 7 - 8 - 9 -")
   self.failUnless( H.chk(o, "2 1 0 3 4 5 6 7 8 9") )
   o, stk = run("0 - 1 - 2 3 - - 4 - 5 - 6 - 7 8 - - 9 -")
   self.failUnless( H.chk(o, "0 1 3 2 4 5 6 8 7 9") )
   o, stk = run("0 1 2 3 - 4 5 - - 6 7 8 9")
   #self.failUnless( H.chk(o, "3 5 2 4 6 1 0 8 7 9") )
   o, stk = run("0 1 2 - - 3 4 5 6 - - - 7 - - 8 - - 9 -")
   self.failUnless( H.chk(o, "2 1 6 5 4 7 3 8 0 9") )
Exemplo n.º 5
0
 def test_wk2_q2a(self):
   desc = 'SELECTION SORT WORDS'
   exp = "BECK BUSH DEVO EVE6 HOLE JAYZ KORN MIMS VAIN RATT TOTO PINK SADE NOFX SOAD WHAM"
   a = "HOLE BUSH MIMS BECK WHAM SOAD NOFX TOTO VAIN RATT DEVO PINK SADE KORN JAYZ EVE6".split()
   array_history = []
   Sort(a, array_history)
   show_array_history(desc, array_history)
   for idx, A in enumerate(array_history):
     if H.chk( A[0], exp ):
       print "MATCH", idx
   print
Exemplo n.º 6
0
  def test_wk2_ex_Stacks_489125(self):
    # (seed = 489125)
    # Suppose that an intermixed sequence of 10 push and 10 pop
    # operations are performed on a LIFO stack. The pushes push
    # the letters 0 through 9 in order; the pops print out the
    # return value. Which of the following output sequence(s)
    # could occur?
    # o, stk = run("0 1 2 3 4 5 6 7 8 9")
    print "\n489125 DONE"
    o, stk = run("0 - 1 - 2 3 - - 4 - 5 6 - 7 8 - - 9 -")
    #self.failUnless( H.chk(o, "0 1 3 2 4 6 8 5 7 9") )

    o, stk = run("0 1 2 3 4 5 6 - - - - - - - 7 - 8 - 9 -")
    self.failUnless( H.chk(o, "6 5 4 3 2 1 0 7 8 9") )

    o, stk = run("0 1 2 3 - 4 - 5 - 6 7 8 - 9 - - - - - -")
    self.failUnless( H.chk(o, "3 4 5 8 9 7 6 2 1 0") )

    o, stk = run("0 - 1 2 3 - 4 5 6 7 - - - - - 8 9 - - -")
    self.failUnless( H.chk(o, "0 3 7 6 5 4 2 9 8 1") )

    o, stk = run("0 1 - - 2 3 - 4 5 - 6 7 8 9")
    # self.failUnless( H.chk(o, "1 0 3 5 2 7 6 8 9 4") )
    print
Exemplo n.º 7
0
  def test_wk2_ex_Queues_608030(self):
    # Question 2
    # (seed = 608030)
    # Suppose that an intermixed sequence of 10 enqueue and 10
    # dequeue operations are performed on a FIFO queue. The
    # enqueues add the letters 0 through 9 in order; the dequeues
    # print out the return value. Which of the following output
    # sequence(s) could occur?
    # r = run("0 1 2 3 4 5 6 7 8 9")
    r = run("0 - 1 2 - 3 4 5 6 7 8 9")
    #self.failUnless( H.chk(r, "0 2 1 3 7 4 9 6 8 5") )

    r = run("0 1 2 - - - 3 4 5 - 6 - - 7 8 9")
    #self.failUnless( H.chk(r, "0 1 2 5 6 4 8 9 3 7") )

    r = run("0 1 2 3 4 5 6 7 8 9 - - - - - - - - - -")
    self.failUnless( H.chk(r, "0 1 2 3 4 5 6 7 8 9") )

    r = run("0 - 1 2 3 4 5 6 7 8 9")
    #self.failUnless( H.chk(r, "0 7 4 1 8 3 6 2 9 5") )

    r = run("0 - 1 2 3 4 5 6 7 8 9")