Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Sets1and2/1_sum_5not7.py
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions Sets1and2/2_room_increase.py
Original file line number Diff line number Diff line change
@@ -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)

28 changes: 28 additions & 0 deletions Sets1and2/3_sort_and_concat.py
Original file line number Diff line number Diff line change
@@ -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))


9 changes: 9 additions & 0 deletions Sets1and2/4_pythogorean_triplets.py
Original file line number Diff line number Diff line change
@@ -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))
45 changes: 45 additions & 0 deletions Sets1and2/5_smallest_divisble_upto_n.py
Original file line number Diff line number Diff line change
@@ -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)





21 changes: 21 additions & 0 deletions Sets1and2/6_print_primes.py
Original file line number Diff line number Diff line change
@@ -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)



10 changes: 10 additions & 0 deletions Sets1and2/7_is_palindrome.py
Original file line number Diff line number Diff line change
@@ -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: ")))


65 changes: 65 additions & 0 deletions set3/8_binary_tree.py
Original file line number Diff line number Diff line change
@@ -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")



1 change: 1 addition & 0 deletions set3/newq
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello