From e4c8d31694c331ff51eb4d345456cee0ee3aac6d Mon Sep 17 00:00:00 2001 From: devarakondapranav Date: Sat, 17 Mar 2018 23:15:51 +0530 Subject: [PATCH 1/6] added programs to folder --- Sets1and2/1_sum_5not7.py | 9 +++++ Sets1and2/2_room_increase.py | 12 +++++++ Sets1and2/3_sort_and_concat.py | 14 ++++++++ Sets1and2/4_pythogorean_triplets.py | 9 +++++ Sets1and2/5_smallest_divisble_upto_n.py | 45 +++++++++++++++++++++++++ Sets1and2/6_print_primes.py | 21 ++++++++++++ Sets1and2/7_is_palindrome.py | 10 ++++++ 7 files changed, 120 insertions(+) create mode 100644 Sets1and2/1_sum_5not7.py create mode 100644 Sets1and2/2_room_increase.py create mode 100644 Sets1and2/3_sort_and_concat.py create mode 100644 Sets1and2/4_pythogorean_triplets.py create mode 100644 Sets1and2/5_smallest_divisble_upto_n.py create mode 100644 Sets1and2/6_print_primes.py create mode 100644 Sets1and2/7_is_palindrome.py diff --git a/Sets1and2/1_sum_5not7.py b/Sets1and2/1_sum_5not7.py new file mode 100644 index 0000000..43795df --- /dev/null +++ b/Sets1and2/1_sum_5not7.py @@ -0,0 +1,9 @@ +def sumMultiplesof5not7(n): + '''prints the sum of numbers less than n that are multiples of 5 but not 7 + ''' + res = 0 + for i in range(5, n, 5): + if(i%7 !=0): + res+=i + print(res) +sumMultiplesof5not7(500) diff --git a/Sets1and2/2_room_increase.py b/Sets1and2/2_room_increase.py new file mode 100644 index 0000000..ffa8f04 --- /dev/null +++ b/Sets1and2/2_room_increase.py @@ -0,0 +1,12 @@ +def printArea(increasePercent, length = 6, breadth = 8, height=10): + ''' + prints the area of the room when the dimesions are increased by a certain amount + ''' + increase = increasePercent/100 + newLength = length + (length*increase) + newBreadth = breadth + (breadth*increase) + area = newBreadth*newLength + print("The area of the room after the increase in dimensions is " + str(area)) + +printArea(15) + diff --git a/Sets1and2/3_sort_and_concat.py b/Sets1and2/3_sort_and_concat.py new file mode 100644 index 0000000..370929a --- /dev/null +++ b/Sets1and2/3_sort_and_concat.py @@ -0,0 +1,14 @@ +a = "Ron,Hermione,Harry,Professor,Dobby,List Items 2,The House Elf,Potter,Lockhart,Weasley" +list1 = a.split(",") +b = "Potter,Fred,Greg,George,Voldemort,Sirius,Dumbledore" +list2 = b.split(",") + +def sort_concat(list1, list2): + ''' + takes two lists, sorts and concatenates them to return a new list + ''' + return(sorted(list1) + sorted(list2)) + +new_list = sort_concat(list1, list2) +print(new_list) + diff --git a/Sets1and2/4_pythogorean_triplets.py b/Sets1and2/4_pythogorean_triplets.py new file mode 100644 index 0000000..689fb36 --- /dev/null +++ b/Sets1and2/4_pythogorean_triplets.py @@ -0,0 +1,9 @@ +'''Iterate for 'a' from 1 to 1000, iterate for 'b' from current 'a' value to 1000-a, get corrsponding 'c' value and check for pythogorean condition +''' + +for a in range(1, 1000): + for b in range(a, 1000-a): + c = 1000-a-b + if(a**2 + b**2 == c**2): + print( "The triplets are " + " ".join(map(str, [a, b, c]))) + print("And their product is " + str(a*b*c)) \ No newline at end of file diff --git a/Sets1and2/5_smallest_divisble_upto_n.py b/Sets1and2/5_smallest_divisble_upto_n.py new file mode 100644 index 0000000..6564a12 --- /dev/null +++ b/Sets1and2/5_smallest_divisble_upto_n.py @@ -0,0 +1,45 @@ +def smallestDivisor(n): + ''' + prints the smallest number divisible by all numbers from 1-n + Time complexity = O(n*n) + ''' + s = {} # to keep track of completed numbers. Using a dictionary will bring down search operation to O(1) + t = {} # tp keep track of completed numbers that are not directly part of the product. + res = 2 + if(n==1): + print(1) + return + for i in range(2, n+1): + ''' + iterate from 2 to n. + at each iteration + check if the current number is divisible by the previously completed value. + If divisible find out the smallest number that has to multiplied to the product. + update the product + ''' + if(len(s) == 0): + s[i] = True + cur_min = i + + for j in s.keys(): + + if(i%j==0): + temp = i/j + if(temp in s and temp!=j and temp not in t): + cur_min = 1 + break + if(temp < cur_min): + cur_min = temp + res = res*cur_min + if(cur_min != i): + t[i] = True + #print(cur_min) + s[i] = True + print(res) + +smallestDivisor(20) + + + + + diff --git a/Sets1and2/6_print_primes.py b/Sets1and2/6_print_primes.py new file mode 100644 index 0000000..f2d30e9 --- /dev/null +++ b/Sets1and2/6_print_primes.py @@ -0,0 +1,21 @@ +def isPrime(x): + '''helper function to check if a number is a prime + ''' + for i in range(2, int(x**0.5)+1): + if(x%i == 0): + return(False) + return(True) + +def printPrimes(n): + '''iterates over the given range, + checks if number is prime and prints it to the console + ''' + print("The primes from 1 to " + str(n) + " are " ) + for i in range(2, n+1): + if(isPrime(i)): + print(i) + +printPrimes(99999) + + + diff --git a/Sets1and2/7_is_palindrome.py b/Sets1and2/7_is_palindrome.py new file mode 100644 index 0000000..be92489 --- /dev/null +++ b/Sets1and2/7_is_palindrome.py @@ -0,0 +1,10 @@ +def isPalindrome(string): + l = len(string) + for i in range(int(l/2)): + if(string[i] != string[-(i+1)]): + return(False) + return(True) + +print(isPalindrome(input("Enter a word to check if it is a palindrome: "))) + + From a03d86c90d1474d2f39e0b7c213d00df4a6376d0 Mon Sep 17 00:00:00 2001 From: devarakondapranav Date: Sat, 17 Mar 2018 23:56:11 +0530 Subject: [PATCH 2/6] made changes to 3_sort_concat --- Sets1and2/3_sort_and_concat.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/Sets1and2/3_sort_and_concat.py b/Sets1and2/3_sort_and_concat.py index 370929a..57fb5e7 100644 --- a/Sets1and2/3_sort_and_concat.py +++ b/Sets1and2/3_sort_and_concat.py @@ -5,10 +5,24 @@ def sort_concat(list1, list2): ''' - takes two lists, sorts and concatenates them to return a new list + assuming the question is to + take two lists, sort the first list and concatenate these values index wise to the values from second list + ''' + list1.sort() + for i in range(len(list2)): + #print(list1[i]) + list1[i] = list1[i] + " " + list2[i] + + + + +sort_concat(list1, list2) +print(list1) + +def sort1_concat(list1, list2): + ''' + if the question is to sort the lists and simply concatenate them ''' return(sorted(list1) + sorted(list2)) -new_list = sort_concat(list1, list2) -print(new_list) From 6ce9cf6d0ec36dc15cd17aae7d7e487144170846 Mon Sep 17 00:00:00 2001 From: devarakondapranav Date: Sun, 18 Mar 2018 16:53:43 +0530 Subject: [PATCH 3/6] Create newq --- set3/newq | 1 + 1 file changed, 1 insertion(+) create mode 100644 set3/newq diff --git a/set3/newq b/set3/newq new file mode 100644 index 0000000..ce01362 --- /dev/null +++ b/set3/newq @@ -0,0 +1 @@ +hello From 28ddf4085d892d2b227727f14ea92a3261d35b76 Mon Sep 17 00:00:00 2001 From: devarakondapranav Date: Sun, 18 Mar 2018 16:54:27 +0530 Subject: [PATCH 4/6] Add files via upload --- set3/8_binary_tree.py | 52 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 set3/8_binary_tree.py diff --git a/set3/8_binary_tree.py b/set3/8_binary_tree.py new file mode 100644 index 0000000..7fdd9c8 --- /dev/null +++ b/set3/8_binary_tree.py @@ -0,0 +1,52 @@ +class BinaryTreeNode: + def __init__(self, value, left=None, right=None): + self.value=value; + self.left = left + self.right = right + + def __add__(self, other): + if(self.left == None): + self.left = other + print("Added " +str(other.value) + " as left child of " + str(self.value)) + elif(self.right == None): + self.right = other + print("Added " +str(other.value) + " as right child of " + str(self.value)) + else: + print("The current node is not vacant") + + def __sub__(self, other): + if(self.left == other): + self.left = None + print("Deleted the left node of " + str(self.value)) + elif(self.right == other): + self.right = None + print("Deleted the right node of" + str(self.value)) + else: + print(str(other.value) +" is not a child of " + str(self.value)) + + def printTree(self, root): + #inorder traversal of the tree + print(root.value) + if(root.left !=None): + self.printTree(root.left) + if(root.right!=None): + self.printTree(root.right) + +root = BinaryTreeNode(1) +a = BinaryTreeNode(2) +b = BinaryTreeNode(3) +c = BinaryTreeNode(4) + +root+a#a is added as left child of root +root+b#b is added as right child of root +b + c#c is added as the left child of b + + +root.printTree(root) + +b-c#c is removed as the left child of b + +root.printTree(root) + + + From 42cc1496d447dd87a06c0f4ceea25785a379f09d Mon Sep 17 00:00:00 2001 From: devarakondapranav Date: Sun, 18 Mar 2018 17:01:39 +0530 Subject: [PATCH 5/6] Update 8_binary_tree.py --- set3/8_binary_tree.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/set3/8_binary_tree.py b/set3/8_binary_tree.py index 7fdd9c8..02adc35 100644 --- a/set3/8_binary_tree.py +++ b/set3/8_binary_tree.py @@ -41,11 +41,24 @@ def printTree(self, root): root+b#b is added as right child of root b + c#c is added as the left child of b +'''Tree is now + 1 + / \ + 2 3 + / + 4 +''' root.printTree(root) b-c#c is removed as the left child of b +'''Tree is now + 1 + / \ + 2 3 +''' + root.printTree(root) From 09bca8d28c94f315b63ab6e45103e676a4778c17 Mon Sep 17 00:00:00 2001 From: devarakondapranav Date: Sun, 18 Mar 2018 18:44:43 +0530 Subject: [PATCH 6/6] Update 8_binary_tree.py --- set3/8_binary_tree.py | 106 +++++++++++++++++++++--------------------- 1 file changed, 53 insertions(+), 53 deletions(-) diff --git a/set3/8_binary_tree.py b/set3/8_binary_tree.py index 02adc35..9ffe21a 100644 --- a/set3/8_binary_tree.py +++ b/set3/8_binary_tree.py @@ -1,65 +1,65 @@ class BinaryTreeNode: + #implments a complete binary tree def __init__(self, value, left=None, right=None): - self.value=value; - self.left = left - self.right = right + self.value=value; + self.nodes = [self.value] def __add__(self, other): - if(self.left == None): - self.left = other - print("Added " +str(other.value) + " as left child of " + str(self.value)) - elif(self.right == None): - self.right = other - print("Added " +str(other.value) + " as right child of " + str(self.value)) - else: - print("The current node is not vacant") - - def __sub__(self, other): - if(self.left == other): - self.left = None - print("Deleted the left node of " + str(self.value)) - elif(self.right == other): - self.right = None - print("Deleted the right node of" + str(self.value)) - else: - print(str(other.value) +" is not a child of " + str(self.value)) - - def printTree(self, root): - #inorder traversal of the tree - print(root.value) - if(root.left !=None): - self.printTree(root.left) - if(root.right!=None): - self.printTree(root.right) - -root = BinaryTreeNode(1) -a = BinaryTreeNode(2) -b = BinaryTreeNode(3) -c = BinaryTreeNode(4) + #Nodes are added to the end of the tree as in CBT + self.nodes.append(other.value) + #print("Node added at the end of the tree") -root+a#a is added as left child of root -root+b#b is added as right child of root -b + c#c is added as the left child of b -'''Tree is now - 1 - / \ - 2 3 - / - 4 -''' + -root.printTree(root) - -b-c#c is removed as the left child of b + def __sub__(self, other): + #Since there are no constraints on how to delete nodes + #the specified node is deleted and the last node in the tree is put in its place + if other.value not in self.nodes: + print(str(other.value) + " is not in the tree") + else: + curPosition = self.nodes.index(other.value) + self.nodes[curPosition] = self.nodes[-1] + self.nodes.remove(self.nodes[-1]) + print("Node is " + "Deleted") + if(len(self.nodes) == 0): + root = None -'''Tree is now - 1 - / \ - 2 3 -''' -root.printTree(root) + def printTree(self, root): + #level order traversal + if(root==None): + print("Tree is empty") + print("*********") + for i in range(len(self.nodes)): + print(self.nodes[i], end = " ") + print(" ") + print("*********") + +root = None +while(True): + print("Select on option") + print("1. Insert a node") + print("2. Delete a node") + print("3. Print the tree") + choice = int(input()) + if(choice ==1): + if(root==None): + root = BinaryTreeNode(int(input('Enter the value to insert...'))) + print("Done inserting at the root") + else: + root + BinaryTreeNode(int(input('Enter the value to insert...'))) + print("Node is inserted") + elif(choice == 2): + root - BinaryTreeNode(int(input("Enter the node to be deleted"))) + elif(choice ==3): + if(root==None): + print("Tree is empty") + else: + print("The tree is ") + root.printTree(root) + else: + print("Choose a valid option")