From bf44f090d5fcda90a81cb3138c17f789b7f8dca6 Mon Sep 17 00:00:00 2001 From: Adarshsidnal <01fe21bcs288@kletech.ac.in> Date: Thu, 5 Oct 2023 11:03:19 +0530 Subject: [PATCH 1/3] Added an algorithm transfrom bst to greater sum tree --- .../binary_tree/transform_bst_sum_tree.py | 126 ++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 data_structures/binary_tree/transform_bst_sum_tree.py diff --git a/data_structures/binary_tree/transform_bst_sum_tree.py b/data_structures/binary_tree/transform_bst_sum_tree.py new file mode 100644 index 000000000000..7b102c0c565f --- /dev/null +++ b/data_structures/binary_tree/transform_bst_sum_tree.py @@ -0,0 +1,126 @@ +from __future__ import annotations +class Node: + """ +prints the inorder Traversal of transformed tree +>>> sum = 0 +>>> root = Node(11) +>>> root.left = Node(2) +>>> root.right = Node(29) +>>> root.left.left = Node(1) +>>> root.left.right = Node(7) +>>> root.right.left = Node(15) +>>> root.right.right = Node(40) +>>> root.right.right.left = Node(35) +>>> printInorder(root) +1 2 7 11 15 29 35 40 + +>>> transformTree(root) + +>>> printInorder(root) +139 137 130 119 104 75 40 0 + +""" + + def __init__(self, number:int) -> None: + self.data = number + self.left = None + self.right = None + +# Recursive function to transform a BST to sum tree. +# This function traverses the tree in reverse inorder so +# that we have visited all greater key nodes of the currently +# visited node +def transform_tree_util(root:Node | None) -> None: + """ + Transform a binary tree into a sum tree. + + Example: + >>> root = Node(11) + >>> root.left = Node(2) + >>> root.right = Node(29) + >>> transformTree(root) + >>> root.data + 60 + >>> root.left.data + 31 + >>> root.right.data + 29 + """ + + # Base case + if (root == None): + return + + # Recur for right subtree + transform_tree_util(root.right) + + # Update sum + global sum + sum = sum + root.data + + # Store old sum in the current node + root.data = sum - root.data + + # Recur for left subtree + transform_tree_util(root.left) + +# A wrapper over transformTreeUtil() +def transform_tree(root:Node | None) -> None: + """ + Transform a binary tree into a sum tree. + + Example: + >>> root = Node(11) + >>> root.left = Node(2) + >>> root.right = Node(29) + >>> transformTree(root) + >>> root.data + 60 + >>> root.left.data + 31 + >>> root.right.data + 29 + """ + + # sum = 0 #Initialize sum + transform_tree_util(root) + +# A utility function to prindorder traversal of a +# binary tree +def print_inorder(root:Node | None): + """ + Perform an inorder traversal of a binary tree and print the nodes. + + Example: + >>> root = Node(11) + >>> root.left = Node(2) + >>> root.right = Node(29) + >>> printInorder(root) + 2 11 29 + """ + + if (root == None): + return + + print_inorder(root.left) + print(root.data, end = " ") + print_inorder(root.right) + +# Driver Program to test above functions +if __name__ == '__main__': + + sum = 0 + root = Node(11) + root.left = Node(2) + root.right = Node(29) + root.left.left = Node(1) + root.left.right = Node(7) + root.right.left = Node(15) + root.right.right = Node(40) + root.right.right.left = Node(35) + + print_inorder(root) + + transform_tree_util(root) + print_inorder(root) + From 56377fdba75342f9bb1b4c32e0b32e01b8d3b2f9 Mon Sep 17 00:00:00 2001 From: Adarshsidnal <01fe21bcs288@kletech.ac.in> Date: Thu, 5 Oct 2023 11:37:43 +0530 Subject: [PATCH 2/3] Added an algorithm to convert camel case string into snake case --- .../binary_tree/transform_bst_sum_tree.py | 126 ------------------ strings/camel_case_to_snake_case.py | 39 ++++++ 2 files changed, 39 insertions(+), 126 deletions(-) delete mode 100644 data_structures/binary_tree/transform_bst_sum_tree.py create mode 100644 strings/camel_case_to_snake_case.py diff --git a/data_structures/binary_tree/transform_bst_sum_tree.py b/data_structures/binary_tree/transform_bst_sum_tree.py deleted file mode 100644 index 7b102c0c565f..000000000000 --- a/data_structures/binary_tree/transform_bst_sum_tree.py +++ /dev/null @@ -1,126 +0,0 @@ -from __future__ import annotations -class Node: - """ -prints the inorder Traversal of transformed tree ->>> sum = 0 ->>> root = Node(11) ->>> root.left = Node(2) ->>> root.right = Node(29) ->>> root.left.left = Node(1) ->>> root.left.right = Node(7) ->>> root.right.left = Node(15) ->>> root.right.right = Node(40) ->>> root.right.right.left = Node(35) ->>> printInorder(root) -1 2 7 11 15 29 35 40 - ->>> transformTree(root) - ->>> printInorder(root) -139 137 130 119 104 75 40 0 - -""" - - def __init__(self, number:int) -> None: - self.data = number - self.left = None - self.right = None - -# Recursive function to transform a BST to sum tree. -# This function traverses the tree in reverse inorder so -# that we have visited all greater key nodes of the currently -# visited node -def transform_tree_util(root:Node | None) -> None: - """ - Transform a binary tree into a sum tree. - - Example: - >>> root = Node(11) - >>> root.left = Node(2) - >>> root.right = Node(29) - >>> transformTree(root) - >>> root.data - 60 - >>> root.left.data - 31 - >>> root.right.data - 29 - """ - - # Base case - if (root == None): - return - - # Recur for right subtree - transform_tree_util(root.right) - - # Update sum - global sum - sum = sum + root.data - - # Store old sum in the current node - root.data = sum - root.data - - # Recur for left subtree - transform_tree_util(root.left) - -# A wrapper over transformTreeUtil() -def transform_tree(root:Node | None) -> None: - """ - Transform a binary tree into a sum tree. - - Example: - >>> root = Node(11) - >>> root.left = Node(2) - >>> root.right = Node(29) - >>> transformTree(root) - >>> root.data - 60 - >>> root.left.data - 31 - >>> root.right.data - 29 - """ - - # sum = 0 #Initialize sum - transform_tree_util(root) - -# A utility function to prindorder traversal of a -# binary tree -def print_inorder(root:Node | None): - """ - Perform an inorder traversal of a binary tree and print the nodes. - - Example: - >>> root = Node(11) - >>> root.left = Node(2) - >>> root.right = Node(29) - >>> printInorder(root) - 2 11 29 - """ - - if (root == None): - return - - print_inorder(root.left) - print(root.data, end = " ") - print_inorder(root.right) - -# Driver Program to test above functions -if __name__ == '__main__': - - sum = 0 - root = Node(11) - root.left = Node(2) - root.right = Node(29) - root.left.left = Node(1) - root.left.right = Node(7) - root.right.left = Node(15) - root.right.right = Node(40) - root.right.right.left = Node(35) - - print_inorder(root) - - transform_tree_util(root) - print_inorder(root) - diff --git a/strings/camel_case_to_snake_case.py b/strings/camel_case_to_snake_case.py new file mode 100644 index 000000000000..ebbd1c342b50 --- /dev/null +++ b/strings/camel_case_to_snake_case.py @@ -0,0 +1,39 @@ +import re + +def camel_to_snake_case(input_str: str) -> str: + """ + Transforms a camelCase or PascalCase given string to snake_case + + >>> camel_to_snake_case("someRandomString") + 'some_random_string' + + >>> camel_to_snake_case("SomeRandomString") + 'some_random_string' + + >>> camel_to_snake_case("someRandomStringWithNumbers123") + 'some_random_string_with_numbers_123' + + >>> camel_to_snake_case("SomeRandomStringWithNumbers123") + 'some_random_string_with_numbers_123' + + >>> camel_to_snake_case(123) + Traceback (most recent call last): + ... + ValueError: Expected string as input, found + """ + + if not isinstance(input_str, str): + msg = f"Expected string as input, found {type(input_str)}" + raise ValueError(msg) + + # Use regular expression to split words on capital letters and numbers + words = re.findall(r'[A-Z][a-z]*|\d+|[a-z]+', input_str) + + # Join the words with underscores and convert to lowercase + return '_'.join(words).lower() + + +if __name__ == "__main__": + from doctest import testmod + + testmod() \ No newline at end of file From cbd75d36b11534dcbb206bdde64f362ca7b47189 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 5 Oct 2023 06:14:18 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/camel_case_to_snake_case.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/strings/camel_case_to_snake_case.py b/strings/camel_case_to_snake_case.py index ebbd1c342b50..6a3057e92bae 100644 --- a/strings/camel_case_to_snake_case.py +++ b/strings/camel_case_to_snake_case.py @@ -1,5 +1,6 @@ import re + def camel_to_snake_case(input_str: str) -> str: """ Transforms a camelCase or PascalCase given string to snake_case @@ -27,13 +28,13 @@ def camel_to_snake_case(input_str: str) -> str: raise ValueError(msg) # Use regular expression to split words on capital letters and numbers - words = re.findall(r'[A-Z][a-z]*|\d+|[a-z]+', input_str) + words = re.findall(r"[A-Z][a-z]*|\d+|[a-z]+", input_str) # Join the words with underscores and convert to lowercase - return '_'.join(words).lower() + return "_".join(words).lower() if __name__ == "__main__": from doctest import testmod - testmod() \ No newline at end of file + testmod()