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..57fb5e7 --- /dev/null +++ b/Sets1and2/3_sort_and_concat.py @@ -0,0 +1,28 @@ +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): + ''' + 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)) + + 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: "))) + + diff --git a/set3/8_binary_tree.py b/set3/8_binary_tree.py new file mode 100644 index 0000000..9ffe21a --- /dev/null +++ b/set3/8_binary_tree.py @@ -0,0 +1,65 @@ +class BinaryTreeNode: + #implments a complete binary tree + def __init__(self, value, left=None, right=None): + self.value=value; + self.nodes = [self.value] + + def __add__(self, other): + #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") + + + + + 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 + + + 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") + + + diff --git a/set3/newq b/set3/newq new file mode 100644 index 0000000..ce01362 --- /dev/null +++ b/set3/newq @@ -0,0 +1 @@ +hello