Skip to content
Merged
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
Binary file added Assignment 3/Python Assignment 3 Qs.pdf
Binary file not shown.
18 changes: 18 additions & 0 deletions Assignment 3/Question_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Write a function called check-season, it takes a month parameter and returns the season: Autumn, Winter, Spring or Summer.

def check_season(month):
month = month.lower()
if month in ["march", "april", "may"]:
return "Spring"
elif month in ["june", "july", "august"]:
return "Summer"
elif month in ["september", "october", "november"]:
return "Autumn"
elif month in ["december", "january", "february"]:
return "Winter"
else:
return "Invalid month"

month = input("Enter the name of the month: ")
season = check_season(month)
print(f"The {month.capitalize()} month falls under {season} season.")
42 changes: 42 additions & 0 deletions Assignment 3/Question_10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
Print a number as a 8 segment display N Lines:
_
_| N=2
|_
_
_|
_| N=3

|_|
| N=4
"""

def print_digit(digit):
segments = [
" _ \n| |\n|_|", # 0
" \n |\n |", # 1
" _ \n |\n| ", # 2
" _ \n _|\n _|", # 3
" \n|_|\n |", # 4
" _ \n|_ \n _|", # 5
" _ \n|_ \n|_|", # 6
" _ \n |\n |", # 7
" _ \n||\n||", # 8
" _ \n|_|\n _|" # 9
]
return segments[digit]

def print_number_as_segment_display(N):
number_str = str(N)
lines = ["", "", ""]
for digit_char in number_str:
digit = int(digit_char)
segment_lines = print_digit(digit).split('\n')
for i in range(3):
lines[i] += segment_lines[i] + " "

for line in lines:
print(line)

N = int(input("Enter the number N: "))
print_number_as_segment_display(N)
43 changes: 43 additions & 0 deletions Assignment 3/Question_11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'''
Print the pattern upto N lines:
1 2
4 3 N=2

1 2 3
8 9 4
7 6 5 N=3

1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7 N=4

'''

def print_pattern(n):
matrix = [[0] * n for _ in range(n)]
start= 0
end = n
num = 1

while start < end:
for i in range(start, end):
matrix[start][i] = num
num += 1
for i in range(start + 1, end):
matrix[i][end - 1] = num
num += 1
for i in range(end - 2, start - 1, -1):
matrix[end - 1][i] = num
num += 1
for i in range(end - 2, start, -1):
matrix[i][start] = num
num += 1
start += 1
end -= 1

for row in matrix:
print(" ".join(str(x) for x in row))

n = int(input("Enter the number of terms: "))
print_pattern(n)
18 changes: 18 additions & 0 deletions Assignment 3/Question_12.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'''
Write a python script that displays the following table
1 1 1 1 1
2 1 2 4 8
3 1 3 9 27
4 1 4 16 64
5 1 5 25 125
'''

def print_table(n):
for i in range(1,n+1):
print(i,end=' ')
for j in range(4):
print(i**j,end=' ')
print()

n = int(input("Enter the number of terms: "))
print_table(n)
13 changes: 13 additions & 0 deletions Assignment 3/Question_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Write a function called calculate_slope which return the slope of a linear equation

def calculate_slope(x1, y1, x2, y2):
if x1 == x2:
return "Undefined"
return (y2 - y1) / (x2 - x1)

x1 = float(input("Enter x1: "))
y1 = float(input("Enter y1: "))
x2 = float(input("Enter x2: "))
y2 = float(input("Enter y2: "))
slope = calculate_slope(x1, y1, x2, y2)
print(f"The slope of the line between ({x1}, {y1}) and ({x2}, {y2}) is {slope}")
38 changes: 38 additions & 0 deletions Assignment 3/Question_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Quadratic equation is calculated as follows: ax² + bx + c = 0. Write a function which calculates solution set of a quadratic equation, _solve_quadratic_eqn_.

import math

def solve_quadratic_eqn_(a, b, c):
if a == 0:
if b == 0:
return "No solution" if c != 0 else "Infinite solutions"
return [-c / b]

discriminant = b ** 2 - 4 * a * c

if discriminant > 0:
root1 = (-b + math.sqrt(discriminant)) / (2 * a)
root2 = (-b - math.sqrt(discriminant)) / (2 * a)
return [root1, root2]
elif discriminant == 0:
root = -b / (2 * a)
return [root]
else:
real_part = -b / (2 * a)
imaginary_part = math.sqrt(-discriminant) / (2 * a)
root1 = complex(real_part, imaginary_part)
root2 = complex(real_part, -imaginary_part)
return [root1, root2]


print("Quadratic Equation Solver: ax² + bx + c = 0")
a = float(input("Enter coefficient a: "))
b = float(input("Enter coefficient b: "))
c = float(input("Enter coefficient c: "))
solutions = solve_quadratic_eqn_(a, b, c)
if isinstance(solutions, str):
print(solutions)
else:
for i, solution in enumerate(solutions, start=1):
print(f"Solution {i}: {solution}")

14 changes: 14 additions & 0 deletions Assignment 3/Question_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Declare a function named print_list. It takes a list as a parameter and it prints out each element of the list

def print_list(elements):
for element in elements:
print(element)

list = []
limit = int(input("Enter the number of elements to be present in the list: "))
r=0
while(r<limit):
list.append(int(input("Enter element in the list: ")))
r+=1

print_list(list)
14 changes: 14 additions & 0 deletions Assignment 3/Question_5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Declare a function named reverse_list. It takes an array as a parameter and it returns the reverse of the array (use loops).

def reverse_list(array):
reversed_array = []
for i in range(len(array) - 1, -1, -1):
reversed_array.append(array[i])
return reversed_array

array = [None] * 5
for i in range(len(array)):
array[i] = int(input("Enter element in the array: "))
reversed_array = reverse_list(array)
print("Reversed array: ",reversed_array)

15 changes: 15 additions & 0 deletions Assignment 3/Question_6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Compute the sum up to n terms in the series 1 - 1/2 + 1/3 - 1/4 + 1/5 -... 1/n where n is a positive integer and input by user.

def compute_sum(r):
total_sum = 0.0
for i in range(1, r+1):
if(i%2 == 0):
total_sum -= 1/i
else:
total_sum += 1/i
return total_sum

r = int(input("Enter the range: "))

compute = compute_sum(r)
print(f"Series upta {r} terms is: {compute}")
20 changes: 20 additions & 0 deletions Assignment 3/Question_7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Write a program to compute sin x for given x. The user should supply x and a positive integer n. We compute the sine of x using the series and the computation should use all terms in the series up through the term involving xn sin x = x - x3/3! + x5/5! - x7/7! + x9/9! ........

def factorial(num):
result = 1
for i in range(2, num + 1):
result *= i
return result

def sin_x(x, n):
sin_x_value = 0.0
for i in range(n):
term = ((-1) ** i) * (x ** (2 * i + 1)) / factorial(2 * i + 1)
sin_x_value += term
return sin_x_value

x = float(input("Enter the value of x in radians: "))
n = int(input("Enter the number of terms n: "))

sin_x_value = sin_x(x, n)
print(f"The value of sin ({x}) using {n} terms is: {sin_x_value}")
20 changes: 20 additions & 0 deletions Assignment 3/Question_8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Write a program to compute cosine of x. The user should supply x and a positive integer n. We compute the cosine of x using the series and the computation should use all terms in the series up through the term involving xn cos x = 1 - x2/2! + x4/4! - x6/6! ....

def factorial(num):
result = 1
for i in range(2, num + 1):
result *= i
return result

def cos_x(x, n):
cos_x_value = 0.0
for i in range(n + 1):
term = ((-1) ** i) * (x ** (2 * i)) / factorial(2 * i)
cos_x_value += term
return cos_x_value

x = float(input("Enter the value of x in radians: "))
n = int(input("Enter the number of terms n: "))

cos_x_value = cos_x(x, n)
print(f"The value of cos({x}) using {n} terms is: {cos_x_value}")
24 changes: 24 additions & 0 deletions Assignment 3/Question_9.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'''
Print the pattern upto N Lines:
.
/_\ N=2
.
/ \
/___\ N=3
.
/ \
/ \
/_____\ N=4

'''

def print_triangle(n):
print(" " * n + ".")

for i in range(1, n):
print(" " * (n - i - 1), end="")
print(" /" + " " * (2 * i - 1) + "\\")
print("/" + "_" * (2 * n - 1) + "\\")

n = int(input("Enter the value of N: "))
print_triangle(n)