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 83c9c65df8c17155b6d85e9c4dabf424f9c21b60 Mon Sep 17 00:00:00 2001 From: Chaitanya Date: Mon, 22 Apr 2024 00:11:11 +0530 Subject: [PATCH 3/6] satellite --- .../__pycache__/satellite.cpython-39.pyc | Bin 0 -> 1284 bytes ...satellite_test.cpython-39-pytest-6.2.4.pyc | Bin 0 -> 2499 bytes practice/satellite/satellite.py | 40 +++++++++++++++++- 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 practice/satellite/__pycache__/satellite.cpython-39.pyc create mode 100644 practice/satellite/__pycache__/satellite_test.cpython-39-pytest-6.2.4.pyc diff --git a/practice/satellite/__pycache__/satellite.cpython-39.pyc b/practice/satellite/__pycache__/satellite.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..cb2df7e6e0506ad77c7f48813c2ba85df0b915eb GIT binary patch literal 1284 zcmaJA%W4!suwOGf8L}cFXyPF(qQSWYPa+~BMDZef352k$4C8cfGCDhxboYk1OfGKx z3lGUL;3xPEen4M6JRF#p0OlT` zH$)Oi&j2Q@M-7pLj4mZK8sB1+P5q$ccCpmwU0GwmV=Hv$_r!nP(%e{aNjbV0x- zYyvO&(F9%+z=nz@kf9%3t2=&Rb#|<@$x721j*RUb0Nb$#s$-zlol;f%_F%mE|68h5 zLsePxueZFeY*tjAQB@p`R0q~|Xs)o;SJ-&H1SnZ@9KyJG7hlP1t!wQ+^W?|r2}0L)0=EuT$rLVHmh>wXX$63gf2SNs@76z*D5Mc^kQAtHuZ0DMtjG- zWqg;Z8NJ5csdoB}JN literal 0 HcmV?d00001 diff --git a/practice/satellite/__pycache__/satellite_test.cpython-39-pytest-6.2.4.pyc b/practice/satellite/__pycache__/satellite_test.cpython-39-pytest-6.2.4.pyc new file mode 100644 index 0000000000000000000000000000000000000000..c2224bb993e052735821243f5bad94d8e3afedd0 GIT binary patch literal 2499 zcmd5;OK%%D5a#SltB2ztP3yWvQ8h@?DhH!!FGY|B2ArTb9pWHG1p|wXIFi@cT`44$ zSb}|W?)?KfG`;CB@!C`WLV=*2;o7p}SVmCv&=oiwa(0I^-#n;vWu+nLx!YVDJgp1y z2QSV(0WCIZ=!Zn4kdTWWxY;XwPk<*W>O~~+@>&u1Vh}$G>C51ikO4+}^&`L6 zkgzQ};h&^QbUR?L8-NY-V8=9+BE7R|9-p_8xJmF?7Eo@WN1pf-IVjVBK~8q(OJp>GfwibJ5` zW#Z635Wfin2XNpWc?bR*j~yn3(4Bj>nQD#7JbFG!bH&Ea0~=_}2R0rn94U#)*0Qo< zV%#5N-=Lfe)uea-`tjktou}LFt&zl?p&s>ja357a)5XqMrG1n2aYvune99$;%-}y3@#Mw1xh7148%*leN^gv0^e~_QMgCNe5QFNqd_9G!2lJOCaJ8sLpn5Py$ciN^fzz~WHL4KHk6pIqy% hCE551?QW&?SAy-j$Y2}auY7;N?Mw5Gal>nc{{XF5W)}be literal 0 HcmV?d00001 diff --git a/practice/satellite/satellite.py b/practice/satellite/satellite.py index 2810ec6..30e5111 100644 --- a/practice/satellite/satellite.py +++ b/practice/satellite/satellite.py @@ -1,2 +1,40 @@ +class Node: + def __init__(self, value, left=None, right=None): + self.value = value + self.left = left + self.right = right + + def to_dict(self): + return { + "v": self.value, + "l": self.left.to_dict() if self.left else {}, + "r": self.right.to_dict() if self.right else {}, + } + def tree_from_traversals(preorder, inorder): - pass + if len(preorder) != len(inorder): + raise ValueError("traversals must have the same length") + if sorted(preorder) != sorted(inorder): + raise ValueError("traversals must have the same elements") + if len(set(preorder)) != len(preorder): + raise ValueError("traversals must contain unique items") + + if not preorder and not inorder: + return {} + + root_value = preorder[0] + root_index = inorder.index(root_value) + + left_subtree = tree_from_traversals(preorder[1:root_index+1], inorder[:root_index]) + right_subtree = tree_from_traversals(preorder[root_index+1:], inorder[root_index+1:]) + + if isinstance(left_subtree, Node): + left_subtree = left_subtree.to_dict() + if isinstance(right_subtree, Node): + right_subtree = right_subtree.to_dict() + + return { + "v": root_value, + "l": left_subtree, + "r": right_subtree, + } \ No newline at end of file From 2c6c3b384c71da9fae20625ed6e10845a200da77 Mon Sep 17 00:00:00 2001 From: Chaitanya Date: Mon, 22 Apr 2024 00:17:34 +0530 Subject: [PATCH 4/6] Added Comments --- practice/satellite/satellite.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/practice/satellite/satellite.py b/practice/satellite/satellite.py index 30e5111..e0b299d 100644 --- a/practice/satellite/satellite.py +++ b/practice/satellite/satellite.py @@ -1,9 +1,11 @@ +# Node class represents a node in the binary tree class Node: def __init__(self, value, left=None, right=None): self.value = value self.left = left self.right = right + # Convert the node and its children into a dictionary def to_dict(self): return { "v": self.value, @@ -11,28 +13,37 @@ def to_dict(self): "r": self.right.to_dict() if self.right else {}, } +# Function to construct a binary tree from its preorder and inorder traversals def tree_from_traversals(preorder, inorder): + # Check if the lengths of preorder and inorder are the same if len(preorder) != len(inorder): raise ValueError("traversals must have the same length") + # Check if the sorted lists are the same (which means they contain the same elements) if sorted(preorder) != sorted(inorder): raise ValueError("traversals must have the same elements") + # Check if the elements in the lists are unique if len(set(preorder)) != len(preorder): raise ValueError("traversals must contain unique items") + # If both preorder and inorder are empty, return an empty dictionary if not preorder and not inorder: return {} + # Find the root of the tree from the preorder traversal root_value = preorder[0] root_index = inorder.index(root_value) + # Split the preorder and inorder traversals into left and right subtrees left_subtree = tree_from_traversals(preorder[1:root_index+1], inorder[:root_index]) right_subtree = tree_from_traversals(preorder[root_index+1:], inorder[root_index+1:]) + # Convert the left and right subtrees to dictionaries if they are Node instances if isinstance(left_subtree, Node): left_subtree = left_subtree.to_dict() if isinstance(right_subtree, Node): right_subtree = right_subtree.to_dict() + # Return a dictionary representing the tree return { "v": root_value, "l": left_subtree, From 59aee8d5982208b3efd59f4d34d4ff3c05a0e73f Mon Sep 17 00:00:00 2001 From: Chaitanya Date: Mon, 22 Apr 2024 02:39:40 +0530 Subject: [PATCH 5/6] removed cache files --- .../__pycache__/satellite.cpython-39.pyc | Bin 1284 -> 0 bytes .../satellite_test.cpython-39-pytest-6.2.4.pyc | Bin 2499 -> 0 bytes 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 practice/satellite/__pycache__/satellite.cpython-39.pyc delete mode 100644 practice/satellite/__pycache__/satellite_test.cpython-39-pytest-6.2.4.pyc diff --git a/practice/satellite/__pycache__/satellite.cpython-39.pyc b/practice/satellite/__pycache__/satellite.cpython-39.pyc deleted file mode 100644 index cb2df7e6e0506ad77c7f48813c2ba85df0b915eb..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1284 zcmaJA%W4!suwOGf8L}cFXyPF(qQSWYPa+~BMDZef352k$4C8cfGCDhxboYk1OfGKx z3lGUL;3xPEen4M6JRF#p0OlT` zH$)Oi&j2Q@M-7pLj4mZK8sB1+P5q$ccCpmwU0GwmV=Hv$_r!nP(%e{aNjbV0x- zYyvO&(F9%+z=nz@kf9%3t2=&Rb#|<@$x721j*RUb0Nb$#s$-zlol;f%_F%mE|68h5 zLsePxueZFeY*tjAQB@p`R0q~|Xs)o;SJ-&H1SnZ@9KyJG7hlP1t!wQ+^W?|r2}0L)0=EuT$rLVHmh>wXX$63gf2SNs@76z*D5Mc^kQAtHuZ0DMtjG- zWqg;Z8NJ5csdoB}JN diff --git a/practice/satellite/__pycache__/satellite_test.cpython-39-pytest-6.2.4.pyc b/practice/satellite/__pycache__/satellite_test.cpython-39-pytest-6.2.4.pyc deleted file mode 100644 index c2224bb993e052735821243f5bad94d8e3afedd0..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2499 zcmd5;OK%%D5a#SltB2ztP3yWvQ8h@?DhH!!FGY|B2ArTb9pWHG1p|wXIFi@cT`44$ zSb}|W?)?KfG`;CB@!C`WLV=*2;o7p}SVmCv&=oiwa(0I^-#n;vWu+nLx!YVDJgp1y z2QSV(0WCIZ=!Zn4kdTWWxY;XwPk<*W>O~~+@>&u1Vh}$G>C51ikO4+}^&`L6 zkgzQ};h&^QbUR?L8-NY-V8=9+BE7R|9-p_8xJmF?7Eo@WN1pf-IVjVBK~8q(OJp>GfwibJ5` zW#Z635Wfin2XNpWc?bR*j~yn3(4Bj>nQD#7JbFG!bH&Ea0~=_}2R0rn94U#)*0Qo< zV%#5N-=Lfe)uea-`tjktou}LFt&zl?p&s>ja357a)5XqMrG1n2aYvune99$;%-}y3@#Mw1xh7148%*leN^gv0^e~_QMgCNe5QFNqd_9G!2lJOCaJ8sLpn5Py$ciN^fzz~WHL4KHk6pIqy% hCE551?QW&?SAy-j$Y2}auY7;N?Mw5Gal>nc{{XF5W)}be From 53efa5396e18208507743351ae30eb4959a37638 Mon Sep 17 00:00:00 2001 From: Chaitanya Date: Mon, 22 Apr 2024 14:08:51 +0530 Subject: [PATCH 6/6] improved the code --- practice/satellite/satellite.py | 74 ++++++++++++++++++++++++++++----- 1 file changed, 63 insertions(+), 11 deletions(-) diff --git a/practice/satellite/satellite.py b/practice/satellite/satellite.py index e0b299d..dbdd33c 100644 --- a/practice/satellite/satellite.py +++ b/practice/satellite/satellite.py @@ -1,49 +1,101 @@ -# Node class represents a node in the binary tree class Node: + """ + A class to represent a node in a binary tree. + + Attributes + ---------- + value : int + The value of the node. + left : Node, optional + The left child of the node (default is None). + right : Node, optional + The right child of the node (default is None). + + Methods + ------- + to_dict(): + Converts the node and its children into a dictionary. + """ + def __init__(self, value, left=None, right=None): + """ + Constructs all the necessary attributes for the Node object. + + Parameters + ---------- + value : int + The value of the node. + left : Node, optional + The left child of the node (default is None). + right : Node, optional + The right child of the node (default is None). + """ self.value = value self.left = left self.right = right - # Convert the node and its children into a dictionary def to_dict(self): + """ + Converts the node and its children into a dictionary. + + Returns + ------- + dict + A dictionary with keys 'v', 'l', 'r' representing value, left child, and right child respectively. + """ return { "v": self.value, "l": self.left.to_dict() if self.left else {}, "r": self.right.to_dict() if self.right else {}, } -# Function to construct a binary tree from its preorder and inorder traversals + def tree_from_traversals(preorder, inorder): - # Check if the lengths of preorder and inorder are the same + """ + Constructs a binary tree from its preorder and inorder traversals. + + Parameters + ---------- + preorder : list + The preorder traversal of the tree. + inorder : list + The inorder traversal of the tree. + + Returns + ------- + dict + A dictionary representing the tree. + + Raises + ------ + ValueError + If the lengths of preorder and inorder are not the same, + or if they do not contain the same elements, + or if they do not contain unique items. + """ if len(preorder) != len(inorder): raise ValueError("traversals must have the same length") - # Check if the sorted lists are the same (which means they contain the same elements) + if sorted(preorder) != sorted(inorder): raise ValueError("traversals must have the same elements") - # Check if the elements in the lists are unique + if len(set(preorder)) != len(preorder): raise ValueError("traversals must contain unique items") - # If both preorder and inorder are empty, return an empty dictionary if not preorder and not inorder: return {} - # Find the root of the tree from the preorder traversal root_value = preorder[0] root_index = inorder.index(root_value) - # Split the preorder and inorder traversals into left and right subtrees left_subtree = tree_from_traversals(preorder[1:root_index+1], inorder[:root_index]) right_subtree = tree_from_traversals(preorder[root_index+1:], inorder[root_index+1:]) - # Convert the left and right subtrees to dictionaries if they are Node instances if isinstance(left_subtree, Node): left_subtree = left_subtree.to_dict() if isinstance(right_subtree, Node): right_subtree = right_subtree.to_dict() - # Return a dictionary representing the tree return { "v": root_value, "l": left_subtree,