From 0a1998063ca0e18cb25e147870fff5c10e6973e2 Mon Sep 17 00:00:00 2001 From: Arnav Thakare <91930603+Arnavthakare19@users.noreply.github.com> Date: Sun, 8 Oct 2023 00:46:00 +0530 Subject: [PATCH 1/2] Added palindrome folder Greetings, The programs I am submitting checks whether the input String/number is palindrome or not Hoping for a favorable reply, Arnavthakare19 --- P/Palindrome/palindrome_number.py | 11 +++++++++++ P/Palindrome/palindrome_string.py | 16 ++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 P/Palindrome/palindrome_number.py create mode 100644 P/Palindrome/palindrome_string.py diff --git a/P/Palindrome/palindrome_number.py b/P/Palindrome/palindrome_number.py new file mode 100644 index 00000000..79670024 --- /dev/null +++ b/P/Palindrome/palindrome_number.py @@ -0,0 +1,11 @@ +num=int(input("Enter a number:")) +temp=num +rev=0 +while(num>0): + dig=num%10 + rev=rev*10+dig + num=num//10 +if(temp==rev): + print("The number is palindrome!") +else: + print("Not a palindrome!") \ No newline at end of file diff --git a/P/Palindrome/palindrome_string.py b/P/Palindrome/palindrome_string.py new file mode 100644 index 00000000..46f2a457 --- /dev/null +++ b/P/Palindrome/palindrome_string.py @@ -0,0 +1,16 @@ +def isPalindrome(str): + + + for i in range(0, int(len(str)/2)): + if str[i] != str[len(str)-i-1]: + return False + return True + + +s = "malayalam" +ans = isPalindrome(s) + +if (ans): + print("Yes") +else: + print("No") \ No newline at end of file From 3d2fb1a35a93bd303281c3b1e13878c9199e89b9 Mon Sep 17 00:00:00 2001 From: Arnav Thakare <91930603+Arnavthakare19@users.noreply.github.com> Date: Sun, 8 Oct 2023 00:50:55 +0530 Subject: [PATCH 2/2] Added binary tree generator --- .../treegenerator.py | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 B/Random Binary Tree Generator/treegenerator.py diff --git a/B/Random Binary Tree Generator/treegenerator.py b/B/Random Binary Tree Generator/treegenerator.py new file mode 100644 index 00000000..83c198ee --- /dev/null +++ b/B/Random Binary Tree Generator/treegenerator.py @@ -0,0 +1,40 @@ +import random + + +class Node: + def __init__(self, value): + self.value = value + self.left = None + self.right = None + +def generate_random_binary_tree(size): + if size == 0: + return None + + # Choose random sizes for left and right subtrees + left_size = random.randint(0, size-1) + right_size = size - 1 - left_size + + # Generate left and right subtrees recursively + left_subtree = generate_random_binary_tree(left_size) + right_subtree = generate_random_binary_tree(right_size) + + # Create new node with random value + root = Node(random.randint(0, 100)) + + # Assign left and right subtrees to children + root.left = left_subtree + root.right = right_subtree + + return root + + +def print_tree(node, level=0): + if node is not None: + print_tree(node.right, level + 1) + print(" " * 4 * level + "->", node.value) + print_tree(node.left, level + 1) + + +tree = generate_random_binary_tree(input('Enter the size of binary tree')) +print_tree(tree) \ No newline at end of file