From 4822c491da664b7f9a8e72bc812bb848a290aa74 Mon Sep 17 00:00:00 2001 From: Shantan Thota <36533123+lucifer824@users.noreply.github.com> Date: Sun, 18 Mar 2018 16:15:47 +0530 Subject: [PATCH 1/3] Add files via upload --- 1_sum_57.py | 8 ++++++++ 2_room_area.py | 28 ++++++++++++++++++++++++++++ 3_hp_list.py | 16 ++++++++++++++++ 4_pythagorean_triplet.py | 13 +++++++++++++ 5_number_div_1_20.py | 18 ++++++++++++++++++ 6_prime_numbers.py | 19 +++++++++++++++++++ 7_palindrome.py | 16 ++++++++++++++++ 7 files changed, 118 insertions(+) create mode 100644 1_sum_57.py create mode 100644 2_room_area.py create mode 100644 3_hp_list.py create mode 100644 4_pythagorean_triplet.py create mode 100644 5_number_div_1_20.py create mode 100644 6_prime_numbers.py create mode 100644 7_palindrome.py diff --git a/1_sum_57.py b/1_sum_57.py new file mode 100644 index 0000000..eca21b1 --- /dev/null +++ b/1_sum_57.py @@ -0,0 +1,8 @@ +def show_sum(): + sum = 0 + for x in range(0,500): + if ((x % 5 == 0) and (x % 7 != 0)): + sum += x + print("Sum of numbers less than 500 which are divisble by 5 and not by 7 = %s" %(sum)) + +show_sum() diff --git a/2_room_area.py b/2_room_area.py new file mode 100644 index 0000000..c37f258 --- /dev/null +++ b/2_room_area.py @@ -0,0 +1,28 @@ +class room: + def __init__(self, length, width, height): + self.length = length + self.width = width + self.height = height + print("Room of dimensions %s X %s X %s is created.\n" %(self.length, self.width, self.height)) + + def new_dimensions(self, increase_percentage): + self.length *= (1+(increase_percentage/100)) + self.width *= (1+(increase_percentage/100)) + self.height *= (1+(increase_percentage/100)) + print("Room dimensions increased by %s percent.\n" %(increase_percentage)) + + def calculate_area(self): + area = 2 * (self.length * self.width + self.width * self.height + self.height * self.length) + print("Suface Area of room of new dimensions is = %s" %(area)) + volume = self.length * self.width * self.height + print("Volume of room of new dimensions is = %s" %(volume)) + +try: + dim = input("Enter dimensions(aXbXc): ") + dim = dim.split('X') + x = room(int(dim[0]), int(dim[1]), int(dim[2])) + percent = int(input("Enter increase percent: ")) + x.new_dimensions(percent) + x.calculate_area() +except: + print("Unexpected error! Please re-enter values.") \ No newline at end of file diff --git a/3_hp_list.py b/3_hp_list.py new file mode 100644 index 0000000..a229cd5 --- /dev/null +++ b/3_hp_list.py @@ -0,0 +1,16 @@ +list1 = ["Ron", "Hermione", "Harry", "Professor", "Dobby"] + +list2 = ["The House Elf", "Potter", "Granger", "Lockhart", "Weasley"] + +print("List 1: " + str(list1)) +list1.sort() +print("Sorted List 1: " + str(list1) + "\n") + +print("List 2: " + str(list2)) +list2.sort() +print("Sorted List 2: " + str(list2) + "\n") + +list3 = list1 + list2 +print("Concatenated list: " + str(list3)) +list3.sort() +print("Sorted Concatenated list: " + str(list3)) \ No newline at end of file diff --git a/4_pythagorean_triplet.py b/4_pythagorean_triplet.py new file mode 100644 index 0000000..de4ee20 --- /dev/null +++ b/4_pythagorean_triplet.py @@ -0,0 +1,13 @@ +def pythagorean(): + sum = 1000 + for x in range(1, int(sum/3)+1): + for y in range(x+1, int(sum/2)+1): + z = sum - x - y + if((x Date: Sun, 18 Mar 2018 16:19:32 +0530 Subject: [PATCH 2/3] Update 7_palindrome.py --- 7_palindrome.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/7_palindrome.py b/7_palindrome.py index 80338c8..d31889a 100644 --- a/7_palindrome.py +++ b/7_palindrome.py @@ -1,7 +1,6 @@ def check_palindrome(): var = input("Enter a string to check whether its a palindrome or not: ") length = len(var) - flag = 0 low = 0 high = length-1 while(low < high): @@ -13,4 +12,4 @@ def check_palindrome(): high -= 1 print("%s is a palindrome." %(var)) -check_palindrome() \ No newline at end of file +check_palindrome() From 51f19d624a03ad82163c4a61334faf984ef4d125 Mon Sep 17 00:00:00 2001 From: Shantan Thota <36533123+tshantan24@users.noreply.github.com> Date: Sun, 18 Mar 2018 19:44:35 +0530 Subject: [PATCH 3/3] Add files via upload --- 8_binary_tree.py | 146 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 8_binary_tree.py diff --git a/8_binary_tree.py b/8_binary_tree.py new file mode 100644 index 0000000..2315096 --- /dev/null +++ b/8_binary_tree.py @@ -0,0 +1,146 @@ +class BinaryTreeNode(): + + def __init__(self, value=None): + self.leftChild = None + self.rightChild = None + if value == None: + self.value = None + else: + self.value = value + + def insertNode(self, data): + if self.value: + if data < self.value: + if self.leftChild == None: + self.leftChild = BinaryTreeNode(data) + print("Node inserted successfully.\n\n") + else: + self.leftChild.insertNode(data) + elif data > self.value: + if self.rightChild == None: + self.rightChild = BinaryTreeNode(data) + print("Node inserted successfully.\n\n") + else: + self.rightChild.insertNode(data) + elif self.value == None: + self.value = data + print("Node inserted successfully.\n\n") + + + def search(self, data, parent=None): + if data < self.value: + if self.leftChild == None: + return None, None + return self.leftChild.search(data, self) + elif data > self.value: + if self.rightChild == None: + return None, None + return self.rightChild.search(data, self) + elif data == self.value: + return self, parent + else: + return None, None + + def children_count(self): + count = 0 + if self.leftChild: + count += 1 + if self.rightChild: + count += 1 + return count + + def deleteNode(self, data): + + node, parent = self.search(data) + if (node == None and parent == None): + print("Entered node does not exist to delete.\n\n") + return + + if (parent == None and node.leftChild == None and node.rightChild == None): + node.value = None + print("Node deleted successfully.\n\n") + return + + if node != None: + child_count = node.children_count() + + if child_count == 0: + if parent: + if parent.leftChild == node: + parent.leftChild = None + else: + parent.rightChild = None + print("Node deleted successfully.\n\n") + del node + else: + parent.value = None + print("Node deleted successfully.\n\n") + + elif child_count == 1: + if node.leftChild: + n = node.leftChild + else: + n = node.rightChild + if parent: + if parent.leftChild == node: + parent.leftChild = n + else: + parent.rightChild = n + print("Node deleted successfully.\n\n") + del node + else: + self.leftChild = n.leftChild + self.rightChild = n.rightChild + self.value = n.value + print("Node deleted successfully.\n\n") + + else: + parent = node + successor = node.rightChild + while successor.leftChild: + parent = successor + successor = successor.leftChild + node.value = successor.value + if parent.leftChild == successor: + parent.leftChild = successor.rightChild + print("Node deleted successfully.\n\n") + else: + parent.rightChild = successor.rightChild + print("Node deleted successfully.\n\n") + + def printInorder(self): + try: + if self: + if self.leftChild: + self.leftChild.printInorder() + + print(self.value) + + if self.rightChild: + self.rightChild.printInorder() + except: + print("Unexpected error! Please try again.") + +def doMain(): + flag = True + root = BinaryTreeNode() + while flag: + opt = input("Enter + to add a node.\nEnter - to delete a node.\nEnter = to print tree(Inorder traversal).\nEnter 0 to quit.\nOption: ") + + if(opt == "+"): + data = input("Enter node to insert: ") + root.insertNode(data) + elif(opt == "-"): + data = input("Enter node to delete: ") + if root == None: + print("No data to delete.\n") + else: + root.deleteNode(data) + elif(opt == "="): + root.printInorder() + elif(opt == "0"): + flag = False + else: + print("Invalid option. Please re-enter.") + +doMain() \ No newline at end of file