예제 #1
0
 def test_add_empty_list( self ):
     ll = DoubleLinkedList()
     ll.addEnd( 50 )
     self.assertEqual( ll.size(), 1 )
     self.assertNotEqual( ll.head, None )
     self.assertNotEqual( ll.tail, None )
     self.assertEqual( ll.head, ll.tail )
예제 #2
0
파일: deque.py 프로젝트: SWHarrison/CS-1-3
 def __init__(self, iterable=None):
     """Initialize this deque and enqueue the given items, if any."""
     # Initialize a new linked list to store the items
     self.list = DoubleLinkedList()
     if iterable is not None:
         for item in iterable:
             self.push_back(item)
예제 #3
0
 def test_remove_middle_list( self ):
     ll = DoubleLinkedList()
     ll.addEnd( 50 )
     ll.addEnd( 55 )
     ll.addEnd( 60 )
     ll.addEnd( 65 )
     ll.remove( ll.search(55) )
     self.assertEqual( ll.size(), 3 )
예제 #4
0
 def test_remove_head_list( self ):
     ll = DoubleLinkedList()
     ll.addEnd( 50 )
     ll.addEnd( 55 )
     ll.addEnd( 60 )
     ll.addEnd( 65 )
     ll.remove( ll.search(50) )
     self.assertEqual( ll.size(), 3 )
     self.assertNotEqual( ll.head, None )
     self.assertEqual( ll.head.getData(), 55 )
예제 #5
0
 def test_remove_end_list( self ):
     ll = DoubleLinkedList()
     ll.addEnd( 50 )
     ll.addEnd( 55 )
     ll.addEnd( 60 )
     ll.addEnd( 65 )
     ll.remove( ll.search(65) )
     self.assertEqual( ll.size(), 3 )
     self.assertNotEqual( ll.tail, None )
     self.assertEqual( ll.tail.getData(), 60 )
예제 #6
0
 def test_nonempty_list( self ):
     ll = DoubleLinkedList()
     ll.add( 50 )
     ll.add( 55 )
     ll.add( 60 )
     ll.add( 65 )
     old_head = ll.head
     old_tail = ll.tail
     ll.reverse()
     self.assertEqual( ll.head, old_tail )
     self.assertEqual( ll.tail, old_head )
     self.assertEqual( ll.toList(), [50,55,60,65] )
예제 #7
0
 def test_replace(self):
     ll = DoubleLinkedList(['A', 'B', 'C'])
     ll.replace('A', 'D')
     assert ll.head.data == 'D'  # new head
     assert ll.tail.data == 'C'  # unchanged
     assert ll.size == 3
     ll.replace('B', 'E')
     assert ll.head.data == 'D'  # unchanged
     assert ll.tail.data == 'C'  # unchanged
     assert ll.size == 3
     ll.replace('C', 'F')
     assert ll.head.data == 'D'  # unchanged
     assert ll.tail.data == 'F'  # new tail
     assert ll.size == 3
     with self.assertRaises(ValueError):
         ll.replace('X', 'Y')  # item not in list
예제 #8
0
 def test_delete(self):
     ll = DoubleLinkedList(['A', 'B', 'C'])
     ll.delete('A')
     assert ll.head.data == 'B'  # new head
     assert ll.tail.data == 'C'  # unchanged
     assert ll.size == 2
     ll.delete('C')
     assert ll.head.data == 'B'  # unchanged
     assert ll.tail.data == 'B'  # new tail
     assert ll.size == 1
     ll.delete('B')
     assert ll.head is None  # new head
     assert ll.tail is None  # new head
     assert ll.size == 0
     with self.assertRaises(ValueError):
         ll.delete('X')  # item not in list
예제 #9
0
 def test_get_at_index(self):
     ll = DoubleLinkedList(['A', 'B', 'C'])
     assert ll.get_at_index(0) == 'A'  # head item
     assert ll.get_at_index(1) == 'B'  # middle item
     assert ll.get_at_index(2) == 'C'  # tail item
     with self.assertRaises(ValueError):
         ll.get_at_index(3)  # index too high
     with self.assertRaises(ValueError):
         ll.get_at_index(-1)  # index too low
예제 #10
0
 def test_nonempty_list( self ):
     ll = DoubleLinkedList()
     ll.add( 50 )
     ll.add( 55 )
     ll.add( 60 )
     ll.add( 65 )
     self.assertEqual( ll.toList(), [65,60,55,50] )
예제 #11
0
 def test_prepend(self):
     ll = DoubleLinkedList()
     ll.prepend('C')
     assert ll.head.data == 'C'  # new head
     assert ll.tail.data == 'C'  # new head
     assert ll.size == 1
     ll.prepend('B')
     assert ll.head.data == 'B'  # new head
     assert ll.tail.data == 'C'  # unchanged
     assert ll.size == 2
     assert ll.head.next.data == 'C'
     assert ll.tail.previous.data == 'B'
     ll.prepend('A')
     assert ll.head.data == 'A'  # new head
     assert ll.tail.data == 'C'  # unchanged
     assert ll.size == 3
     assert ll.head.next.data == 'B'
     assert ll.tail.previous.data == 'B'
