Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
format python files
Browse files Browse the repository at this point in the history
  • Loading branch information
harshraj8843 committed Jun 16, 2024
1 parent e8052d7 commit 4989e48
Show file tree
Hide file tree
Showing 26 changed files with 248 additions and 220 deletions.
9 changes: 8 additions & 1 deletion program/program/add-two-matrices/add_two_matrices.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@ def get_matrix_input(rows, cols):
matrix.append(row)
return matrix


def add_matrices(matrix1, matrix2):
"""Function to add two matrices."""
return [[matrix1[i][j] + matrix2[i][j] for j in range(len(matrix1[0]))] for i in range(len(matrix1))]
return [
[matrix1[i][j] + matrix2[i][j] for j in range(len(matrix1[0]))]
for i in range(len(matrix1))
]


def print_matrix(matrix):
"""Function to print the matrix."""
for row in matrix:
print(" ".join(map(str, row)))


def main():
rows = int(input("Enter the number of rows for the matrices: "))
cols = int(input("Enter the number of columns for the matrices: "))
Expand All @@ -34,5 +40,6 @@ def main():
print("Result of adding Matrix 1 and Matrix 2:")
print_matrix(result_matrix)


if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ def ncr(n, r):
return 1
r = min(r, n - r) # take advantage of symmetry
# ncr = n! / (n-r)! * r! = n*(n-1)*(n-2)*..*(n-r+1)*(n-r)! / (n-r)! * r! = n*(n-1)*(n-2)*..*(n-r+1) / r!
numerator = 1 # (n-r)!
denominator = 1 # r!
numerator = 1 # (n-r)!
denominator = 1 # r!
for i in range(r):
numerator *= (n - i)
denominator *= (i + 1)
numerator *= n - i
denominator *= i + 1
return numerator // denominator
n=int(input("Enter the n value: "))
r=int(input("Enter the r value: "))
print("npr value is:", ncr(n,r))


n = int(input("Enter the n value: "))
r = int(input("Enter the r value: "))
print("npr value is:", ncr(n, r))
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
def npr(n, r):
if r > n:
return 0
# n! / (n-r)! = n*(n-1)*(n-2)*..*(n-r+1)*(n-r)! / (n-r)! =n*(n-1)*(n-2)*..*(n-r+1)
# n! / (n-r)! = n*(n-1)*(n-2)*..*(n-r+1)*(n-r)! / (n-r)! =n*(n-1)*(n-2)*..*(n-r+1)
result = 1
for i in range(n, n - r, -1):
result *= i
return result
n=int(input("Enter the n value: "))
r=int(input("Enter the r value: "))
print("npr value is:", npr(n,r))


n = int(input("Enter the n value: "))
r = int(input("Enter the r value: "))
print("npr value is:", npr(n, r))
6 changes: 3 additions & 3 deletions program/program/check-anagram-string/check_anagram_string.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Function to check if string is anagram or not
def isanagram(str1,str2):
return sorted(str1)== sorted(str2):
def isanagram(str1, str2):
return sorted(str1) == sorted(str2)


string1 = input()
string2 = input()
if isanagram(string1,string2) == True
if isanagram(string1, string2) == True:
print("Anagram Strings")
else:
print("Not Anagram Strings")
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@

# Solution
def checkEven(num):
if(num>0):
if num > 0:
return (num % 2) == 0
else:
print("Please give number greater than 0")



