From 7c1b7a4da822a9c6e22c422b338a26c6d47121c2 Mon Sep 17 00:00:00 2001 From: MsDiala Date: Mon, 7 Dec 2020 21:30:18 +0200 Subject: [PATCH 1/2] ffff --- .../__pycache__/array_search.cpython-38.pyc | Bin 501 -> 501 bytes .../challenges/array_reverse/array_search.py | 14 --- .../challenges/array_reverse/array_shift.py | 6 - .../__pycache__/linked_list.cpython-38.pyc | Bin 411 -> 1285 bytes .../linked_list/linked_list.py | 112 +++++++++++++++++- ...t_array_search.cpython-38-pytest-6.1.2.pyc | Bin 4927 -> 174 bytes ...st_array_shift.cpython-38-pytest-6.1.2.pyc | Bin 2575 -> 173 bytes tests/challenges/test_array_search.py | 66 ----------- ...st_linked_list.cpython-38-pytest-6.1.2.pyc | Bin 0 -> 10738 bytes tests/data_structures/test_linked_list.py | 108 ++++++++++++++++- 10 files changed, 209 insertions(+), 97 deletions(-) diff --git a/data_structures_and_algorithms/challenges/array_reverse/__pycache__/array_search.cpython-38.pyc b/data_structures_and_algorithms/challenges/array_reverse/__pycache__/array_search.cpython-38.pyc index 5d1378ae7138cb99ca926457d1664e5bcf38efba..679e8e4818865e50253999c1b73981b5bc2aa9e5 100644 GIT binary patch delta 20 acmey${FRwIl$V!_0SNrJpW4X%iV*-ie+BFS delta 20 acmey${FRwIl$V!_0SLlo9oxwLiV*-h?gi2S diff --git a/data_structures_and_algorithms/challenges/array_reverse/array_search.py b/data_structures_and_algorithms/challenges/array_reverse/array_search.py index 69aa2f4..e69de29 100644 --- a/data_structures_and_algorithms/challenges/array_reverse/array_search.py +++ b/data_structures_and_algorithms/challenges/array_reverse/array_search.py @@ -1,14 +0,0 @@ -def binary_search(arr, key): - low = 0 - high = len(arr) - while len(arr) > 0: - middle = (low+high) // 2 - if arr[middle] == key: - return middle - if arr[middle] < key: - low= middle - if arr[middle] > key: - high= middle - if middle == 0 or middle == len(arr) - 1: - return -1 - return -1 \ No newline at end of file diff --git a/data_structures_and_algorithms/challenges/array_reverse/array_shift.py b/data_structures_and_algorithms/challenges/array_reverse/array_shift.py index 0b06801..139597f 100644 --- a/data_structures_and_algorithms/challenges/array_reverse/array_shift.py +++ b/data_structures_and_algorithms/challenges/array_reverse/array_shift.py @@ -1,8 +1,2 @@ -def array_shift(l,b): - middle = len(l) // 2 - if len(l) % 2 == 0: - return l[:middle] + [b] + l[middle:] - else: - return l[:middle + 1] + [b] + l[middle +1:] diff --git a/data_structures_and_algorithms/data_structures/linked_list/__pycache__/linked_list.cpython-38.pyc b/data_structures_and_algorithms/data_structures/linked_list/__pycache__/linked_list.cpython-38.pyc index d4bbf7a9f6c6d01de5b799f24975027f48c2d272..b75bc0a76b3950ecf70717b131ea291a3cf7cb35 100644 GIT binary patch literal 1285 zcmbtU&2G~`5Z+zeX=<8MfrKiRgRefM;l>3a#E&>o%t9dA$@k_+iu z@C3c_Bs{=gIrSAdF|#H(6mE>PvopIhJAa@3w$}?8(r)-oeie*;CuNg7RG#6gBV@0c zVv1kkT8R_Vgz`}HP~*x!W^s_B)gOd!HvSJ$&SD zJy3a4BmrwKfN?rRXQu#T1m8$t<~-8L6MhZtz?nc zW>Y?#UfQmdd6^q2sgl}9#(0O%dsOY=`Yz>k{)u<_0J-5IijKkIA^qDS^g02 zVw4-|_K?vd?<1ow z`T{%2LxHdm9kIu|Jmm9(jlSOYMo0q90d|P>h&l8fkkrns9o#Nn5H#25+X6blpMgW6 UQ~u>=yH6UhKc!~6&trG{CojAWg8%>k delta 216 zcmZqWn$64?%FD~e00hTnkH!}=OyrZP7XtF!8B!Qh7+Zk2nJJ1Pg(;XplQ|ir3>h#2 zISxQvEC3`@8KM|d7^0X`7^9dW>ThxRWaee3rubwQmsH7cDF8u0X^BEgesXb1QD$Dc zLPly)Dp=T0ljRmie0*MFZfbn|WF{sl{#)$v@j!*~@hcgM*ccclK4_LH0$B&b#VkOA cgOP<5EaIohSj0GaA&dCr4=jAFpMg>g0OS2FB>(^b diff --git a/data_structures_and_algorithms/data_structures/linked_list/linked_list.py b/data_structures_and_algorithms/data_structures/linked_list/linked_list.py index d15924f..e31601a 100644 --- a/data_structures_and_algorithms/data_structures/linked_list/linked_list.py +++ b/data_structures_and_algorithms/data_structures/linked_list/linked_list.py @@ -1,7 +1,109 @@ +class Node: + def __init__(self, value): + self.value = value + self.next = None + + def __repr__(self): + return f'{self.value}' + + class LinkedList: - """ - Put docstring here - """ + def __init__(self): + self.head = None + self.tail = None + + self.length = 0 + + '''Adds node to head of linked list''' + + def insert(self, value): + node = Node(value) + node.next = self.head + self.head = node + self.length += 1 + + '''Takes in a target value and iterates through Linked List + until the value is found, or the end of the list is reached + returns a boolean''' + + def includes(self, value): + current = self.head + + while current: + if current.value == value: + return True + current = current.next + + return False + + def __str__(self): + values = '' + current = self.head + while current: + values += f'Node: {str(current.value)} ' + current = current.next + return values + + '''Iterates to the end of a Linked List and appends a value''' + + def append(self, value): + new_node = Node(value) + if self.head == None: + self.head = new_node + return + last = self.head + + while last.next: + last = last.next + + last.next = new_node + + + def insertBefore(self, exisiting_value, new_value): + new_node = Node(new_value) + current = self.head + + while current.next: + if current.next.value == exisiting_value: + new_node.next = current.next + current.next = new_node + return + else: + current = current.next + + + def insertAfter(self, existing_value, new_value): + new_node = Node(new_value) + current = self.head + while current: + if current.value == existing_value: + + old_node = current.next + current.next = new_node + new_node.next = old_node + + return + else: + current = current.next + + + def nthFromEnd(self, n=0): + if type(n) != int: + return 'Exception' + if n > self.length() or n < 0: + return 'Exception' + current = self.head + counter = 0 + while current.next: + current = current.next + counter+=1 + if counter > n: + current = current.next + return current.value + + + + + + - # put your LinkedList implementation here - pass diff --git a/tests/challenges/__pycache__/test_array_search.cpython-38-pytest-6.1.2.pyc b/tests/challenges/__pycache__/test_array_search.cpython-38-pytest-6.1.2.pyc index bc729e61381f84534f08bc58d4ec72a2e5c8b1cd..ca2a0050acf90ae059413530ed27788f1e7d8bcf 100644 GIT binary patch delta 91 zcmdn5wvN$0l$V#wp6gsZ0}wn05y*fM$Z-JTViq8g!Vt`$$>_I|p$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+R)Cd`bIssWeLN?8mT~cZrp=G(EiA4O4c=t$b*U+-O z5SWKA0pu~92$rnywZUqbuOpRTmEtbaZM*`APX)Q?oaA{@zsXs;HO$f>bFzk6`sy$- zUk!6|DeegAiRdVN+oRrEsO(#Eq9{3=k-W>6y!n&7C6T;9N{-i()gXDUNPhZ~$1;+e ksw`C{sOHs9m$n>njS~s)6pX1=wNlE=;G0#JYi3yc2e2;`Z~y=R diff --git a/tests/challenges/__pycache__/test_array_shift.cpython-38-pytest-6.1.2.pyc b/tests/challenges/__pycache__/test_array_shift.cpython-38-pytest-6.1.2.pyc index 7a7047610706ab23fda9119f295c8455045495f1..01fa727f28e3c372c932634b6c2e0a1468d7cfba 100644 GIT binary patch delta 90 zcmeAdS<7f2%FD~ezvEOq0}wn05y*fM$Z-JTViq8g!Vt`$$>_I|p$H@lB_{VX#R}Tw Q=BJeAq}qYRKLarX0Bqn6OaK4? literal 2575 zcmds(OOG2x5XZZFo_=Pti4Y(j5h8#=YeQIWEDDOaaM%Nn2w9OPz3%qd%GhIdk2kw3 zn?r*55D1}c4w0M#2fhPeqOY9%74}3`&+ORR5JxDSu;uEW?wXmdU)R6O-!~f7-FN7fW9N#pGY?pp75cqh(LtU zRz*crp{T@hx^Blnct^Yv|* z_h$5zs=Lthu{UNS6qVBB$a)a)cwjxC2Obd8uj~~vcQB}#Go;XWvj9B8nR`kf(gy%* z&IExF8~aNNt6n0~B?hx}(70e0^o;H8E$GSi^$#Y;AGh?iy}fJLz0uOf=e=yo6BEj# ziR@)kESPb?s|)7FS(9rmRc5_+!5ap57ZrssG1Z%s<4o!-;>ui(dOS%YB`2zvjwW15 zQ-|s3U^+~);TT?%q~M{=tAIY8Cc?T#agrYJBpUOPG?hs6zQiY(=9l)Z!*u+WQmHam zqJFAIJd0qB2_N$eW)_~R!zxq_mr}!b<5+4t0D+ge)Seuh(9%f9 z67xuJfg!X>w`oXRa+lOelc={~?0=SahH^s>3_UTlXQ)sigs7@$aBK2SYGCgY8l<8w zqj?<-?niFkgkC`~yPx6>|4i^*n0Y?IKVWtxT;a!lDP0FojMx+I1@S%wIWdqeiZ?99 zyOL(tN)yE^tTc1+f;3C<&a4ND_h;*oi?=Fjg?QmCdVzSG`wG>nP_4>G^EMjHBZXRi zDX3MOmf1i_c?F(TVBMlu*Aw$P%se0K6PUGFY3vk0Y0Lz>fbus8AI?mR(k)SXC8&K1 z3Q_)GLFFi6TS||K^+1#-)+0yhi=aRWXXa*8P2?B&2f=Q4NVZAc3w`)j;OqYlUByOm diff --git a/tests/challenges/test_array_search.py b/tests/challenges/test_array_search.py index 0ec091d..e69de29 100644 --- a/tests/challenges/test_array_search.py +++ b/tests/challenges/test_array_search.py @@ -1,66 +0,0 @@ -from data_structures_and_algorithms.challenges.array_reverse.array_search import binary_search - -# "Happy Path" cases -def test_simple(): - expected = 3 - actual = binary_search([1,2,3,4,5,6],4) - assert actual == expected - -def test_original_nums(): - expected = 2 - actual = binary_search([4,8,15,16,23,42], 15) - assert actual == expected - -def test_absence(): - expected = -1 - actual = binary_search([11,22,33,44,55,66,77], 90) - assert actual == expected - -# Expected Failure -def test_num_in_left(): - expected = 0 - actual = binary_search([11,22,33,44,55,66,77,89,100], 11) - assert actual == expected - -def test_num_in_right(): - expected = 8 - actual = binary_search([11,22,33,44,55,66,77,89,100], 100) - assert actual == expected - -def test_num_in_middle(): - expected = 4 - actual = binary_search([11,22,33,44,55,66,77,89,100], 55) - assert actual == expected - -def test_num_not_in_right(): - expected = -1 - actual = binary_search([11,22,33,44,55,66,77,89,100], 101) - assert actual == expected - -def test_num_not_in_left(): - expected = -1 - actual = binary_search([11,22,33,44,55,66,77,89,100], 10) - assert actual == expected - - - -# Edge Cases -def test_two_element_arr(): - expected = 1 - actual = binary_search([10, 34], 34) - assert actual == expected - -def test_two_element_arr_anbsence(): - expected = -1 - actual = binary_search([10, 34], 19) - assert actual == expected - -def test_empty_array(): - expected = -1 - actual = binary_search([], 19) - assert actual == expected - -def test_empty_array_negative(): - expected = -1 - actual = binary_search([], -1) - assert actual == expected \ No newline at end of file diff --git a/tests/data_structures/__pycache__/test_linked_list.cpython-38-pytest-6.1.2.pyc b/tests/data_structures/__pycache__/test_linked_list.cpython-38-pytest-6.1.2.pyc index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..1d5ef0ed7f09933225763bac87327f47167fca32 100644 GIT binary patch literal 10738 zcmdT~&2JmW72hu|mrGJ#{)nA8iJB&@*~YS|FWYHsI7#iaX{x6AYRUwmDa}fvO_9Xx z${*^oK%1P39(*W@9)v&!iU8^Nv*a#K$+DC5 zLjq@JXNKJOcHaB_-n`k@ibeP1pZukI)YY_qY6E`;t}o!@zlTC-LT_jlooi#wG&EgX zvnm;#MvR8NmaSyhoQi{MQ&{h6m7K^38?`I4!aQ zF)k)hkBI}~AnI{(NE}8zAs!G%P#+LS#e=91iigB8)Q7~w;t|w`h5m|GdUOXeY6~T! zYb~^d*L7d4H?Ml)#kwCX(MbGfqmjkuIeh$Ipa`^gw2l^PSM&~a>lmRP8dr^u8JhmP zfw5%@Jv5hf!Trm|ZA+MewUxQ8UA6s77`H?1wk}^nD@(1WfpKcbsg>j8tx8zzasCqZrjasu7 zV5DSATAFU2ZWGB`PF~C=FJ_{P=vU^G7iYFLc@)3%>6<54TWj7)QLi;>Cq*r&o$v#> zj)x;X|3s}RPShGJEm;p%*ZdREd$rX(Ny_^?x7we}z10RA6DmKLYTxWm@N=T8>XP(W zetMAp1PV>JbsOvH;Ip6=v=L1{gx-`97SWNzhmG(&+$=W2EnUEHbeJPrFdfx0qt+Fp zqlfwxG7=L8YJ?^X)e0^D12Rd077R5*{cNaqU~gsFP_JOzVM7hj$|*y&xlM*jeYhLf zxR=OoT4bcc5in6USUYernh)&(gEe5Z7G%+sYy~)w0SPT(bh4opW+9;yI=6KHGQLkT zI*`$&k+h7MF@YH@<6(|9 zC@2jIyJ-L|@*S7qVq#%OhdJ#KVDFG6N(%+R<9R}g*%&;~P~fpAo=jb!4#5xm;g@zLAbCh(r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8hP? zsdUe8alUC%<`ZCs(@>S}agex)&Q4#(xE+8@`-=jhY}VW#kt87=jZV`jnA1l&2x$%1|f9> zCTOTY7_mNQ5RuYPWDs$L&^3`l>E`RrdVs@6I)GM(CKa5Qlx6%eOE+nT?;0(u#-Boy_{2PF|cHw2-69uUPK-hF86g<552IB&St}n`4~# zq2y0Hvi~Z^QWitcl0MJTmxfskD3a`j5WP-*(7i zBFk82D~R{X1Ly8MM*Dv1r<6}lrF>|=9~~2B)j9kgxpVfyLc(I?522W>py=i<-dOV5 zbbQU$f{oKlWGzF^k$P;@CnFfpR5Mt;BwK42n>fZFo{#3G9Y`i^N9XnxxBvD1+6wka zWGjOZK23d$2wAYr!AEXRia-$TZ_#(z5Pi(N4MV@#EM#mn=HD%sh_QWh>cAR;O z-iWzN=8e5EiQj#1jI#Qdp==D@FOxGOsEVK)4F%o%amEw$U4)YfY^T%*uVQKN(3uax zImeKW$8aMK*m#{0+6{9(+IB|?0CK`)pB#MK`Mw;FVUP8I%y^UY+>CiUz zMw?$l=^q8jWsIdPC56_%$0%9K3QiJx??fffIMbf^Ypka^BsCvf$Lko6tm87Td}JLN zZYLO=uah`EEHxil$K7x`r*N8LuqcIh@2(dew6W{a3qGx7dhdZ8Re7L04Chw%e6D{| z&ozph=shrzUCAb%v6JA!^f_|mfp`rE9_aSCFyD)khI?TCuFtrjxDfB)-6I!{4&}lf zf}9bGsckCS zXn?7wq<0U__y&HbsSu)|9=C>9DPy% literal 0 HcmV?d00001 diff --git a/tests/data_structures/test_linked_list.py b/tests/data_structures/test_linked_list.py index 88ec27d..c823227 100644 --- a/tests/data_structures/test_linked_list.py +++ b/tests/data_structures/test_linked_list.py @@ -1,8 +1,104 @@ -from data_structures_and_algorithms.data_structures.linked_list.linked_list import ( - LinkedList, -) +from data_structures_and_algorithms.data_structures.linked_list.linked_list import Node, LinkedList +def test_list_creation(): + actual = LinkedList() + assert actual.head == None -def test_instance(): - ll = LinkedList() - assert isinstance(ll, LinkedList) +def test_insert_method(): + lst_one = LinkedList() + lst_one.insert(1) + lst_one.insert(2) + lst_one.insert(3) + + assert lst_one.head.value == 3 + assert lst_one.head.next.value == 2 + assert lst_one.head.next.next.value == 1 + +def test_includes_method(): + lst_two = LinkedList() + lst_two.insert('apples') + lst_two.insert('pickles') + lst_two.insert('chips') + + assert lst_two.includes('pickles') == True + assert lst_two.includes('whales') == False + + + + +def test_to_string_method(): + lst_three = LinkedList() + lst_three.insert(1) + lst_three.append(2) + lst_three.append(3) + lst_three.append(4) + lst_three.append(5) + + actual = lst_three.__str__() + expected = 'Node: 1 Node: 2 Node: 3 Node: 4 Node: 5 ' + assert actual == expected + +def test_append(): + lst_four = LinkedList() + lst_four.insert('apple') + lst_four.append('banana') + assert lst_four.head.next.value == 'banana' + +def test_insert_before(): + lst_five = LinkedList() + lst_five.insert(1) + lst_five.append(2) + lst_five.append(3) + lst_five.append(5) + lst_five.insert_before(5, 4) + + assert lst_five.head.next.next.value == 3 + assert lst_five.head.next.next.next.value == 4 + assert lst_five.head.next.next.next.next.value == 5 + +def test_insert_after(): + lst_six = LinkedList() + lst_six.insert(1) + lst_six.append(2) + lst_six.append(3) + lst_six.append(4) + lst_six.append(6) + lst_six.insert_after(4, 5) + + assert lst_six.head.next.next.value == 3 + assert lst_six.head.next.next.next.value == 4 + assert lst_six.head.next.next.next.next.value == 5 + assert lst_six.head.next.next.next.next.next.value == 6 + + +def test_kth_method(): + lst_seven = LinkedList() + lst_seven.insert(1) + lst_seven.append(2) + lst_seven.append(3) + lst_seven.append(4) + lst_seven.append(5) + +def test_initialize_with_list(): + initialList = LinkedList([5,6,7,8]) + assert str(initialList) == '5 ->6 ->7 ->8' + +def test_nth_greater_than(): + initialList = LinkedList([5,6,7,8]) + assert initialList.nthFromEnd(8) == 'Exception' + +def test_nth_last(): + initialList = LinkedList([5,6,7,8]) + assert initialList.nthFromEnd(4) == 5 + +def test_nth_negative(): + initialList = LinkedList([5,6,7,8]) + assert initialList.nthFromEnd(-2) == 'Exception' + +def test_nth_list_of_one(): + initialList = LinkedList([5]) + assert initialList.nthFromEnd(0) == 5 + +def test_nth_average_use(): + initialList = LinkedList([5,6,7,8,9,10,11]) + assert initialList.nthFromEnd(3) == 8 \ No newline at end of file From c2c130f0a967eb9e53ed8a3d032ae38eca8307f8 Mon Sep 17 00:00:00 2001 From: MsDiala Date: Tue, 8 Dec 2020 06:50:29 +0200 Subject: [PATCH 2/2] g --- .../__pycache__/linked_list.cpython-38.pyc | Bin 1285 -> 2976 bytes .../linked_list/linked_list.py | 58 ++++++++++++++---- ...st_linked_list.cpython-38-pytest-6.1.2.pyc | Bin 10738 -> 9630 bytes tests/data_structures/test_linked_list.py | 53 ++++++++-------- 4 files changed, 72 insertions(+), 39 deletions(-) diff --git a/data_structures_and_algorithms/data_structures/linked_list/__pycache__/linked_list.cpython-38.pyc b/data_structures_and_algorithms/data_structures/linked_list/__pycache__/linked_list.cpython-38.pyc index b75bc0a76b3950ecf70717b131ea291a3cf7cb35..5f98fe4a837ec9214ddc32ae0171e70ff6d33d1c 100644 GIT binary patch literal 2976 zcmaJ@&yUka6rLH69VeUYQhqF`qV1Ltx4H z3tQ*39Qz-jO6-xprgvX^+W(;9)bGvYhqHm&n)hZry}-_TMAw!fya7iX&en6(>yShRErjP9XjmmxIka>cqr zaV1`|uBSXDp{4TiZcxh3Q0YFsF}tWw?LJy|2BP2vi+~ssJ3VG^c&9B&|F>Q;)}>7K z^BhB&>Ex;Lu>1`(0o2<+c6NvRdPl{*q_?Abd2c(*&A6YBjn1}vsoL%(&xR(>clWdH z(TjX{nA%m*VpW7y(PGu<>767__q2+VILoX3>!TNC7)5a!=TSsRG728!0Y7XJ+Uv8q zKBwo?i~7_o>=2!T4cIHdHMB*WJ8aze7+HW$)G(yej2@XNBH$UVXN$AkOsDPBvb49a zqo`~|(f&}46Y4jk=wRGS+>D`<4N%OFlOZSr$ikdft#BVxqD4=N_RHA@LdJI?)g6$4@-%oS#< z5HOkV33EyD#|$Ge5eI=1@TS-K=^<>gM$!(}(j_Bn{jo7R%}?7I#%VtpE1i9W*Rm!A zTq`7RaLL1E2hUb6#hXqex7> zR{TC)xa4>OF?akO45~~~3~s;dxI*k8_DsCO8whiQiHNXD^JMx|4Dfqni#Tda{lW9T9_8^ct)RxQ zPyS-0OD_gSa{s~$w&qN8wdASgZCSJ4(B^a~8)?4#&YJ`e5@W5Mn^*cHLk@n2ZDsha`v7aM^S8Ut<;P&@GM^6^twQi!d*zJ%Oh5Q4$fwP9FLW+g z#eI7(@u#n?>7IBQyA&u$j!6dBgoJfi%0oy+!aIP{GUbT~`94cUKS&@ET?cm!55)u? zU>0vgA`)0(6F6a4LF&x1`bWlE?gW5YP0gJuE>f3YAdGg-#_YKuEQLHl;>$;^fw}+87Xzx>T(OxZsZkB5d z5tTNX+lYsxAVSX;vCx>aQN^xl*P0mL)u?Sv>99oJ!3u$a2M+g$0r$b@9xs9q0gTqp zBi&WuIZd_wKf6lZ=P4H#Ebself.length() or n<0: + return'Exception' + current= self.head + counter= 0 + while current.next(): + current= current.next + counter+=1 + if counter>n: + current= current.next + return current.value + - def nthFromEnd(self, n=0): - if type(n) != int: - return 'Exception' - if n > self.length() or n < 0: - return 'Exception' - current = self.head - counter = 0 - while current.next: - current = current.next - counter+=1 - if counter > n: - current = current.next - return current.value @@ -107,3 +119,23 @@ def nthFromEnd(self, n=0): + + + + + +node1 = Node(1) +node2 = Node(2) +node3 = Node(3) +node4 = Node(4) +node5 = Node(5) +ll = LinkedList() +ll.insert(node1) +ll.insert(node2) +ll.insert(node3) +ll.insertBefore(node2,node4) +ll.insertAfter(node3,node5) +print(ll) + + + \ No newline at end of file diff --git a/tests/data_structures/__pycache__/test_linked_list.cpython-38-pytest-6.1.2.pyc b/tests/data_structures/__pycache__/test_linked_list.cpython-38-pytest-6.1.2.pyc index 1d5ef0ed7f09933225763bac87327f47167fca32..b50a0612181e3f1006140a9f02d145ebd84217f2 100644 GIT binary patch delta 1740 zcmah}O>7%Q6rR~X&;Hnn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Mq``(eKX(A%Cn+6!?D?W6q=yXgQOgqWm5bQofal2ej1 zvMRyfj3YPHPwsw0NQ1vCPmwufJz}FOw86+e2e5H(^+#+jl)bUnlrl88AdW}>ip9B&_LL*f3e^H-j!+%*bMlHx z4PU(-S&`;7? zRq-J3^JH5sHX|y*PQ%-->IeqFa_oup?*HA!a4ZBn>`K7|b^Ci!BREAH{@=3v zV^sougYWu3HmMT3fH!6l+Wajhv<1b>FBCi1G| zWC$1FekiR4A)ioM=CtUvYYQYL@?e@^5GXAj4I7%tZQOjo?QUI3pufAxaHX44Dshf*-|FJ;Z|jrLhs`B2IHx6u?S!uChMZf-w3JotqxE-Mt>{AJIbg9Wi_uqJ%NfGukCka2qW+gsl!1AKgJ zw1giE@Y@1F5nKeE#)=3yyhW~$zC?`a#U*-q!99emdT9Vd>XJHTLp3ZT3jZ2hal?Rq G^Z!o)0gCJZ diff --git a/tests/data_structures/test_linked_list.py b/tests/data_structures/test_linked_list.py index c823227..7a8e85b 100644 --- a/tests/data_structures/test_linked_list.py +++ b/tests/data_structures/test_linked_list.py @@ -1,4 +1,5 @@ from data_structures_and_algorithms.data_structures.linked_list.linked_list import Node, LinkedList +import pytest def test_list_creation(): actual = LinkedList() @@ -50,7 +51,7 @@ def test_insert_before(): lst_five.append(2) lst_five.append(3) lst_five.append(5) - lst_five.insert_before(5, 4) + lst_five.insertBefore(5, 4) assert lst_five.head.next.next.value == 3 assert lst_five.head.next.next.next.value == 4 @@ -63,42 +64,42 @@ def test_insert_after(): lst_six.append(3) lst_six.append(4) lst_six.append(6) - lst_six.insert_after(4, 5) + lst_six.insertAfter(4, 5) assert lst_six.head.next.next.value == 3 assert lst_six.head.next.next.next.value == 4 assert lst_six.head.next.next.next.next.value == 5 assert lst_six.head.next.next.next.next.next.value == 6 - -def test_kth_method(): - lst_seven = LinkedList() - lst_seven.insert(1) - lst_seven.append(2) - lst_seven.append(3) - lst_seven.append(4) - lst_seven.append(5) - -def test_initialize_with_list(): - initialList = LinkedList([5,6,7,8]) - assert str(initialList) == '5 ->6 ->7 ->8' -def test_nth_greater_than(): - initialList = LinkedList([5,6,7,8]) - assert initialList.nthFromEnd(8) == 'Exception' -def test_nth_last(): - initialList = LinkedList([5,6,7,8]) - assert initialList.nthFromEnd(4) == 5 +def test_nth_greater_than(): + list = LinkedList() + list.append(1) + list.append(2) + list.append(3) + with pytest.raises(Exception): + assert list.nthFromEnd(8) +# initialList = LinkedList([5,6,7,8]) +# assert initialList.nthFromEnd(4) == 5 def test_nth_negative(): - initialList = LinkedList([5,6,7,8]) - assert initialList.nthFromEnd(-2) == 'Exception' + list = LinkedList() + list.append(1) + list.append(2) + list.append(3) + with pytest.raises(Exception): + assert list.nthFromEnd(-30) + def test_nth_list_of_one(): - initialList = LinkedList([5]) - assert initialList.nthFromEnd(0) == 5 + list = LinkedList() + list.append(1) + assert list.nthFromEnd(0) == 1 def test_nth_average_use(): - initialList = LinkedList([5,6,7,8,9,10,11]) - assert initialList.nthFromEnd(3) == 8 \ No newline at end of file + list = LinkedList() + list.append(1) + list.append(2) + list.append(3) + assert list.nthFromEnd(1) == 2 \ No newline at end of file