This repository has been archived by the owner on Jul 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e8052d7
commit 4989e48
Showing
26 changed files
with
248 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 6 additions & 4 deletions
10
...f-n-objects-taken-r-at-a-time/calculate_the_permutation_of_n_objects_taken_r_at_a_time.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 18 additions & 16 deletions
34
...-julian-date-and-vice-versa/convert_standard_date_format_to_julian_date_and_vice_versa.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
program/program/find-average-of-numbers/find_average_of_numbers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
7 changes: 4 additions & 3 deletions
7
program/program/find-factorial-of-a-number/find_factorial_of_a_number.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 10 additions & 10 deletions
20
program/program/find-roots-of-quadratic-equation/find_roots_of_quadratic_equation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
4 changes: 3 additions & 1 deletion
4
program/program/find-sum-of-digits-of-a-number/find_sum_of_digits_of_a_number.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
42 changes: 22 additions & 20 deletions
42
...ind-the-largest-three-elements-in-an-array/find_the_largest_three_elements_in_an_array.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
42 changes: 22 additions & 20 deletions
42
...am/find-the-largest-two-elements-in-an-array/find_the_largest_two_elements_in_an_array.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
42 changes: 22 additions & 20 deletions
42
...d-the-smallest-three-elements-in-an-array/find_the_smallest_three_elements_in_an_array.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Oops, something went wrong.