isEven = checkEven(3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ def fahrenheit_to_kelvin(temp_f):
if __name__ == "__main__":
fahrenheit = int(input("Fahrenheit input: "))
print("Kelvin:", fahrenheit_to_kelvin(fahrenheit))

Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import datetime

#Function to convert standard input date into julian date
def stddate_to_jd (std_date):
fmt='%Y-%m-%d'

# Function to convert standard input date into julian date
def stddate_to_jd(std_date):
fmt = "%Y-%m-%d"
converted_date = datetime.datetime.strptime(std_date, fmt)
converted_date = converted_date.timetuple()
jul_date = str(converted_date.tm_year) + '' + str(converted_date.tm_yday)
return(jul_date)
jul_date = str(converted_date.tm_year) + "" + str(converted_date.tm_yday)
return jul_date


#Function to convert julian input date into standard date
def jd_to_stddate (jdate):
fmt = '%Y%j'
# Function to convert julian input date into standard date
def jd_to_stddate(jdate):
fmt = "%Y%j"
date_std = datetime.datetime.strptime(jdate, fmt).date()
return(date_std)
return date_std



print('Please provide input date to be converted : ')
print("Please provide input date to be converted : ")
input_date = input()

if len(input_date) == 10:
return_val = stddate_to_jd(input_date)
print("Converted date is : ", return_val)
return_val = stddate_to_jd(input_date)
print("Converted date is : ", return_val)
elif len(input_date) == 7:
return_val = jd_to_stddate(input_date)
print("Converted date is : " ,return_val)
return_val = jd_to_stddate(input_date)
print("Converted date is : ", return_val)
else:
print("Incorrect format of date. Please provide correct format.")
print("Incorrect format of date. Please provide correct format.")
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
C = input("Input (C) : ")

if float(C) > -273.15:
K = (float(C) + 273.15)
K = float(C) + 273.15

print(f"Output (K) : {K}")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
lst = list(map(int, input().split(" ")))
sum = 0
n=len(lst)
n = len(lst)
for i in range(0, n):
sum = sum + lst[i]
print(sum / n)
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
def factorial_dp(n):
# Initialize a list to store factorial results
factorial = [1] * (n + 1)

# Base case
if n >= 0:
factorial[0] = 1

# Bottom-up DP approach
for i in range(2, n + 1):
factorial[i] = factorial[i - 1] * i

return factorial[n]


# Example usage
n = int(input("Enter a non-negative integer to calculate factorial: "))
result = factorial_dp(n)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#Importing Complex math module
# Importing Complex math module
import cmath

print("Enter the quadratic equation in the format: ax^2+bx+c")

a=int(input("Enter a"))
b=int(input("Enter b"))
c=int(input("Enter c"))
a = int(input("Enter a"))
b = int(input("Enter b"))
c = int(input("Enter c"))

#Evaluating the discriminant using formula: d= b^2-4ac
d = (b**2)-(4*a*c)
# Evaluating the discriminant using formula: d= b^2-4ac
d = (b**2) - (4 * a * c)

#Finding the roots of equation using formula: (-b-(d)^1/2)/4a and (-b+(d)^1/2)/4a
root1 = (-b-cmath.sqrt(d))/(2*a)
root2 = (-b+cmath.sqrt(d))/(2*a)
# Finding the roots of equation using formula: (-b-(d)^1/2)/4a and (-b+(d)^1/2)/4a
root1 = (-b - cmath.sqrt(d)) / (2 * a)
root2 = (-b + cmath.sqrt(d)) / (2 * a)

print('The roots of quadratic equations are: ',root1, root2)
print("The roots of quadratic equations are: ", root1, root2)
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# This python script calculates the sum of the digits of a random number n


def find_sum_of_digits_of_a_number(n: int) -> int:
sum_of_digits = 0
for digit in str(n):
sum_of_digits += int(digit)
return sum_of_digits


print(find_sum_of_digits_of_a_number(123))

# Shorthand for the above function
# def sumOfDigits(n: int) -> int:
# return sum([int(x) for x in str(n)])
#
#
# print(sumOfDigits(123))
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
def main():
seq = [10, 4, 3, 50, 23, 90, 1, 100, 49]
print(largest_three_elements(seq))
seq = [10, 4, 3, 50, 23, 90, 1, 100, 49]
print(largest_three_elements(seq))


def largest_three_elements(S):
""" Return the three largest elements of sequence S """
max1 = S[0]
max2 = S[0]
max3 = S[0]

for val in S:
if val > max1:
max3 = max2
max2 = max1
max1 = val
elif val > max2:
max3 = max2
max2 = val
elif val > max3:
max3 = val
return (max1, max2, max3)
"""Return the three largest elements of sequence S"""
max1 = S[0]
max2 = S[0]
max3 = S[0]

for val in S:
if val > max1:
max3 = max2
max2 = max1
max1 = val
elif val > max2:
max3 = max2
max2 = val
elif val > max3:
max3 = val
return (max1, max2, max3)


if __name__ == '__main__':
main()
if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
def main():
"""
Return the two largest elements from a nonempty Python list (array)
1. Sort the array
2. Return the last two elements from the sorted array
Asymptotic analysis
By sorting the sequence of the elements, elements will be placed next to each other. Therefore
1. built in function sorted guarantees a worst case running time of O(n log n)
2. return tuple assignment is in constant time O(1)
"""
seq = [12, 13, 1, 10, 34, 35] # Test with various sequences

largest, second_largest = find_two_largest_elements(seq) #unpacking tuple
print(f'Largest: {largest} || Second largest: {second_largest}')
"""
Return the two largest elements from a nonempty Python list (array)
1. Sort the array
2. Return the last two elements from the sorted array
Asymptotic analysis
By sorting the sequence of the elements, elements will be placed next to each other. Therefore
1. built in function sorted guarantees a worst case running time of O(n log n)
2. return tuple assignment is in constant time O(1)
"""
seq = [12, 13, 1, 10, 34, 35] # Test with various sequences

largest, second_largest = find_two_largest_elements(seq) # unpacking tuple
print(f"Largest: {largest} || Second largest: {second_largest}")


def find_two_largest_elements(S):
S_sorted = sorted(S)

return (S_sorted[-2], S_sorted[-1]) # Return in tuple form
S_sorted = sorted(S)

return (S_sorted[-2], S_sorted[-1]) # Return in tuple form


if __name__ == '__main__':
main()
if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
def main():
seq = [12, 13, 1, 10, 34, 1]
print(smallest_three_elements(seq))
seq = [12, 13, 1, 10, 34, 1]
print(smallest_three_elements(seq))


def smallest_three_elements(S):
""" Return the three smallest elements of sequence S """
min1 = S[0]
min2 = S[0]
min3 = S[0]

for val in S:
if val < min1:
min3 = min2
min2 = min1
min1 = val
elif val < min2:
min3 = min2
min2 = val
elif val < min3:
min3 = val
return (min1, min2, min3)
"""Return the three smallest elements of sequence S"""
min1 = S[0]
min2 = S[0]
min3 = S[0]

for val in S:
if val < min1:
min3 = min2
min2 = min1
min1 = val
elif val < min2:
min3 = min2
min2 = val
elif val < min3:
min3 = val
return (min1, min2, min3)


if __name__ == '__main__':
main()
if __name__ == "__main__":
main()
Loading

0 comments on commit 4989e48

Please sign in to comment.