예제 #12
0
 def test_init_with_list(self):
     ll = DoubleLinkedList(['A', 'B', 'C'])
     assert ll.head.data == 'A'  # first item
     assert ll.tail.data == 'C'  # last item
     assert ll.size == 3
예제 #13
0
 def test_nonempty_list_reverse_updates_prev_ptr( self ):
     ll = DoubleLinkedList()
     ll.add( 50 )
     ll.add( 55 )
     ll.add( 60 )
     ll.add( 65 )
     ll.reverse()
     n60 = ll.remove( ll.search( 60 ) )
     # NOTE: This test is testing that you aren't cheating with
     # your reverse() function by just doing what a singly linked
     # list does..  it might run into an infinite loop :)
     self.assertEqual( ll.toList(), [50,55,65] )
예제 #14
0
 def test_create_empty_list( self ):
     ll = DoubleLinkedList()
     self.assertEqual( ll.size(), 0 )
     self.assertEqual( ll.head, None )
     self.assertEqual( ll.tail, None )
예제 #15
0
 def test_insert_end_nonempty_list( self ):
     ll = DoubleLinkedList()
     ll.add( 50 )
     ll.insert( 100, ll.search(50) )
     self.assertEqual( ll.size(), 2 )
예제 #16
0
 def test_search_empty_list( self ):
     ll = DoubleLinkedList()
     self.assertEqual( ll.search(10), None )
예제 #17
0
 def test_search_nonempty_list_notfound( self ):
     ll = DoubleLinkedList()
     ll.add( 10 )
     self.assertEqual( ll.search(11), None )
예제 #18
0
 def test_size(self):
     ll = DoubleLinkedList()
     assert ll.size == 0
     # append and prepend operations increment size
     ll.append('B')
     assert ll.size == 1
     ll.prepend('A')
     assert ll.size == 2
     ll.append('C')
     assert ll.size == 3
     # delete operations decrement size
     ll.delete('B')
     assert ll.size == 2
     ll.delete('C')
     assert ll.size == 1
     ll.delete('A')
     assert ll.size == 0
예제 #19
0
 def test_items(self):
     ll = DoubleLinkedList()
     assert ll.items() == []
     ll.append('B')
     assert ll.items() == ['B']
     ll.prepend('A')
     assert ll.items() == ['A', 'B']
     ll.append('C')
     assert ll.items() == ['A', 'B', 'C']
예제 #20
0
파일: tidalbar.py 프로젝트: stshow/tidalbar
#
#   CREATE OBJECTS
#

# Get the mpv player up and running
player = mpv.MPV()
# ! Check for errors

# Establish the Tidal session using the Kodi tidalapi library
session = tidalapi.Session()

# Create list to keep track of playlist. Can't use mpv since the URLs given back from
# Tidal have expiration dates. Usually, you can get 6 or so tracks in before the URLs
# start to go bad, but this object just fetches a URL as needed
internal_playlist = DoubleLinkedList()

#
#   LOGIN TO TIDAL
#
#   Use the tidalapi to login, give the user 3 attempts before aborting
#
#   * nonblockingkb is not used since getpass is more secure

login_attempts = 0
allowed_attempts = 3
while login_attempts < allowed_attempts:
    try:
        username = input('TIDAL username: '******'TIDAL password: ')
        if session.login(username, password):
예제 #21
0
파일: deque.py 프로젝트: SWHarrison/CS-1-3
class Deque(object):
    def __init__(self, iterable=None):
        """Initialize this deque and enqueue the given items, if any."""
        # Initialize a new linked list to store the items
        self.list = DoubleLinkedList()
        if iterable is not None:
            for item in iterable:
                self.push_back(item)

    def __repr__(self):
        """Return a string representation of this deque."""
        return 'deque({} items, front={} back ={})'.format(
            self.length(), self.front(), self.back())

    def is_empty(self):
        """Return True if this deque is empty, or False otherwise."""
        return self.list.size == 0

    def length(self):
        """Return the number of items in this deque."""
        return self.list.size

    def push_back(self, item):
        """Insert the given item at the back of this deque.
        Running time: O(1)"""
        # TODO: Insert given item
        self.list.append(item)

    def push_front(self, item):
        """Insert the given item at the front of this deque.
        Running time: O(1)"""
        # TODO: Insert given item
        self.list.prepend(item)

    def front(self):
        """Return the item at the front of this deque without removing it,
        or None if this deque is empty."""
        if (self.is_empty()):
            print("deque is empty!")
            return None

        return self.list.head.data

    def back(self):
        """Return the item at the front of this deque without removing it,
        or None if this deque is empty."""
        if (self.is_empty()):
            print("deque is empty!")
            return None

        return self.list.tail.data

    def pop_front(self):
        """Remove and return the item at the front of this deque,
        or raise ValueError if this deque is empty.
        Running time: O(1)"""
        if (self.is_empty()):
            raise ValueError("deque is empty!")

        to_return = self.list.head.data
        self.list.head = self.list.head.next
        self.list.size -= 1
        return to_return

    def pop_back(self):
        """Remove and return the item at the back of this deque,
        or raise ValueError if this deque is empty.
        Running time: O(1)"""
        if (self.is_empty()):
            raise ValueError("deque is empty!")

        to_return = self.list.tail.data
        self.list.tail = self.list.tail.previous
        self.list.size -= 1
        return to_return
예제 #22
0
 def test_find(self):
     ll = DoubleLinkedList(['A', 'B', 'C'])
     assert ll.find(lambda item: item == 'B') == 'B'
     assert ll.find(lambda item: item < 'B') == 'A'
     assert ll.find(lambda item: item > 'B') == 'C'
     assert ll.find(lambda item: item == 'X') is None
예제 #23
0
 def test_empty_list( self ):
     ll = DoubleLinkedList()
     ll.reverse()
     self.assertEqual( ll.toList(), [] )
예제 #24
0
파일: tidalbar.py 프로젝트: stshow/tidalbar
def clear_playlist():
    internal_playlist = DoubleLinkedList()
    run_menu(main_menu)
예제 #25
0
 def test_insert_and_remove_list( self ):
     ll = DoubleLinkedList()
     ll.add( 50 )
     ll.addEnd( 100 )
     ll.insert( 150, ll.search(50) ) # insert in middle
     self.assertEqual( ll.size(), 3 )
     ll.remove( ll.search( 150 ) )
     self.assertEqual( ll.size(), 2 )
     self.assertEqual( ll.toList(), [50, 100] )
예제 #26
0
 def test_remove_entire_list( self ):
     ll = DoubleLinkedList()
     ll.add( 50 )
     ll.add( 55 )
     ll.add( 60 )
     ll.add( 65 )
     ll.remove( ll.search(65) )
     self.assertEqual( ll.size(), 3 )
     ll.remove( ll.search(55) )
     self.assertEqual( ll.size(), 2 )
     ll.remove( ll.search(50) )
     self.assertEqual( ll.size(), 1 )
     ll.remove( ll.search(60) )
     self.assertEqual( ll.size(), 0 )
예제 #27
0
    def test_doublelinkedlist(self):
        node1 = ListNode(1)
        node2 = ListNode(5)
        node3 = ListNode(10)
        node4 = ListNode(5)

        linked_list = DoubleLinkedList()

        linked_list.add_list_item(node1)
        linked_list.add_list_item(node2)
        linked_list.add_list_item(node3)
        linked_list.add_list_item(node4)
        
        self.assertEqual(linked_list.list_length(), 4)
        self.assertEqual(linked_list.unordered_search(5), [1, 3])

        linked_list.remove_list_item_by_index(1)

        self.assertEqual(linked_list.list_length(), 3)
        self.assertEqual(linked_list.unordered_search(5), [2])
예제 #28
0
 def test_insert_remove_and_reverse_list( self ):
     ll = DoubleLinkedList()
     ll.add( 50 )
     ll.addEnd( 100 )
     ll.insert( 150, ll.search(50) ) # insert in middle
     self.assertEqual( ll.size(), 3 )
     ll.remove( ll.search( 50 ) )
     self.assertEqual( ll.size(), 2 )
     ll.reverse()
     # NOTE: This test is testing that you don't forget to
     # update next / prev properly when inserting..
     self.assertEqual( ll.toList(), [100, 150] )
예제 #29
0
 def test_add_nonempty_list( self ):
     ll = DoubleLinkedList()
     ll.add( 50 )
     ll.add( 100 )
     self.assertEqual( ll.size(), 2 )
예제 #30
0
 def test_length(self):
     ll = DoubleLinkedList()
     assert ll.length() == 0
     # append and prepend operations increase length
     ll.append('B')
     assert ll.head.previous == None
     assert ll.head.next == None
     assert ll.tail.previous == None
     assert ll.tail.next == None
     assert ll.length() == 1
     ll.prepend('A')
     assert ll.head.previous == None
     assert ll.head.next.data == 'B'
     assert ll.tail.previous.data == 'A'
     assert ll.tail.next == None
     assert ll.length() == 2
     ll.append('C')
     assert ll.head.previous == None
     assert ll.head.next.data == 'B'
     assert ll.tail.previous.data == 'B'
     assert ll.tail.next == None
     assert ll.length() == 3
     # delete operations decrease length
     ll.delete('B')
     assert ll.head.previous == None
     assert ll.head.next.data == 'C'
     assert ll.tail.previous.data == 'A'
     assert ll.tail.next == None
     assert ll.length() == 2
     ll.delete('C')
     assert ll.head.previous == None
     assert ll.head.next == None
     assert ll.tail.previous == None
     assert ll.tail.next == None
     assert ll.length() == 1
     ll.delete('A')
     assert ll.head == None
     assert ll.head == None
     assert ll.length() == 0