This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

boris_g

By: a guest on Oct 26th, 2008  |  syntax: Python  |  size: 1.46 KB  |  views: 36  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. ''' Sorted linked list - based on linked list'''
  2.  
  3. import unittest
  4.  
  5. from linkedlist import LinkedList
  6.  
  7.  
  8.  
  9. class Derived(LinkedList):
  10.     def __init__ (self, sequence=()):
  11.         LinkedList.__init__(self, sequence)
  12.         self.sort()
  13.  
  14.     def insert_sorted(self, item, size_max = None):
  15.         ''' Insert the item, keeping the entire list sorted.
  16.  
  17.        If size_max is specified, then if after the addition the list size
  18.        will be higher than size_max, the last element will be removed
  19.  
  20.        NOT DEBUGGED
  21.  
  22.        '''
  23.         LinkedList.append(self, item)
  24.         self.sort()
  25.  
  26.     def __repr__(self):
  27.         return "Derived(%s)" % LinkedList.__repr__(self)
  28.  
  29.  
  30. class TestDerived (unittest.TestCase):
  31.     def test_derived(self):
  32.         class SomeItem:
  33.             def __init__(self, data,dummy):
  34.                 self.data = data
  35.                 self.dummy = dummy
  36.  
  37.             def __cmp__(self, another):
  38.                 return cmp(getattr(self,'data',None), getattr(self,'data',another))
  39.  
  40.             def __rep__(self):
  41.                 return "<%d,%d>"%(self.data, self.dummy)
  42.  
  43.         l = Derived()
  44.         for i in range(10):
  45.             si = SomeItem(i, i)
  46.             l.insert_sorted(si)
  47.  
  48.         print 'derived:%s'%(l)
  49.         l_iter = iter(l)
  50.         prev = l_iter.next()
  51.         for i in l_iter:
  52.             self.assertEqual(cmp(prev, i), -1)
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60. if __name__ == '__main__':
  61.     unittest.main()
clone this paste RAW Paste Data