From 5d3ddd9a8c175940b35b0fb9d006220fd8402dfe Mon Sep 17 00:00:00 2001 From: WuodRose Date: Sat, 31 Aug 2024 19:57:15 +0300 Subject: [PATCH] My solution --- Data_Structure/README.md | 0 Data_Structure/requirements.txt | 4 + Data_Structure/src/__init__.py | 4 + .../src/__pycache__/__init__.cpython-312.pyc | Bin 0 -> 326 bytes .../linked_list_max.cpython-312.pyc | Bin 0 -> 3328 bytes .../queue_with_stacks.cpython-312.pyc | Bin 0 -> 3344 bytes .../__pycache__/stack_reverse.cpython-312.pyc | Bin 0 -> 2837 bytes Data_Structure/src/linked_list_max.py | 86 ++++++++++++++++ Data_Structure/src/queue_with_stacks.py | 71 +++++++++++++ Data_Structure/src/stack_reverse.py | 85 ++++++++++++++++ Data_Structure/tests/__init__.py | 0 .../__pycache__/__init__.cpython-312.pyc | Bin 0 -> 164 bytes ...nked_list_max.cpython-312-pytest-8.3.2.pyc | Bin 0 -> 3232 bytes ...e_with_stacks.cpython-312-pytest-8.3.2.pyc | Bin 0 -> 6016 bytes ...stack_reverse.cpython-312-pytest-8.3.2.pyc | Bin 0 -> 5043 bytes Data_Structure/tests/test_linked_list_max.py | 49 +++++++++ .../tests/test_queue_with_stacks.py | 94 ++++++++++++++++++ Data_Structure/tests/test_stack_reverse.py | 37 +++++++ 18 files changed, 430 insertions(+) create mode 100644 Data_Structure/README.md create mode 100644 Data_Structure/requirements.txt create mode 100644 Data_Structure/src/__init__.py create mode 100644 Data_Structure/src/__pycache__/__init__.cpython-312.pyc create mode 100644 Data_Structure/src/__pycache__/linked_list_max.cpython-312.pyc create mode 100644 Data_Structure/src/__pycache__/queue_with_stacks.cpython-312.pyc create mode 100644 Data_Structure/src/__pycache__/stack_reverse.cpython-312.pyc create mode 100644 Data_Structure/src/linked_list_max.py create mode 100644 Data_Structure/src/queue_with_stacks.py create mode 100644 Data_Structure/src/stack_reverse.py create mode 100644 Data_Structure/tests/__init__.py create mode 100644 Data_Structure/tests/__pycache__/__init__.cpython-312.pyc create mode 100644 Data_Structure/tests/__pycache__/test_linked_list_max.cpython-312-pytest-8.3.2.pyc create mode 100644 Data_Structure/tests/__pycache__/test_queue_with_stacks.cpython-312-pytest-8.3.2.pyc create mode 100644 Data_Structure/tests/__pycache__/test_stack_reverse.cpython-312-pytest-8.3.2.pyc create mode 100644 Data_Structure/tests/test_linked_list_max.py create mode 100644 Data_Structure/tests/test_queue_with_stacks.py create mode 100644 Data_Structure/tests/test_stack_reverse.py diff --git a/Data_Structure/README.md b/Data_Structure/README.md new file mode 100644 index 00000000..e69de29b diff --git a/Data_Structure/requirements.txt b/Data_Structure/requirements.txt new file mode 100644 index 00000000..803dc011 --- /dev/null +++ b/Data_Structure/requirements.txt @@ -0,0 +1,4 @@ + +pytest +mypy +flake8 diff --git a/Data_Structure/src/__init__.py b/Data_Structure/src/__init__.py new file mode 100644 index 00000000..9911e260 --- /dev/null +++ b/Data_Structure/src/__init__.py @@ -0,0 +1,4 @@ + +from .stack_reverse import reverse_string +from .queue_with_stacks import QueueWithStacks +from .linked_list_max import LinkedList diff --git a/Data_Structure/src/__pycache__/__init__.cpython-312.pyc b/Data_Structure/src/__pycache__/__init__.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..67420fedae2a53b7871cdd2ea76072b3159fb77a GIT binary patch literal 326 zcmXw#&q~BF5XO`K5n1dmK85zO;8jEr_uyd>Wl^C7hSEkiG;P;Os`dqZ1)srZ@wC^1 zUc|%RguU&_Nm=JG`SN9cUr0WpXuznxZ5>a|b>LvYoX3S3k*ZCXf`L@S_2F#>da1z;}d%?UK1 ziHk=FVWj|}!iKA4I!J>HaKdiVqz%xHB#=EMLa&qvzfvO7k^rkDEedCKI_LU%)7s{3 z1h^qc$3VK}3j9S#REm@UKn38o55aK|ehnE|m6U}tQDEedhF{I| zNl{1zNT6<(dJQ?b$edcR9lr*Q-euHXDOp*mdbXX8)4EbHn%G6Y4HZ5 zY|hGfUw8)BbVN68InU%-Bd^-2jQkngg)e~0i&VDkJf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cpython-312.pyc b/Data_Structure/src/__pycache__/queue_with_stacks.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..5f6556fcd4db135a4bbbc405b81c5cb169afa78c GIT binary patch literal 3344 zcmcgu&2Jk;6rWwM9XC#zC<)F7ElfWoY9+FW8xGh&TTui-RcM={N>QZM#ygHTjd$JI zaazZfT2K%P6r}|ux3`o_Rpn3Nk~FQFTD`%An;{3JUf|8_cw;*bR2-Nn<2O5R-n@D5 z_kJ@!cXqZTXph$(XMR0^&@X&YYnl{xsvwk+iatgrT0*9>u6i?5h$yCdRz(D@ z%LP*^QRmv^pzg;EtSjD7LHlTE|Edw()qp)UuoJ1H|yjHHJIht7;J!?acGX17{)?-bj07quuaThrAc0C zWg?C*XNfeE;MPel&x#|M_zqHn88vJ-LntMtzfp!dIk`J#((445i0u~m8gL@*z>+k> z4mAnIPR6$y_svYi)Jn)Zg#1lBQYxBD<})r2CVu(phso*BT|!;aPoE|373Sop&lg$N zvGq?NNu$$e4QA+*j26-i_MUcWdRiP`zXplaWkmDEN?6w|+hV%DIB=mc&+*y}&#~JB z-7Qo-i24VYwcC;H=)kh}U1WzRx}sc9*FeU{91j8$Nf63_h&leXsm*bXrglSFmuOLt zK#Nk3mNTXa;IL~EKyq1}Fy8Km^1MNf9AOZtr2p+YK4IA`mh{l%<&@i+ zA=ofYfRMD?Z0jO{K-zVNCLEh^D}WH#BkP#hbcl;>hhZvo!Pz2SFy;#+j>j3!Ii{7d zK*RSG&lLHd0_NcTthE5RlQ3(*0}Vo?Dj_4EC$^G86LVf`ZRo)ayF`jJm(v#oYSeu=m;cazl^&5km-6H zz#`t<2;=UH1_UfK{H3yw_PN%0p{HBxw<7D-7Up=6n%Z8nZM~K%xA)usAFNIyV7&YF z7`{ky&H_MybCMTl4-P;bHmjw6KQvzgjJ!IDjb}VA`adbmFS3h<<&wHu8MjSx{Ub^p zn#ADPjKi{$)v*E0C>hqWO)Cwg&S^huu)0F2v2a~&@2N+q>c*m$7QEOtGDPhLT73Pi zycGm_b9Uim?-5BXDC&-e05p~M-6PP0{6iq`#Q08ZE3Cf8|4sOF`u5H zAb9yVwOScZq6W<-Ej~0A_*1_M@RPT;wFoo2_F@6!-Nup;yo(J04CTBMh;L5l@E*apCYhvGd@)!`LD;ojeK zTgZt7q{U4^2T<*kczn?ty~E0-yQQt(V;ddEUUc=`87rsmrtY(~o_lPw_xKunSb9*} z>`iQSo!khY6!@EfMKHd#L z_Xr6yqQWT0rRexQ;vR=?3H_mlgM-z+Nbrq&y*mhgszd$3BV2}`okT})%rn5(a!+!G zWy9`m=ZNb%_GnE=)P$20v5>bB2W>^UDjcc=^MF6*`E?j3cD;HTe$PA82b6mxT$=@< z8c-DFPxXCe>~{oR^|Xq#j^)M2fx+kD{;lxftuxzN@ayp}#+R)p+Q5GDbM3(Lk=58r V?AzfdTC{HTREt(Skv1e8`5WzaAhQ4f literal 0 HcmV?d00001 diff --git a/Data_Structure/src/__pycache__/stack_reverse.cpython-312.pyc b/Data_Structure/src/__pycache__/stack_reverse.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..aa20b1414103c524303fbcbed105623e85a8f2f3 GIT binary patch literal 2837 zcmb7G%TL@!7$1A>#VoK%2q6&VF$uKYl@MxBPAIAxL0(mgqHNMiD_YB04`A4}w=-U% zT|q)y4$!26o=DYJZKX<8#6QqO|ASsg6I#jYAyQA3de}guN;&oW#`Xp_NgpHm^UQn? z&-}iZf9mK+5g5~-{5liiwvtcS>LST&b^@plmPtNo z>p)x3Mxf_>t1x2?fqsg@I716XO9;wYnKQv$-*G3YMFmPx+wyVBD+Ry8*4ud_MP~QVRk2C5|G0KXp1eOK$CgfUbMAqJB{*-4&rpgsDm2rGl zLPNGKsO3^zqB%6=d*vn@c!jc8&Va%dR`O;U>ez4_IIt@kO47&#_rMaEiN24zjxbrN z>{rpi$P_Eg29{8RTXo3|d1RjDuJvTBueoZhgBpn}RVg}$Dzgz`8 zUGoCC@8tL>_|MG6{u>v(FfL7*oXvtJ8!T4?!!#W@ooUw6IduA9yiUVsL>rI=Qa4C< z&yx0OY8!7?h$0y05&?`~YRW38v&Z@ixmm2B|yf>vB zQXO_MR%@vcGMsn8uIL5=aHUCW$4Y8B^{df)-{`&DziAxfSSnr|KY8FtQA z*uOlXLd+9^5$9ouegaaOC*dk#tV_iCK3LaUBf!1#5oDhLBJs`)d_Vu~{CBsO{FT~r zZRO_j%?EpqZ5VI(>@gl?)DFR9jra}ZUySm4#J80hD7O;Rxdt--Ts9hFSHhC? z48ZH2R|EukwlMC>`R_JTm1&@r~J!k#z? zWPvDw^+Buryj151Jn8D*M5$}~%!T`A(c-84U_SjC9b zVw$#BFiqYH207LW=^&0DzDJ~~&}|4aM}ioN!$20vV=bi{Tiu%8v(?_B)2+@H{fx2= zv_Dit@jwwlGOXjf(nbTGblsmY+3h;NksxuC6>WYaL7??UZv05`F{|Z>lVx6+BctTK z{e;Y`bILT{s%gxrvA#m?9MGVB&nG|8IX>PqFb)`u{N|=D2`EMup{+dK?{mu z#>YaDYAkC(j7BB+9jjPjXF2yk<6@Zpj{F$(Q`ht1D5!0<#)FgmYQOwZfFegZB8qCg=+cAgnuK9ZyF* zWC47<54f+xBW?hJL^({_yH~9*_djeq@>{xRqxa;4^w5Se^dw0-I&Ys}I9pGWeVtoG z)q9t;M~1BWtEV=Np1KZO^%UvqUbqA@d%9NKW%ptF=p&=+uKsoF=dE9~-8cG{+Ut76 z+e{w?@%Ek#?VwD8+Ua*$u~_g*j9&HV+f|CqoaW+YPDkW#+a>0|Cg=rgmeF$+7bGX) ze~R(rV*o<}ZorQr!Ak{t87}DaUfjTDB=7&!I2|VV8~hd6lR0_{CiR4(D1WH~O8;X5 mr2f8gLg`s8)Cs)q7Pj&A_$`Gf$q(I)1GxvutDEGSwE8zjH= max_value: + max_value = current.data + current = current.next + + return max_value + + def print_list(self): + """ + Print all the values in the linked list. + + Returns: + list: A list of all values in the linked list. + """ + values = [] + current = self.head + while current: + values.append(current.data) + current = current.next + return values + + +if __name__ == "__main__": + ll = LinkedList() + ll.append(5) + ll.append(10) + ll.append(25) + ll.append(65) + + # Print all items in the linked list + print("LinkedList items:", ll.print_list()) # Output: LinkedList items: [3, 1, 4, 2] + + # Find and print the maximum value + print("Maximum Value in LinkedList:", ll.find_max()) # Output: Maximum Value in LinkedList: 4 diff --git a/Data_Structure/src/queue_with_stacks.py b/Data_Structure/src/queue_with_stacks.py new file mode 100644 index 00000000..2aaca5e8 --- /dev/null +++ b/Data_Structure/src/queue_with_stacks.py @@ -0,0 +1,71 @@ +class QueueWithStacks: + def __init__(self): + """ + This class implements a queue using two stacks. The enqueue operation is performed on stack1, + and the dequeue operation is performed on stack2. When stack2 is empty, elements are transferred + from stack1 to stack2 to ensure the correct order of elements. + """ + # Initialize two stacks: stack1 for enqueue operations, stack2 for dequeue operations + self.stack1 = [] + self.stack2 = [] + + def enqueue(self, x: int): + """ + Adds an element to the back of the queue. + + Parameters: + x (int): The element to be added to the queue. + + Returns: + None: This method does not return any value. It modifies the queue by adding the given element. + """ + # Push the element onto stack1 + self.stack1.append(x) + + def dequeue(self) -> int: + """ + Removes and returns the front element of the queue. + + The dequeue operation is performed on stack2. If stack2 is empty, elements are transferred + from stack1 to stack2 to ensure the correct order of elements. + + Raises: + IndexError: If both stacks are empty, indicating that the queue is empty. + + Returns: + int: The element at the front of the queue. + """ + # If both stacks are empty, raise an IndexError + if not self.stack1 and not self.stack2: + raise IndexError("Dequeue from an empty queue") + + # If stack2 is empty, transfer elements from stack1 to stack2 + if not self.stack2: + while self.stack1: + self.stack2.append(self.stack1.pop()) + + # Pop the element from stack2 (which is the front of the queue) + return self.stack2.pop() + + def __str__(self): + """ + Returns a string representation of the queue's current state. + + If stack2 is not empty, it represents the front of the queue in reverse order. + Otherwise, stack1 represents the back of the queue. + + Returns: + str: A string representation of the queue. + """ + if not self.stack2: + return str(self.stack1) + return str(self.stack2[::-1] + self.stack1) + +# Example usage: +if __name__ == "__main__": + q = QueueWithStacks() + q.enqueue("Lesson1") + q.enqueue("Lesson2") + q.enqueue("Lesson3") + print(q.dequeue()) # Output: 1 + print(q.dequeue()) # Output: 2 diff --git a/Data_Structure/src/stack_reverse.py b/Data_Structure/src/stack_reverse.py new file mode 100644 index 00000000..6867d368 --- /dev/null +++ b/Data_Structure/src/stack_reverse.py @@ -0,0 +1,85 @@ +class Stack: + + """ + A class representing a stack data structure. + + Attributes: + stack (list): A list to store the elements of the stack. + + Methods: + push(item): Adds an item to the top of the stack. + pop(): Removes and returns the top item from the stack. + is_empty(): Checks if the stack is empty. + """ + def __init__(self): + """ + Initializes an empty list to represent the stack. + """ + self.stack = [] + + def push(self, item): + """ + Adds an item to the top of the stack. + + Parameters: + item (any): The item to be added to the stack. + """ + self.stack.append(item) + + def pop(self): + """ + Removes and returns the top item from the stack. + + Returns: + any: The top item from the stack, or None if the stack is empty. + """ + return self.stack.pop() if not self.is_empty() else None + + def is_empty(self): + """ + Checks if the stack is empty. + + Returns: + bool: True if the stack is empty, False otherwise. + """ + return len(self.stack) == 0 + + +def reverse_string(s: str) -> str: + """ + Reverses a given string using a stack. + + Parameters: + s (str): The input string to be reversed. + + Returns: + str: The reversed string. + + Raises: + ValueError: If the input is not a string. + """ + # Check if the input is a string, raise an error if not + if not isinstance(s, str): + raise ValueError("Input must be a string") + + # Initialize a stack object to store characters + stack = Stack() + + # Iterate over each character in the string + for char in s: + # Push each character onto the stack + stack.push(char) + + # Initialize an empty string to store the reversed string + reversed_str = "" + + # While the stack is not empty + while not stack.is_empty(): + # Pop characters from the stack and append to the reversed string + reversed_str += stack.pop() + + # Return the reversed string + return reversed_str + + +print(reverse_string("Wellcome To My Data Structure Class Let's Have Fun Together!")) diff --git a/Data_Structure/tests/__init__.py b/Data_Structure/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Data_Structure/tests/__pycache__/__init__.cpython-312.pyc b/Data_Structure/tests/__pycache__/__init__.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..3b811ed8f89059562c606e7f8adf7c4cb80341ff GIT binary patch literal 164 zcmX@j%ge<81b;#=rh(|kAOanHW&w&!XQ*V*Wb|9fP{ah}eFmxdYnUieuvAGxIV_;^XxS iDt~d<#Fx1ma>4<0CU8BV!RWkOcs|k}AFc literal 0 HcmV?d00001 diff --git a/Data_Structure/tests/__pycache__/test_linked_list_max.cpython-312-pytest-8.3.2.pyc b/Data_Structure/tests/__pycache__/test_linked_list_max.cpython-312-pytest-8.3.2.pyc new file mode 100644 index 0000000000000000000000000000000000000000..84f74b2ec9894355b302400480c0cf2035c04774 GIT binary patch literal 3232 zcmbtW-A^1<6u&b&yUY#?EMK7%TE}Wr7GcYWZ47CNSW6|LL|bDlX)+mh1{gX!v$-=1 z>}Iy9hD3}ejp;)*)prcW2mc4_gD=ofp_3+k&==ka^ugeZ=ib@N&LXuY-Yoa?oZmg? z+}}CFuXS}2f;RKRqv^XbgnlOr)*|J}-Xu)!BMoVsi6%KZ^4Um+=TKIdjB@0JH)GkF z$r_kPme8cA$2H+r7eYA6BVAlA191pek``U%f(;H8xB{1oZIk6hiu0v$!@8ww=_@zuJTP>OiG7a?&j37+8mB!ILUtreq9-~Np?i<=ppo6DHBsA}L-I$(NM!P5599yY!#LAqBk`~#78+hd z?r|20*AYHel0sZ;@uX@l-aw!w-IfXs!MMOJpvA}yw7{iW)juFzB^la>vxXy2=dFxu z*p}?-j_b(otS(O*mZoIYMLDaxv$iJN)3Pd8N`ag)RmVw7WeoivM>4>IL_Vu)vTMt; z>bxG{+NNnQ7}iV~*cqY_y|S9i>6X?tubO#1H6)K{8UVELYUXwN>`W({Qd@fyJRHY?Gq5O)AfJJLyKC%N&$ySVZ8j8UFSNsqdp1D3nl? zVSc!kc#p^39(Pnp_xQPT=?ZJDmha&ZoI{Z~+$bXVC174hDQ+Sq_`b=suHZ{KYydpDv)D^StAyCI zrZ1RwMm3$`bVZz$E@v3JJ7_D}v>e@PJGI$%X1(pqa^gw-@vTJT-SN-IzwX>@?_O{3 z-blQ$)p2ICqi4ONXL)=#iW)k0Vkp_VoY*5lD>j7^k9s~N7f)O*)0mP{1{zm{hU65t z&tJTl;)+eDyK;rTlp|lfD3j@6%JJjiof0G(??>R6bc4l~%Z!5O&&gAHSDpce7L(F2 zxdXaR{S8!Nd%xkyt7h)6<8`FIGZKLF}4cG6S{=~THJjjk`Uu=~>6xdMF@75j|}ynv}-J%=-PHm73UPr$JU{oF~63W6W|Sm7yZE^xbV&J;=C`?Fe;`QR_F`M4EUVxknjNWS;LLwzV;XL z6`zH35?7}MQuGdb-if4^FFNnMvn9kn8~bEzSzQ`u)2XGaM^ATSv1se_B#KIFiId+H zS57^+y>>FaCY{|7`hdKR&G&1+s=ZftXY8pEUrVG`QY#&6(zy-c{4-&A^MC(-6NYzU z(dfX_<`efWEQ?!Wa#L(y7uz?)j%VWN=8JzD;^?1GYg=~_2g+jITC!*5gOy8b^?jRS z|GL=!*G>cu_8n3ze-=j4ql4(-VEjr;v zq;*Bkk@wbK1ulq;i1;LZ93`M`Iv@maCyn(549V@s54QZ8@*eIaI?WEwq=Sq)C{jUs zkoP1;5gIEBCjTUSk1#3glnVReg{-aRO??=@1$(3{JN?jsuN?Obp(XSfb?*w@+`!Jq g91;@C#Yd6$C&Gyh;gu)wcvAmkVPGeUgyR(QZ%M8h(*OVf literal 0 HcmV?d00001 diff --git a/Data_Structure/tests/__pycache__/test_queue_with_stacks.cpython-312-pytest-8.3.2.pyc b/Data_Structure/tests/__pycache__/test_queue_with_stacks.cpython-312-pytest-8.3.2.pyc new file mode 100644 index 0000000000000000000000000000000000000000..673ba32b67f3002967fc3d62e4166c9770589be5 GIT binary patch literal 6016 zcmd5=|8En?6`!@&_S()@I|&IK*G&RPEQm#%K&#}!5ta7Jm7{lnbh-$2wRqOp!QM5q zYlt1EM^99jtF8h{C3Ct~g8sst=nwoCmHI_8Bq-b}5=i~&eyLJYsTIGpZ)U%EV;nf4 z>z9c$J73S3jVZ@ zLj`^|z|kIGK3E9NhB)LyZzAl!j4*%4XN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sf?;uzyugzlgxXy0>X8}6DriILLs eMfFj9^KZ(=X$8{sRA%23rDraIlr8)qNc{sNxe`_Y literal 0 HcmV?d00001 diff --git a/Data_Structure/tests/test_linked_list_max.py b/Data_Structure/tests/test_linked_list_max.py new file mode 100644 index 00000000..9628ebbf --- /dev/null +++ b/Data_Structure/tests/test_linked_list_max.py @@ -0,0 +1,49 @@ +from src.linked_list_max import LinkedList + +def test_linked_list_find_max(): + """ + This function tests the find_max method of a LinkedList class. + + The LinkedList class is assumed to have the following methods: + - append(value): Adds a new node with the given value to the end of the list. + - find_max(): Returns the maximum value in the list. If the list is empty, raises a ValueError. + + Raises: + AssertionError: If the find_max method does not return the expected maximum value + or does not raise a ValueError for an empty list. + """ + + # Create and populate a LinkedList + ll = LinkedList() + ll.append(5) + ll.append(10) + ll.append(25) + ll.append(65) + + # Print appended items + def print_list(linked_list): + items = [] + current_node = linked_list.head + while current_node: + items.append(current_node.data) + current_node = current_node.next + return items + + print(f"LinkedList items: {print_list(ll)}") + + # Find and print the maximum value + max_value = ll.find_max() + print(f"Maximum Value in LinkedList: {max_value}") # Display maximum value + assert max_value == 65, f"Expected 65 but got {max_value}" + + # Test find_max with an empty LinkedList + try: + empty_ll = LinkedList() + empty_ll.find_max() + assert False, "Did not raise ValueError for empty linked list" + except ValueError as e: + print(f"Error for empty LinkedList: {e}") # Display error message + +if __name__ == "__main__": + test_linked_list_find_max() + print("All LinkedList find_max tests passed!") diff --git a/Data_Structure/tests/test_queue_with_stacks.py b/Data_Structure/tests/test_queue_with_stacks.py new file mode 100644 index 00000000..aec2387f --- /dev/null +++ b/Data_Structure/tests/test_queue_with_stacks.py @@ -0,0 +1,94 @@ +from src.queue_with_stacks import QueueWithStacks + +class QueueWithStacks: + def __init__(self): + """ + This class implements a queue using two stacks. The enqueue operation is performed on stack1, + and the dequeue operation is performed on stack2. When stack2 is empty, elements are transferred + from stack1 to stack2 to ensure the correct order of elements. + """ + self.stack1 = [] # enqueue operations + self.stack2 = [] # dequeue operations + + def enqueue(self, x: int): + """ + Adds an element to the back of the queue. + + Parameters: + x (int): The element to be added to the queue. + """ + self.stack1.append(x) + + def dequeue(self) -> int: + """ + Removes and returns the front element of the queue. + + Raises: + IndexError: If both stacks are empty, indicating that the queue is empty. + + Returns: + int: The element at the front of the queue. + """ + if not self.stack1 and not self.stack2: + raise IndexError("Dequeue from an empty queue") + + if not self.stack2: + while self.stack1: + self.stack2.append(self.stack1.pop()) + + return self.stack2.pop() + + def __str__(self): + """ + Returns a string representation of the queue's current state. + """ + if not self.stack2: + return str(self.stack1) + return str(self.stack2[::-1] + self.stack1) + + +def test_queue_with_stacks(): + """ + This function tests the functionality of a Queue implemented using two Stacks. + + The QueueWithStacks class has two methods: + - enqueue(value): Adds an element to the end of the queue. + - dequeue(): Removes and returns the element at the front of the queue. + + Raises: + IndexError: If the dequeue method is called on an empty queue. + """ + q = QueueWithStacks() + q.enqueue("Lesson1") + q.enqueue("Lesson2") + + # Test first dequeue + first_out = q.dequeue() + print(f"Dequeued: {first_out} | Queue state: {q}") + assert first_out == "Lesson1" + + # Test second and third dequeues + q.enqueue("Lesson3") + print(f"Queue after enqueueing 3-Lessons: {q}") + + second_out = q.dequeue() + print(f"Dequeued: {second_out} | Queue state: {q}") + assert second_out == "Lesson2" + + third_out = q.dequeue() + print(f"Dequeued: {third_out} | Queue state: {q}") + assert third_out == "Lesson3" + + # Test dequeue on an empty queue + try: + empty_queue = QueueWithStacks() + empty_queue.dequeue() + assert False, "Did not raise IndexError for empty queue" + except IndexError: + print("IndexError raised as expected for empty queue") + assert True + + +if __name__ == "__main__": + test_queue_with_stacks() + print("All QueueWithStacks tests passed!") diff --git a/Data_Structure/tests/test_stack_reverse.py b/Data_Structure/tests/test_stack_reverse.py new file mode 100644 index 00000000..528fb836 --- /dev/null +++ b/Data_Structure/tests/test_stack_reverse.py @@ -0,0 +1,37 @@ +from src.stack_reverse import reverse_string + +def test_reverse_string(): + """ + This function tests the reverse_string function from the src.stack_reverse module. + + It tests the reverse_string function with various inputs and asserts that the + expected output matches the actual output. It also tests the function's behavior when + given a non-string input. + + Raises: + AssertionError: If any of the test cases fail. + ValueError: If the reverse_string function raises a ValueError for a non-string input. + """ + + # Test cases with assertions and print statements + s = "Wellcome To My Data Structure Class Let's Have Fun Together!" + reversed_s = reverse_string(s) + print(f"Original String: {s}, Reversed String: {reversed_s}") # Display output + assert reversed_s == "!rehtegoT nuF evaH s'teL ssalC erutcurtS ataD yM oT emoclleW" + + assert reverse_string("") == "" + assert reverse_string("a") == "a" + assert reverse_string("madam") == "madam" + assert reverse_string("wuodrose") == "esordouw" + + # Test with non-string input + try: + reverse_string(12345) + assert False, "Did not raise ValueError for non-string input" + except ValueError: + print("ValueError raised as expected for non-string input") # Display output + assert True + +if __name__ == "__main__": + test_reverse_string() + print("All reverse_string tests passed!")