Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit e4c8d31

Browse files
added programs to folder
1 parent e3039eb commit e4c8d31

7 files changed

+120
-0
lines changed

Sets1and2/1_sum_5not7.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def sumMultiplesof5not7(n):
2+
'''prints the sum of numbers less than n that are multiples of 5 but not 7
3+
'''
4+
res = 0
5+
for i in range(5, n, 5):
6+
if(i%7 !=0):
7+
res+=i
8+
print(res)
9+
sumMultiplesof5not7(500)

Sets1and2/2_room_increase.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def printArea(increasePercent, length = 6, breadth = 8, height=10):
2+
'''
3+
prints the area of the room when the dimesions are increased by a certain amount
4+
'''
5+
increase = increasePercent/100
6+
newLength = length + (length*increase)
7+
newBreadth = breadth + (breadth*increase)
8+
area = newBreadth*newLength
9+
print("The area of the room after the increase in dimensions is " + str(area))
10+
11+
printArea(15)
12+

Sets1and2/3_sort_and_concat.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
a = "Ron,Hermione,Harry,Professor,Dobby,List Items 2,The House Elf,Potter,Lockhart,Weasley"
2+
list1 = a.split(",")
3+
b = "Potter,Fred,Greg,George,Voldemort,Sirius,Dumbledore"
4+
list2 = b.split(",")
5+
6+
def sort_concat(list1, list2):
7+
'''
8+
takes two lists, sorts and concatenates them to return a new list
9+
'''
10+
return(sorted(list1) + sorted(list2))
11+
12+
new_list = sort_concat(list1, list2)
13+
print(new_list)
14+

Sets1and2/4_pythogorean_triplets.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'''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
2+
'''
3+
4+
for a in range(1, 1000):
5+
for b in range(a, 1000-a):
6+
c = 1000-a-b
7+
if(a**2 + b**2 == c**2):
8+
print( "The triplets are " + " ".join(map(str, [a, b, c])))
9+
print("And their product is " + str(a*b*c))
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
def smallestDivisor(n):
2+
'''
3+
prints the smallest number divisible by all numbers from 1-n
4+
Time complexity = O(n*n)
5+
'''
6+
s = {} # to keep track of completed numbers. Using a dictionary will bring down search operation to O(1)
7+
t = {} # tp keep track of completed numbers that are not directly part of the product.
8+
res = 2
9+
if(n==1):
10+
print(1)
11+
return
12+
for i in range(2, n+1):
13+
'''
14+
iterate from 2 to n.
15+
at each iteration
16+
check if the current number is divisible by the previously completed value.
17+
If divisible find out the smallest number that has to multiplied to the product.
18+
update the product
19+
'''
20+
if(len(s) == 0):
21+
s[i] = True
22+
cur_min = i
23+
24+
for j in s.keys():
25+
26+
if(i%j==0):
27+
temp = i/j
28+
if(temp in s and temp!=j and temp not in t):
29+
cur_min = 1
30+
break
31+
if(temp < cur_min):
32+
cur_min = temp
33+
res = res*cur_min
34+
if(cur_min != i):
35+
t[i] = True
36+
#print(cur_min)
37+
s[i] = True
38+
print(res)
39+
40+
smallestDivisor(20)
41+
42+
43+
44+
45+

Sets1and2/6_print_primes.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def isPrime(x):
2+
'''helper function to check if a number is a prime
3+
'''
4+
for i in range(2, int(x**0.5)+1):
5+
if(x%i == 0):
6+
return(False)
7+
return(True)
8+
9+
def printPrimes(n):
10+
'''iterates over the given range,
11+
checks if number is prime and prints it to the console
12+
'''
13+
print("The primes from 1 to " + str(n) + " are " )
14+
for i in range(2, n+1):
15+
if(isPrime(i)):
16+
print(i)
17+
18+
printPrimes(99999)
19+
20+
21+

Sets1and2/7_is_palindrome.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def isPalindrome(string):
2+
l = len(string)
3+
for i in range(int(l/2)):
4+
if(string[i] != string[-(i+1)]):
5+
return(False)
6+
return(True)
7+
8+
print(isPalindrome(input("Enter a word to check if it is a palindrome: ")))
9+
10+

0 commit comments

Comments
 (0)