Esempio n. 1
0
def is_sorted(lk: LinkedList):
    n = lk.get(0)
    for i in range(1, lk.size()):
        j = lk.get(i)
        if n > j:
            return False
        n = j
    return True
Esempio n. 2
0
 def test_insertion_sort2(self):
     lk = LinkedList()
     lk.add(2)
     lk.add(3)
     lk.add(1)
     lk.insertion_sort()
     assert lk.get(0) == 1
     assert lk.get(1) == 2
     assert lk.get(2) == 3
Esempio n. 3
0
 def test_get_raise(self):
     lk = LinkedList()
     lk.add("1")
     lk.add("2")
     lk.add("3")
     with raises(IndexError):
         lk.get(10)
     with raises(IndexError):
         lk.get(-1)
Esempio n. 4
0
 def test_set(self):
     lk = LinkedList()
     lk.add("1")
     lk.add("2")
     lk.add("3")
     lk.set("4", 1)
     assert lk.get(0) == "1"
     assert lk.get(1) == "4"
     assert lk.get(2) == "3"
Esempio n. 5
0
 def test_raise_when_bad_index(self):
     lk = LinkedList()
     lk.add("1")
     lk.add("2")
     lk.add("3")
     with raises(IndexError):
         lk.set("4", -1)
     with raises(IndexError):
         lk.set("4", 10)
Esempio n. 6
0
 def test_merge_sort2(self):
     lk = LinkedList()
     lk.add(2)
     lk.add(3)
     lk.add(1)
     lk.merge_sort()
     assert lk.get(0) == 1
     assert lk.get(1) == 2
     assert lk.get(2) == 3
Esempio n. 7
0
 def test_set_first_and_last(self):
     lk = LinkedList()
     lk.add("1")
     lk.add("2")
     lk.add("3")
     lk.set("4", 0)
     lk.set("5", 2)
     assert lk.get(0) == "4"
     assert lk.get(1) == "2"
     assert lk.get(2) == "5"
 def test_is_empty(self):
     lk = LinkedList()
     assert lk.is_empty() == True
     lk.add("1")
     assert lk.is_empty() == False
     lk.clear()
     assert lk.is_empty() == True
Esempio n. 9
0
 def test_dichotomic_search_minus_one(self):
     lk = LinkedList()
     lk.add(1)
     lk.add(3)
     lk.add(2)
     # Il faut préalablement trier le tableau
     lk.merge_sort()
     assert lk.dichotomic_search(4) == -1
 def test_add_element_in_center(self):
     lk = LinkedList()
     values = ["1", "2", "3", "4"]
     for value in values:
         lk.add(value)
     lk.add("5", 2)
     assert lk.size() == 5
     assert lk.get(0) == values[0]
     assert lk.get(1) == values[1]
     assert lk.get(2) == "5"
     assert lk.get(3) == values[2]
     assert lk.get(4) == values[3]
Esempio n. 11
0
 def test_dichotomic_search2(self):
     lk = LinkedList()
     lk.add(2)
     lk.add(3)
     lk.add(1)
     # Il faut préalablement trier le tableau
     lk.merge_sort()
     assert lk.dichotomic_search(3) == 2
     assert lk.dichotomic_search(2) == 1
 def test_clear(self):
     lk = LinkedList()
     values = ["1", "2", "3", "4"]
     for value in values:
         lk.add(value)
     lk.clear()
     assert lk.size() == 0
     with raises(IndexError):
         lk.get(0)
 def test_remove_with_value_when_multiple_nodes(self):
     lk = LinkedList()
     values = ["1", "2", "3", "4"]
     for value in values:
         lk.add(value)
     lk.remove("3")
     assert lk.size() == 3
     assert lk.get(0) == values[0]
     assert lk.get(1) == values[1]
     assert lk.get(2) == values[3]
Esempio n. 14
0
 def test_get(self):
     lk = LinkedList()
     lk.add("1")
     lk.add("2")
     lk.add("3")
     assert lk.get(0) == "1"
     assert lk.get(1) == "2"
     assert lk.get(2) == "3"
 def test_raise_when_bad_index(self):
     lk = LinkedList()
     values = ["1", "2", "3", "4"]
     for value in values:
         lk.add(value)
     with raises(IndexError):
         lk.remove(None, 10)
     with raises(IndexError):
         lk.remove(None, -1)
 def test_add_multiple_elements_to_end_with_index(self):
     lk = LinkedList()
     values = ["1", "2", "3", "4"]
     for value in values:
         lk.add(value, lk.size())
     assert lk.size() == 4
     assert lk.get(0) == values[0]
     assert lk.get(1) == values[1]
     assert lk.get(2) == values[2]
     assert lk.get(3) == values[3]
Esempio n. 17
0
 def test_contains(self):
     lk = LinkedList()
     lk.add("1")
     lk.add("2")
     lk.add("3")
     assert lk.contains("1") == True
     assert lk.contains("0") == False
Esempio n. 18
0
 def test_index_of(self):
     lk = LinkedList()
     lk.add("1")
     lk.add("2")
     lk.add("3")
     assert lk.index_of("1") == 0
     assert lk.index_of("3") == 2
 def test_raise_when_no_value(self):
     lk = LinkedList()
     values = ["1", "2", "3", "4"]
     for value in values:
         lk.add(value)
     with raises(ValueError):
         lk.remove("5")
 def test_remove_with_index(self):
     lk = LinkedList()
     value = "1"
     lk.add(value)
     lk.remove(None, 0)
     assert lk.size() == 0
Esempio n. 21
0
def populate(lk: LinkedList, n: int):
    for i in range(0, n):
        lk.add(random.randint(0, 1000))
Esempio n. 22
0
import random

from src.metier.LinkedList import LinkedList

if __name__ == '__main__':
    # Vos tests ici
    lk = LinkedList()
    # Ajouter des nombres aléatoires dans la liste
    for i in range(0, 10):
        lk.add(random.randint(0, 1000000))
    # Afficher la liste
    print(lk)
Esempio n. 23
0
        n = j
    return True


def current_time():
    return int(round(time.time() * 1000))


def populate(lk: LinkedList, n: int):
    for i in range(0, n):
        lk.add(random.randint(0, 1000))


if __name__ == '__main__':
    # Vos tests ici
    lk = LinkedList()
    n: int = 1000

    print("========== BAD INSERTION SORT ==========")
    lk.clear()
    populate(lk, n)
    t = current_time()
    lk.bad_insertion_sort()
    print("Temps : {} ms\n".format(current_time() - t))
    print("Tableau trié ? ", is_sorted(lk))

    print("========== INSERTION SORT ==========")
    lk.clear()
    populate(lk, n)
    t = current_time()
    lk.insertion_sort()
 def test_add_first_element_without_index(self):
     lk = LinkedList()
     value = "1"
     lk.add(value)
     assert lk.size() == 1
     assert lk.get(0) == value
Esempio n. 25
0
 def test_insertion_sort3(self):
     lk = LinkedList()
     lk.add(2)
     lk.insertion_sort()
     assert lk.get(0) == 2
 def test_remove_with_value(self):
     lk = LinkedList()
     value = "1"
     lk.add(value)
     lk.remove("1")
     assert lk.size() == 0
Esempio n. 27
0
 def test_merge_sort3(self):
     lk = LinkedList()
     lk.add(2)
     lk.merge_sort()
     assert lk.get(0) == 2
Esempio n. 28
0
 def test_index_of_return_minus_one(self):
     lk = LinkedList()
     lk.add("1")
     lk.add("2")
     lk.add("3")
     assert lk.index_of("0") == -1
 def test_raise_on_bad_index(self):
     lk = LinkedList()
     with raises(IndexError):
         lk.add("1", -1)
     with raises(IndexError):
         lk.add("1", 1)
Esempio n. 30
0
 def test_to_str(self):
     lk = LinkedList()
     lk.add("1")
     lk.add("2")
     lk.add("3")
     assert str(lk) == "1 <-> 2 <-> 3"