def makeFormulas(self):
     """
     The main workhorse method, which extracts formulas from the text.
     """
     formulaDict = {}
     for i in reversed(range(2, 15)):
         if self.verbose:
             print('Поиск n-грамм длины ' + str(i))
         Ngrams = []
         for line in self.poem:
             Ngrams += self.makeNGrams(line, i)
         if self.verbose and len(Ngrams) > 0:
             for item in Ngrams[:-1]:
                 print(item, end=' | ')
             print(Ngrams[-1])
         while True:
             Ngrams = self.filter(Ngrams)
             if len(Ngrams) > 1:
                 basis  = Ngrams[0]
                 formulas2Be = LinkedList()
                 formulas2Be.add(basis)
                 for Ngram in Ngrams[1:]:
                     if Ngram == basis:
                         formulas2Be.add(Ngram)
                 for item in formulas2Be:
                     Ngrams.remove(item)
                 if len(formulas2Be) >= 2:
                     for item in formulas2Be:
                         for word in item.words:
                             self.useMatrix[word.line][word.index] = True
                     formulaDict[basis.key] = formulas2Be
             else:
                 break
     return formulaDict
Ejemplo n.º 2
0
class Queue():
    def __init__(self):
        self.items = LinkedList()

    def enqueue(self, data):
        self.items.prepend(data=data)

    def dequeue(self):
        if self.items.tail.data:
            item = self.items.tail.data
            self.items.remove(item)
            return item
        return None

    def peek(self):
        if self.items.tail.data:
            return self.items.tail.data
        return None

    def lookup(self, data):
        while self.items.head.data:
            if self.items.head.data == data:
                return data
            else:
                self.items.head = self.items.head.next
        return None
Ejemplo n.º 3
0
        else:
            new_word = new_word + present_node.get_data()

        present_node = present_node.get_next()
        next_node = next_node.get_next()
        print(present_node.get_data())
        #print(next_node.get_data())

    new_word = new_word + present_node.get_data()
    new_sentence = new_word

    return new_sentence


word_list = LinkedList()
word_list.add("T")
word_list.add("h")
word_list.add("e")
word_list.add("/")
word_list.add("*")
word_list.add("s")
word_list.add("k")
word_list.add("y")
word_list.add("*")
word_list.add("i")
word_list.add("s")
word_list.add("/")
word_list.add("/")
word_list.add("b")
word_list.add("l")
Ejemplo n.º 4
0
    def DS_linkedlist(self):
        try:
            head = int(input(" Enter the Head of your Linked List: "))
        except:
            return self.DS_queue()

        # Linked List instantiation with the given length
        linkedlist = LinkedList(head)

        while True:
            print(
                '\n [LINKED LIST OPERATIONS] \n\n 1. Append\n 2. Prepend\n 3. Display\n 4. Count\n 5. Insert\n 6. Remove\n 7. Reverse\n 8. Back\n'
            )

            try:
                selectedNumber = int(input("Select a number: "))
            except:
                print("\nPlease enter a valid input\n")
                input("Press [Enter] to continue...")
                self.DS_linkedlist()

            if selectedNumber == 1:
                value = int(input("Enter the value that you want to append: "))
                linkedlist.append(value)
                print("Success!")
                input("Press [Enter] to continue...")

            elif selectedNumber == 2:
                value = int(
                    input("Enter the value that you want to prepend: "))
                linkedlist.prepend(value)
                print("Success!")
                input("Press [Enter] to continue...")

            elif selectedNumber == 3:
                linkedlist.display()
                input("Press [Enter] to continue...")

            elif selectedNumber == 4:
                linkedlist.count()
                input("Press [Enter] to continue...")

            elif selectedNumber == 5:
                linkedlist.insert()
                print('Insert Complete')
                input("Press [Enter] to continue...")

            elif selectedNumber == 6:
                value = int(input("Enter the value that you want to remove: "))
                linkedlist.remove(value)
                print("Success!")
                input("Press [Enter] to continue...")

            elif selectedNumber == 7:
                linkedlist.reverse()
                print("Success!")
                input("Press [Enter] to continue...")

            elif selectedNumber == 8:
                self.DS_main()

            else:
                print(
                    "The number you entered is not in the choices... Going back to the main screen instead..."
                )
                input("Press [Enter] to continue...")
                self.DS_main()
