Skip to content

Commit d2ba347

Browse files
authored
Add files via upload
1 parent 05e0d08 commit d2ba347

21 files changed

+566
-0
lines changed

Assignment1.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
def add(x, y):
2+
print(a,"+",b,"=", x+y);
3+
4+
def subtract(x, y):
5+
print(a,"-",b,"=", x-y);
6+
7+
def multiply(x, y):
8+
print(a,"+",b,"=", x*y);
9+
10+
def divide(x, y):
11+
print(a,"+",b,"=", x/y);
12+
13+
m = 'y'
14+
15+
while m == 'y' or m == 'Y':
16+
print("Calculator")
17+
print("1.Addition")
18+
print("2.Subtraction")
19+
print("3.Multiplication")
20+
print("4.Division")
21+
choice=input("Enter your Choice: ")
22+
23+
if choice == '1':
24+
a=float(input('Enter value of a: '))
25+
b=float(input('Enter value of b: '))
26+
add(a,b)
27+
m=input('Do you want to restart the calculator: ')
28+
29+
elif choice == '2':
30+
a=float(input('Enter value of a: '))
31+
b=float(input('Enter value of b: '))
32+
subtract(a,b)
33+
m=input('Do you want to restart the calculator: ')
34+
35+
elif choice == '3':
36+
a=float(input('Enter value of a: '))
37+
b=float(input('Enter value of b: '))
38+
multiply(a,b)
39+
m=input('Do you want to restart the calculator: ')
40+
41+
elif choice == '4':
42+
a=float(input('Enter value of a: '))
43+
b=float(input('Enter value of b: '))
44+
divide(a,b)
45+
m=input('Do you want to restart the calculator: ')
46+
47+
else:
48+
print("Invalid input")

Assignment2.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import math
2+
class Complex:
3+
4+
def __init__(self, r=0, i=0):
5+
self.r = r
6+
self.i = i
7+
8+
def __add__(self, n):
9+
return Complex(self.r + n.r, self.i + n.i)
10+
11+
def __sub__(self, n):
12+
return Complex(self.r - n.r, self.i - n.i)
13+
14+
def __mul__(self, n):
15+
real = (self.r*n.r - self.i*n.i)
16+
imag = (self.r*n.i + self.i*n.r)
17+
return Complex(real, imag)
18+
19+
def __div__(self, n):
20+
real = (self.r*n.r + self.i*n.i)/(n.r**2 + n.i**2)
21+
imag = (self.i*n.r - self.r*n.i)/(n.r**2 + n.i**2)
22+
return Complex(real, imag)
23+
24+
def __abs__(self):
25+
return (math.sqrt(self.r**2 + self.i**2))
26+
27+
def __str__(self):
28+
29+
if (self.i == 0):
30+
return ("%.2f" %(self.r))
31+
elif (self.r == 0):
32+
return ("%.2fi" %(self.i))
33+
else:
34+
if (self.i > 0):
35+
return ("%.2f + %.2fi" %(self.r, self.i))
36+
elif (self.i < 0):
37+
return ("%.2f - %.2fi" %(self.r, -1*self.i))
38+
39+
re_1 = float(input("Enter real part of first number : "))
40+
img_1 = float(input("Enter imaginary part of first number : "))
41+
re_2 = float(input("Enter real part of second number : "))
42+
img_2 = float(input("Enter imaginary part of second number : "))
43+
44+
x = Complex(re_1, img_1)
45+
y = Complex(re_2, img_2)
46+
47+
add = x + y
48+
sub = x - y
49+
mul = x * y
50+
div = x.__div__(y)
51+
modx = x.__abs__()
52+
mody = y.__abs__()
53+
print (add)
54+
print (sub)
55+
print (mul)
56+
print (div)
57+
print ("%.2f" %(modx))
58+
print ("%.2f" %(mody))

