From 0e5da5a08105ac626778f40f9e65001106e13696 Mon Sep 17 00:00:00 2001 From: Chaitanya Date: Thu, 11 Apr 2024 03:42:33 +0530 Subject: [PATCH 1/6] hello World #1 --- practice/hello-world/hello_world.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/practice/hello-world/hello_world.py b/practice/hello-world/hello_world.py index adaa6c2..d695ea1 100644 --- a/practice/hello-world/hello_world.py +++ b/practice/hello-world/hello_world.py @@ -1,2 +1,2 @@ def hello(): - return 'Goodbye, Mars!' + return 'Hello, World!' From 95931877024fdd8519820db0b0e83f9b8c16a9e0 Mon Sep 17 00:00:00 2001 From: Chaitanya Date: Thu, 11 Apr 2024 23:48:39 +0530 Subject: [PATCH 2/6] Revert "hello World #1" This reverts commit 0e5da5a08105ac626778f40f9e65001106e13696. --- practice/hello-world/hello_world.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/practice/hello-world/hello_world.py b/practice/hello-world/hello_world.py index d695ea1..adaa6c2 100644 --- a/practice/hello-world/hello_world.py +++ b/practice/hello-world/hello_world.py @@ -1,2 +1,2 @@ def hello(): - return 'Hello, World!' + return 'Goodbye, Mars!' From 945a862d31170abaebe2d30f30a055a2f3fffa82 Mon Sep 17 00:00:00 2001 From: Chaitanya Date: Tue, 16 Apr 2024 20:09:39 +0530 Subject: [PATCH 3/6] dominoes --- .../__pycache__/dominoes.cpython-39.pyc | Bin 0 -> 731 bytes .../dominoes_test.cpython-39-pytest-6.2.4.pyc | Bin 0 -> 5346 bytes practice/dominoes/dominoes.py | 25 +++++++++++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 practice/dominoes/__pycache__/dominoes.cpython-39.pyc create mode 100644 practice/dominoes/__pycache__/dominoes_test.cpython-39-pytest-6.2.4.pyc diff --git a/practice/dominoes/__pycache__/dominoes.cpython-39.pyc b/practice/dominoes/__pycache__/dominoes.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..9cb05682408f6388b6af16c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l0|Nc0w* zK{B@ijntLTX2wwsIO6w{N}KtJ*G%25fWOZE2#W z8+%(Ek2qB`DR4Jzu*3*hm=rhBd1HQ0-cpv3Mj$@_62|gWFZB+PpASWN*{w;L=@+d;Dhj58T`nIf{?o!zHZ<{{9VUtJpRA?2e@NQkN6; zZYCT!c7er(qIDbQ5g0TyN$NuWbUaZw6A#dp$xFUaKx7mg9sN_MD{bIL(N* zA>Vb9isN8xRd+akSK%kIID^F^7Ko7Jmm`iJNVts!9wYfREUsg53yUfi7__C4`u363 zORWcx$nPcisf6Q-#fG>61)Qi0BY(5jlx0{YYud7`l9Z`VC)P&W4`M$QN##*%$LaMJ zoz@O!Bi*>|N8t)*e{er$NlDQQL&;P+Rt902ytq8E+Myp~hvT_MeG0R14|wdt=P=ZD U6o|xns0dxf?Nb`&s$MDl50)L{S^xk5 literal 0 HcmV?d00001 diff --git a/practice/dominoes/dominoes.py b/practice/dominoes/dominoes.py index d71e6a8..0f1643d 100644 --- a/practice/dominoes/dominoes.py +++ b/practice/dominoes/dominoes.py @@ -1,2 +1,25 @@ def can_chain(dominoes): - pass + def dfs(chain): + if not dominoes: + return [] + if len(chain) == len(dominoes) and chain[0][0] == chain[-1][1]: + return chain + for i in range(len(dominoes)): + if visited[i]: + continue + if not chain or chain[-1][1] == dominoes[i][0]: + visited[i] = True + result = dfs(chain + [dominoes[i]]) + if result: + return result + visited[i] = False + elif chain[-1][1] == dominoes[i][1]: + visited[i] = True + result = dfs(chain + [(dominoes[i][1], dominoes[i][0])]) + if result: + return result + visited[i] = False + return None + + visited = [False] * len(dominoes) + return dfs([]) \ No newline at end of file From ca94e7c9b0cee20c719c5f408ed963bcc98aa157 Mon Sep 17 00:00:00 2001 From: Chaitanya Date: Mon, 22 Apr 2024 03:14:11 +0530 Subject: [PATCH 4/6] removed cache files --- .../dominoes/__pycache__/dominoes.cpython-39.pyc | Bin 731 -> 0 bytes .../dominoes_test.cpython-39-pytest-6.2.4.pyc | Bin 5346 -> 0 bytes 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 practice/dominoes/__pycache__/dominoes.cpython-39.pyc delete mode 100644 practice/dominoes/__pycache__/dominoes_test.cpython-39-pytest-6.2.4.pyc diff --git a/practice/dominoes/__pycache__/dominoes.cpython-39.pyc b/practice/dominoes/__pycache__/dominoes.cpython-39.pyc deleted file mode 100644 index 9cb05682408f6388b6af16c8f9950077dd84235d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 731 zcmZWnL2J}N6n<~AyQbBxMWtF05f9Q__9Pz4QWh*d?q$V;L?mQpV{A5QXHuz>p0=lHIn_7rt-aOy2jsmoP!MyGn@9UmlJ39ikt0b2S7u1Hjw@ zNs@d@uStp#c}~bUT87P`uVJ1dnbC}AV(dtk;?zAO=|r4U$y2^ZXQHHtrVN+X@IM=C z(E;uP<~OKh<3=>1oC*jUgMFc&6&Pi+1~P%n*`($iwTrqb@6wy^CtQjGwR(6sC`iA* ztS{M~TOTOD$(F6_Q;AN^n~3kD=ZPqU{#pn@j5%bxsj^(0K%Zu?7XzC<(MWk+t45bd6|DvtA(TPO)BuJ9;qHwCWboe$=Qz&9@pgS@HMM zzELkz@>Hu9j$kIORwYTvb3@15a{bW5p}|Xs&LjAXzoB6Am35sGs=w9hhMKl+(gH0) zzd)y`1$B{@Xc_7$I!!B3TXcq=fVxCy=}D-|^c0N}6Vh$edq9`JY zd&4ODB8r55UrljrG!n=Wn!IOu&NkaI%UXUU%Ii6l@s$Xu_*Cjwb>(zY<#QADUX*&HB{+M9KCptcTOU~JeiGPK zwAt9bM+L2rf$wd&-pd&F+L!lpct!-RtibUR6mtmVD&m)vk)#KHOzah`<#MoybT#qa zhhby|jt`^MT|h^YZ>cgmngWN0fHj26>IkzDWv?sH0(&jv0LH8FrkazE7{WH$+g>o& zO#}2aY%f=aJumb8@e$??l?DZ=|4mQ_1|*Ehi^`T*di1M3kjey68Sm);GTb=77)CZh z+QSqjy|CR&YSA3A788}CcgUKy56SI5=KDr1Lr*FuKth~{qNBZ-!_Xf?7K|d#;ZU)y zV4h$g zZZ6LXx1mAFo&jeBI4vBIJyW)HXcgqS!R!<|#(|+u4v-n|{*)TjZVN_jKeXd5W~)5a zZni~iZ!kF|+$OVKp>Bsv*bV;`3sV7^=mPG$pg=OMjv?P~x80yxlw9x<+~9b0OpK;z z)GI18R@mw@>V1yt=GjV5HK;)^Cwc)>33(LN_4m42CS|g_lwx^?=aKGNC?l0|Nc0w* zK{B@ijntLTX2wwsIO6w{N}KtJ*G%25fWOZE2#W z8+%(Ek2qB`DR4Jzu*3*hm=rhBd1HQ0-cpv3Mj$@_62|gWFZB+PpASWN*{w;L=@+d;Dhj58T`nIf{?o!zHZ<{{9VUtJpRA?2e@NQkN6; zZYCT!c7er(qIDbQ5g0TyN$NuWbUaZw6A#dp$xFUaKx7mg9sN_MD{bIL(N* zA>Vb9isN8xRd+akSK%kIID^F^7Ko7Jmm`iJNVts!9wYfREUsg53yUfi7__C4`u363 zORWcx$nPcisf6Q-#fG>61)Qi0BY(5jlx0{YYud7`l9Z`VC)P&W4`M$QN##*%$LaMJ zoz@O!Bi*>|N8t)*e{er$NlDQQL&;P+Rt902ytq8E+Myp~hvT_MeG0R14|wdt=P=ZD U6o|xns0dxf?Nb`&s$MDl50)L{S^xk5 From 176a44687ddfee1d88b4cfc35254b2808b6311e4 Mon Sep 17 00:00:00 2001 From: Chaitanya Date: Mon, 22 Apr 2024 20:00:35 +0530 Subject: [PATCH 5/6] improved the code --- practice/dominoes/dominoes.py | 44 +++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/practice/dominoes/dominoes.py b/practice/dominoes/dominoes.py index 0f1643d..de80f3e 100644 --- a/practice/dominoes/dominoes.py +++ b/practice/dominoes/dominoes.py @@ -1,25 +1,65 @@ def can_chain(dominoes): + """ + Determine if the given dominoes can be arranged in a chain. + + A chain is a sequence of dominoes where the right number of one domino is + the same as the left number of the next domino, and the chain forms a loop + where the right number of the last domino is the same as the left number + of the first domino. + + This function uses depth-first search to try all possible chains. + + Args: + dominoes (list): A list of tuples representing the dominoes. + + Returns: + list: A list of tuples representing the chain if one exists, or None if no chain exists. + + Examples: + >>> can_chain([(1, 2), (2, 1)]) + [(1, 2), (2, 1)] + """ def dfs(chain): - if not dominoes: - return [] + """ + Perform depth-first search to find a chain. + + Args: + chain (list): The current chain of dominoes. + + Returns: + list: A list of tuples representing the chain if one exists, or None if no chain exists. + """ + # If all dominoes are in the chain and the chain forms a loop, return the chain if len(chain) == len(dominoes) and chain[0][0] == chain[-1][1]: return chain + + # Try to add each unvisited domino to the chain for i in range(len(dominoes)): + # Skip the domino if it has already been visited if visited[i]: continue + + # If the chain is empty or the right number of the last domino in the chain is the same as the left number of the current domino, try to add the domino to the chain if not chain or chain[-1][1] == dominoes[i][0]: visited[i] = True result = dfs(chain + [dominoes[i]]) if result: return result visited[i] = False + + # If the right number of the last domino in the chain is the same as the right number of the current domino, try to add the domino to the chain in reverse order elif chain[-1][1] == dominoes[i][1]: visited[i] = True result = dfs(chain + [(dominoes[i][1], dominoes[i][0])]) if result: return result visited[i] = False + + # If no chain was found, return None return None + # Initialize the visited list visited = [False] * len(dominoes) + + # Start the depth-first search with an empty chain return dfs([]) \ No newline at end of file From 43ea244dfd221feb91b81cafaf6c9f42d209f5ae Mon Sep 17 00:00:00 2001 From: Chaitanya Date: Mon, 22 Apr 2024 20:53:46 +0530 Subject: [PATCH 6/6] fixed the failing test case --- practice/dominoes/dominoes.py | 36 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/practice/dominoes/dominoes.py b/practice/dominoes/dominoes.py index de80f3e..f85ed16 100644 --- a/practice/dominoes/dominoes.py +++ b/practice/dominoes/dominoes.py @@ -2,22 +2,20 @@ def can_chain(dominoes): """ Determine if the given dominoes can be arranged in a chain. - A chain is a sequence of dominoes where the right number of one domino is - the same as the left number of the next domino, and the chain forms a loop - where the right number of the last domino is the same as the left number - of the first domino. - - This function uses depth-first search to try all possible chains. + This function uses a depth-first search (DFS) algorithm to try to arrange + the dominoes in a chain where the numbers on the ends match and each + domino's numbers match the numbers on the ends of the adjacent dominoes. Args: - dominoes (list): A list of tuples representing the dominoes. + dominoes (list): A list of tuples where each tuple represents a domino + and contains two integers. Returns: - list: A list of tuples representing the chain if one exists, or None if no chain exists. + list: A list of tuples representing the chain if one exists, or an + empty list if no chain exists. - Examples: - >>> can_chain([(1, 2), (2, 1)]) - [(1, 2), (2, 1)] + Raises: + IndexError: An error occurs if the list is empty. """ def dfs(chain): """ @@ -29,17 +27,19 @@ def dfs(chain): Returns: list: A list of tuples representing the chain if one exists, or None if no chain exists. """ + # If the list of dominoes is empty, return an empty list + if not dominoes: + return [] + # If all dominoes are in the chain and the chain forms a loop, return the chain - if len(chain) == len(dominoes) and chain[0][0] == chain[-1][1]: + if len(chain) == len(dominoes) and chain and chain[0][0] == chain[-1][1]: return chain - # Try to add each unvisited domino to the chain for i in range(len(dominoes)): - # Skip the domino if it has already been visited if visited[i]: continue - # If the chain is empty or the right number of the last domino in the chain is the same as the left number of the current domino, try to add the domino to the chain + # If the chain is empty or the last number of the last domino in the chain matches the first number of the current domino if not chain or chain[-1][1] == dominoes[i][0]: visited[i] = True result = dfs(chain + [dominoes[i]]) @@ -47,7 +47,7 @@ def dfs(chain): return result visited[i] = False - # If the right number of the last domino in the chain is the same as the right number of the current domino, try to add the domino to the chain in reverse order + # If the last number of the last domino in the chain matches the second number of the current domino elif chain[-1][1] == dominoes[i][1]: visited[i] = True result = dfs(chain + [(dominoes[i][1], dominoes[i][0])]) @@ -55,11 +55,7 @@ def dfs(chain): return result visited[i] = False - # If no chain was found, return None return None - # Initialize the visited list visited = [False] * len(dominoes) - - # Start the depth-first search with an empty chain return dfs([]) \ No newline at end of file