Ejemplo n.º 5
0
def main():
    a_list = LinkedList()
    a_list.generate_random()
    print('\nnew list: ', end='')
    a_list.display()
    print(f'list size: {a_list.size()}')
        p2 = LL.head
        found_dup = False
        while p2 is not p1:
            if id(p2.data) == id(p1.data):
                print("we found the same data")
                found_dup = True
                break
        if not found_dup:
            print 'no duplicate yet', p1.data, p1.next.data
            p1 = p1.next
        else:
            print 'duplicate ', p1.data, p1.next.data
            p1 = p1.next
    LL.tail = p1


node_a = Node('data')
node_b = Node('alex')
node_c = Node('alex')
node_d = Node('hello')

node_b.next = node_c
node_c.next = node_d

linked_list = LinkedList()
linked_list.head = node_b
remove_dups_constant_space(linked_list)
print len(linked_list)
print(linked_list.head.data)
print(linked_list.head.next.data)

# ----------------------------- Problem 1: Reverse a LinkedList -----------------------------
def reverse_linked_list(linkedlist):
    current_node = linkedlist.head
    next_node = None
    prev_node = None
    if current_node.next is None:
        return current_node
    while current_node is not None:
        # 1 -> 2 -> 3 -> 4
        # C    N
        # C    NP
        #
        next_node = current_node.next
        current_node.next = prev_node
        prev_node = current_node
        current_node = next_node
    linked_list.head = prev_node
    return linked_list


# ----------------------------- Problem 1: Test -----------------------------
linked_list = LinkedList()
data = [1, 2, 3, 4, 5]
for num in data:
    linked_list.append(num)
print(repr(linked_list))

print("Reversed List:")
print(repr(reverse_linked_list(linked_list)))
Ejemplo n.º 8
0
from DataStructures import LinkedList

ll = LinkedList()
ll.add(1)
ll.add(2)
ll.add(3)
print(ll)

print(ll.remove())
print(ll)
Ejemplo n.º 9
0
 def __init__(self):
     self.items = LinkedList()
Ejemplo n.º 10
0
def LinkedListTail(n):
	LL = LinkedList()
	for i in range(0,n):
		LL.insert_tail(i)
Ejemplo n.º 11
0
	def test_SLL(self, mock_stdout):
		sll = LinkedList(5)

		# check default instantiation values
		self.assertEqual(sll.head.value, 5)
		self.assertEqual(sll.tail.value, 5)
		self.assertEqual(sll.tail.next, None)
		self.assertEqual(sll.length, 1)
		self.assertEqual(sll.count(), 1)

		# check if append() is working properly
		sll.append(6)
		self.assertEqual(sll.head.next.value, 6)
		self.assertEqual(sll.count(), 2)

		# check if prepend is working properly
		sll.prepend(4)
		self.assertEqual(sll.head.value, 4)
		self.assertEqual(sll.head.next.value, 5)
		self.assertEqual(sll.count(), 3)

		# check if traverse to index is working properly
		self.assertEqual(sll.traverseToIndex(1).value, 5)

		# check if insert() working properly
		sll.insert(1, 10)
		self.assertEqual(sll.head.next.value, 10)

		# Check if display() is working properly
		sll.display()
		self.assertEqual(mock_stdout.getvalue(), '4-->10-->5-->6-->\n')

		# reset stdout
		mock_stdout.seek(0)
		mock_stdout.truncate(0)

		# Check if remove() is working properly
		sll.remove(5)
		self.assertEqual(sll.head.next.value, 10)

		# Test reverse()
		sll.reverse()
		sll.display()
		self.assertEqual(mock_stdout.getvalue(), '6-->10-->4-->\n')

		# Test count() again
		self.assertEqual(sll.count(), 3)