Binary_Search.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def binarySearch(alist, item):
2+
first=0
3+
last=len(alist)-1
4+
found = False
5+
6+
while first<=last and not found:
7+
midpoint=(first+last)//2
8+
if alist[midpoint]==item:
9+
found =True
10+
else:
11+
if item<alist[midpoint]:
12+
last=midpoint-1
13+
else:
14+
first=midpoint+1
15+
return found
16+
print("Enter the number to search")
17+
num=int(input())
18+
alist=[0, 1, 2, 8, 13, 17, 19, 32, 42,]
19+
z=(binarySearch(alist,num))
20+
if z==True:
21+
print("Number is present in the list")
22+
else:
23+
print("Number is not present in the list")

Even.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
option='y'
2+
while option=='y':
3+
print("Enter the number")
4+
num=int(input())
5+
if(num%2==0):
6+
print(str(num)+" is an even number")
7+
else:
8+
print(str(num)+" is a odd number")
9+
print("Do you want to continue?(y/n)")
10+
option=input()
11+
print('Thank you for using this programme')

Factorial_Iteration.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
option='y'
2+
while option=='y':
3+
print("Enter the number whose factorial to find")
4+
num=int(input())
5+
fac=1
6+
while(num!=1):
7+
fac=fac*num
8+
num=num-1
9+
print("Factorial is "+str(fac))
10+
print("Do you want to continue?(y/n)")
11+
option=input()
12+
print('Thank you for using this programme')

Factorial_Recursion.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
option='y'
2+
while option=='y':
3+
def factorial(n):
4+
if n == 1:
5+
return 1
6+
else:
7+
return n * factorial(n-1)
8+
9+
print("Enter the number whose factorial to find")
10+
num=int(input())
11+
12+
print(factorial(num))
13+
print("Do you want to continue?(y/n)")
14+
option=input()
15+
print('Thank you for using this programme')

Fibonacci_Iteration.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
option='y'
2+
while option=='y':
3+
def fibo(n):
4+
a=0
5+
b=1
6+
for i in range(0,n):
7+
temp=a
8+
a=b
9+
b=temp+b
10+
return a
11+
12+
print("Enter the limit of fibonacci series")
13+
num=int(input())
14+
15+
for c in range(0,num):
16+
print (fibo(c))
17+
print("Do you want to continue?(y/n)")
18+
option=input()
19+
print('Thank you for using this programme')

Fibonacci_Recursion.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
option='y'
2+
while option=='y':
3+
def recur_fibo(n):
4+
"""Recursive function to
5+
print Fibonacci sequence"""
6+
if n <= 1:
7+
return n
8+
else:
9+
return(recur_fibo(n-1) + recur_fibo(n-2))
10+
11+
12+
# take input from the user
13+
nterms = int(input("How many terms? "))
14+
15+
# check if the number of terms is valid
16+
if nterms <= 0:
17+
print("Please enter a positive integer")
18+
else:
19+
print("Fibonacci sequence:")
20+
for i in range(nterms):
21+
print(recur_fibo(i))
22+
print("Do you want to continue?(y/n)")
23+
option=input()
24+
print('Thank you for using this programme')

First.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
def Multiply(x,y):
2+
z = x * y
3+
print("Answer: ",z)
4+
5+
def Divide(x,y):
6+
if y != 0:
7+
z = x / y
8+
print("Answer: ",z)
9+
else:
10+
print("Division by 0 is not defined")
11+
12+
def Add(x,y):
13+
z = x + y
14+
print("Answer: ",z)
15+
16+
def Subtract(x,y):
17+
z = x - y
18+
print("Answer: ",z)
19+
20+
ch = "y"
21+
22+
while ch == "Y" or ch == "y":
23+
24+
operation = input("What would you like to do?\n1.Add\n2.Subtract\n3.Multiply\n4.Divide ")
25+
26+
if operation == "1":
27+
x = float(input("First number: "))
28+
y = float(input("Second number: "))
29+
Add(x,y)
30+
elif operation == "2":
31+
x = float(input("First number: "))
32+
y = float(input("Second number: "))
33+
Subtract(x,y)
34+
elif operation == "3":
35+
x = float(input("First number: "))
36+
y = float(input("Second number: "))
37+
Multiply(x,y)
38+
elif operation == "4":
39+
x = float(input("First number: "))
40+
y = float(input("Second number: "))
41+
Divide(x,y)
42+
else:
43+
print("Wrong Input")
44+
ch = input("Would you like to continue ? y/n")