Ejemplo n.º 12
0
def LinkedListHead(n):
	LL = LinkedList()
	for i in range(0,n):
		LL.insert_head(i)
#  __Date__     : 1/5/2020.
#  __Author__   :  CodePerfectPlus
#  __Package__  :  Python 3
#  __GitHub__   : https://www.github.com/codeperfectplus
#
from DataStructures import LinkedList

myList = LinkedList()

myList.insertStart(11)
myList.insertEnd(19)
myList.insertEnd(10)

myList.treaverse()
print(f'Linked List Size : {myList.size()}')
Ejemplo n.º 14
0
    next_node = present_node.get_next()

    while (present_node.get_next() != None):

        data1 = present_node.get_data()
        data2 = next_node.get_data()

        if data1 == data2:
            print(data1)
            duplicate_list.delete(data1)
            present_node = next_node
            next_node = next_node.get_next()

        if data1 != data2:
            present_node = next_node
            next_node = next_node.get_next()

    duplicate_list.display()
    return duplicate_list


#Add different values to the linked list and test your program
duplicate_list = LinkedList()
duplicate_list.add(30)
duplicate_list.add(40)
duplicate_list.add(40)
duplicate_list.add(40)
duplicate_list.add(40)

print(remove_duplicates(duplicate_list))
Ejemplo n.º 15
0
 def setUp(self):
     self.head = Node(0)
     self.linkedlist = LinkedList(self.head)
     for i in range(1, 6):
         self.linkedlist.insert(i)
Ejemplo n.º 16
0
class TestLinkedList(unittest.TestCase):
    
    def setUp(self):
        self.head = Node(0)
        self.linkedlist = LinkedList(self.head)
        for i in range(1, 6):
            self.linkedlist.insert(i)
            
    def tearDown(self):
        self.linkedlist = None

    def test_len(self):
        self.assertEqual(len(self.linkedlist), 6)
        for i in range(6, 50):
            self.linkedlist.insert(i)
        self.assertEqual(len(self.linkedlist), 50)

    def test_search(self):
        self.assertTrue(self.linkedlist.search(0))
        self.assertTrue(self.linkedlist.search(2))
        self.assertTrue(self.linkedlist.search(5))
        self.assertFalse(self.linkedlist.search(99))
        
    def test_delete_from_middle(self):
        self.linkedlist.delete(self.head.next.next.next)
        self.assertEqual(self.linkedlist._traverse(), '0 -> 1 -> 2 -> 4 -> 5 -> None')
        self.assertEqual(self.linkedlist.size, 5)
        
    def test_delete_head(self):
        self.linkedlist.delete(self.linkedlist.head)
        self.assertEqual(self.linkedlist._traverse(), '1 -> 2 -> 3 -> 4 -> 5 -> None')
        self.assertEqual(self.linkedlist.size, 5)
        
    def test_delete_tail(self):
        self.linkedlist.delete(self.linkedlist.tail)
        self.assertEqual(self.linkedlist._traverse(), '0 -> 1 -> 2 -> 3 -> 4 -> None')
        self.assertEqual(self.linkedlist.size, 5)

    def test_detect_cycle_false(self):
        self.assertFalse(self.linkedlist.is_cycle())
        
    def test_detect_cycle_true(self):
        self.linkedlist.tail.next = self.linkedlist.head
        self.assertTrue(self.linkedlist.is_cycle())

    def test_reverse(self):
        self.assertEqual(self.linkedlist._traverse(), '0 -> 1 -> 2 -> 3 -> 4 -> 5 -> None')
        self.linkedlist.reverse()
        self.assertEqual(self.linkedlist._traverse(), '5 -> 4 -> 3 -> 2 -> 1 -> 0 -> None')
        
    def test_get_mid(self):
        self.assertEqual(self.linkedlist.get_mid().val, 3)
        self.linkedlist.delete(self.linkedlist.head)
        self.linkedlist.delete(self.linkedlist.head)
        self.linkedlist.delete(self.linkedlist.head)
        self.assertEqual(self.linkedlist.get_mid().val, 4)