Largest.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
option='y'
2+
while option=='y':
3+
print("Enter first number")
4+
num1=int(input())
5+
print("Enter second number")
6+
num2=int(input())
7+
print("Enter third number")
8+
num3=int(input())
9+
if(num1>=num2&num1>=num3):
10+
print(str(num1)+" is largest among these numbers")
11+
elif(num2>=num1&num2>=num3):
12+
print(str(num1)+" is largest among these numbers")
13+
else:
14+
print(str(num3)+" is largest among these numbers")
15+
print("Do you want to continue?(y/n)")
16+
option=input()
17+
print('Thank you for using this programme')
18+

MiniAssignment.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import pygame, random, sys
2+
from pygame.locals import *
3+
pygame.init()
4+
5+
window_height=500
6+
window_width=500
7+
8+
black = (0,0,0)
9+
green = (0, 255, 0)
10+
fps = 20
11+
speed = 10
12+
font = pygame.font.SysFont(None, 28)
13+
mainClock = pygame.time.Clock()
14+
15+
Canvas = pygame.display.set_mode((window_width,window_height))
16+
pygame.display.set_caption('MINI PROJECT')
17+
18+
pygame.display.update()
19+
20+
ballimage = pygame.image.load('ball.png')
21+
ballrect = ballimage.get_rect()
22+
23+
24+
while True:
25+
moveup = movedown = moveright = moveleft = False
26+
ballrect.centerx = ballrect.centery = 250
27+
28+
while True:
29+
for event in pygame.event.get():
30+
if event.type == KEYDOWN:
31+
if event.key == K_UP:
32+
moveup = True
33+
34+
if event.key == K_DOWN:
35+
movedown = True
36+
37+
if event.key == K_LEFT:
38+
moveleft = True
39+
40+
if event.key == K_RIGHT:
41+
moveright = True
42+
43+
if event.key == K_ESCAPE:
44+
pygame.quit()
45+
sys.exit()
46+
47+
48+
if event.type == KEYUP:
49+
50+
if event.key == K_UP:
51+
moveup = False
52+
53+
if event.key == K_DOWN:
54+
movedown = False
55+
56+
if event.key == K_LEFT:
57+
moveleft = False
58+
59+
if event.key == K_RIGHT:
60+
moveright = False
61+
62+
if (moveup and (ballrect.top > 0)):
63+
ballrect.top-= speed
64+
if (movedown and (ballrect.bottom < 500)):
65+
ballrect.bottom += speed
66+
if (moveleft and (ballrect.left > 0)):
67+
ballrect.left -= speed
68+
if (moveright and (ballrect.right < 500)):
69+
ballrect.right += speed
70+
71+
x = str(ballrect.centerx)
72+
y = str(ballrect.centery)
73+
t = ','
74+
m = '('
75+
n = ')'
76+
77+
78+
pos_x = font.render(x, 1, green)
79+
pos_y = font.render(y, 1, green)
80+
pos_c = font.render(t, 1, green)
81+
pos_m = font.render(m, 1, green)
82+
pos_n = font.render(n, 1, green)
83+
84+
85+
pos_xrect = pos_x.get_rect()
86+
pos_yrect = pos_y.get_rect()
87+
pos_crect = pos_c.get_rect()
88+
pos_mrect = pos_m.get_rect()
89+
pos_nrect = pos_n.get_rect()
90+
91+
pos_xrect.topleft = (10, 10)
92+
pos_crect.topleft = (45, 10)
93+
pos_yrect.topleft = (50, 10)
94+
pos_mrect.topleft = (5, 10)
95+
pos_nrect.topleft = (85, 10)
96+
97+
Canvas.fill(black)
98+
Canvas.blit(pos_x, pos_xrect)
99+
Canvas.blit(pos_c, pos_crect)
100+
Canvas.blit(pos_y, pos_yrect)
101+
Canvas.blit(pos_m, pos_mrect)
102+
Canvas.blit(pos_n, pos_nrect)
103+
104+
105+
Canvas.blit(ballimage, ballrect)
106+
pygame.display.update()
107+
108+
mainClock.tick(fps)
109+
110+
111+
pygame.display.update()

0 commit comments

Comments
 (0)