Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python solutions for PS-Sprint-1 problems #16

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions PS-Sprint-1/10_gcd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def gcd(a,b):
while b != 0:
a, b = b, a%b
return a

try:
num1 = int(input("Enter the first non-negative integer : "))
num2 = int(input("Enter the second non-negative integer :"))
result = gcd(num1, num2)

if num1 < 0 or num2 < 0:
print("Please, enter non-negative integers only....")
else:
print(f"The Greatest Common Divisor of {num1} and {num2} is {result}")
except ValueError:
print("Invalid input. Please, enter valid non-negative integers....")
21 changes: 21 additions & 0 deletions PS-Sprint-1/11_lcm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def gcd(a,b):
while b != 0:
a, b = b, a%b
return a

def lcm(a,b):
if a == 0 or b == 0:
return 0
return (a*b) / gcd(a,b)

try:
num1 = int(input("Enter the first non-negative integer number : "))
num2 = int(input("Enter the second non-negative integer number : "))
result = lcm(num1,num2)

if num1 < 0 or num2 < 0:
print("Please, enter non-negative numbers....")
else:
print(f"The Least Common Multiple of {num1} and {num2} is {result}")
except ValueError:
print("Invalid input. Please, enter valid non-negative integers....")
20 changes: 20 additions & 0 deletions PS-Sprint-1/12_count_vowel_consonant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def count_v_c(string):
vowels = "aeiouAEIOU"
v_count = 0
c_count = 0

for char in string:
if char.isalpha():
if char in vowels:
v_count += 1
else:
c_count += 1
return v_count, c_count

try:
string = input("Enter a string : ")
vowel, consonant = count_v_c(string)
print(f"Numbers of vowel : {vowel}")
print(f"Numbers of consonant : {consonant}")
except Exception as e:
print(f"An error occurred. {e}")
9 changes: 9 additions & 0 deletions PS-Sprint-1/13_reverse_string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def reverse_str(string):
return string[::-1]

try:
string = input("Enter a string to reverse : ")
result = reverse_str(string)
print(f"Reversed string : {result}")
except Exception as e:
print(f"An error occurred: {e}")
18 changes: 18 additions & 0 deletions PS-Sprint-1/14_largest_smallest_num_in_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
array = []

while True:
num = input("Enter a number OR write 'done' to finish : ")
if num.lower() == 'done':
break
else:
try:
array.append(int(num))
except ValueError:
print("Invalid input. Please enter a number or 'done' to finish....")
if array:
largest = max(array)
smallest =min(array)
print(f"The maximum number is {largest}")
print(f"The minimum number is {smallest}")
else:
print("No numbers were entered....")
16 changes: 16 additions & 0 deletions PS-Sprint-1/15_sorting_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
array = []

while True:
num = input("Enter a number OR write 'done' to finish : ")
if num.lower() == 'done':
break
else:
try:
array.append(int(num))
except ValueError:
print("Invalid input. Please enter a number or 'done' to finish....")
if array:
array.sort()
print(f"The array sorted in ascending order is {array}.")
else:
print("No numbers were entered....")
11 changes: 11 additions & 0 deletions PS-Sprint-1/1_check_even_odd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def check_even_odd(num):
if num%2 == 0:
return "This is an Even Number...."
else:
return "This is an Odd Number...."
try:
num = int(input("Enter a number to determine Even or Odd : "))
result = check_even_odd(num)
print(result)
except ValueError:
print("Invalid Input. Please enter a valid integer....")
15 changes: 15 additions & 0 deletions PS-Sprint-1/2_check_prime_num.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def check_prime_num(num):
if num <= 1:
return False
for i in range(2, int(num**0.5)+1):
if num%i == 0:
return False
return True
try:
num = int(input("Enter a integer number to check if it is a prime number : "))
if check_prime_num(num):
print(f"{num} is a prime number....")
else:
print(f"{num} is not a prime number....")
except ValueError:
print("Invalid input. Please, enter a valid integer number.....")
11 changes: 11 additions & 0 deletions PS-Sprint-1/3_check_leap_year.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def check_leap_year(year):
if (year%4 == 0 and year%100 != 0) or year%400 == 0:
return f"{year} is a leap year...."
else:
return f"{year} is a common year...."
try:
year = int(input("Enter a year to check if it is a leap year or a common year : "))
result = check_leap_year(year)
print(result)
except ValueError:
print("Invalid input. Please enter a valid year....")
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def check_armstrong_num(num):
digits = str(num)
num_digits = len(digits)
total = sum(int(digit)**num_digits for digit in digits)
if total == num:
return f"{num} is an Armstrong Number...."
else:
return f"{num} is not an Armstrong Number...."

try:
num = int(input("Enter a number to check if it is a Armstrong Number : "))
result = check_armstrong_num(num)
print(result)
except ValueError:
print("Invalid input. Please enter a valid integer....")
17 changes: 17 additions & 0 deletions PS-Sprint-1/5_fibonacci_series.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def fibonacci_series(num):
series = []
a,b = 0,1
while a <= num:
series.append(a)
a,b = b,a+b
return series

try:
num = int(input("Enter a number to generate the Fibonacci Sequence up to that number : "))
result = fibonacci_series(num)
if num<0:
print("Please enter a non-negative number....")
else:
print("The Fibonacci Series is : ", result)
except ValueError:
print("Invalid input. Please, enter a valid integer number....")
13 changes: 13 additions & 0 deletions PS-Sprint-1/6_check_palindrome.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def check_palindrome(value):
value_str = str(value)
if value_str == value[::-1]:
return f"{value} is a palindrome...."
else:
return f"{value} is not a palindrome...."

try:
value = input("Enter a string or number to check if it is a Palindrome : ")
result = check_palindrome(value)
print(result)
except Exception as e:
print("An error occurred: ", e)
30 changes: 30 additions & 0 deletions PS-Sprint-1/7_different_star_patterns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
def pyramid(num):
print("Pyramid Pattern: ")
for i in range(num):
print(" " * (num - (i + 1)), end="")
print("* " * (i + 1))

def diamond(num):
print("Diamond Pattern: ")
for i in range (num):
print(" " * (num - (i + 1)), end="")
print("* " * (i + 1))
for i in range(num):
print(" " * (i + 1), end="")
print("* " * (num - (i + 1)))

print("Choose a pattern to print : ")
print("1. Pyramid")
print("2. Diamond")

try:
choice = int(input("Enter your choice(1 or 2) : "))
num = int (input("Enter the number of row or height : "))
if choice == 1:
pyramid(num)
elif choice == 2:
diamond(num)
else:
print("Invalid choice. Please, enter 1 or 2....")
except Exception as e:
print("An error occurred: ", e)
18 changes: 18 additions & 0 deletions PS-Sprint-1/8_factorial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def factorial(num):
if num == 0 or num == 1:
return 1
else:
result = 1
for i in range(2, num + 1):
result *= i
return result

try:
num = int(input("Enter a non-negative integer number to compute its factorial : "))
result = factorial(num)
if num > 0:
print(f"The Factorial of {num} is {result}")
else:
print("Factorial is not defined for negative numbers....")
except ValueError:
print("Invalid input. Please, enter a non-negative integer....")
17 changes: 17 additions & 0 deletions PS-Sprint-1/9_summing_digits_of_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def sum_of_digits(num):
total = 0
while num > 0:
digit = num % 10
total += digit
num //= 10
return total

try:
num = int(input("Enter a non-negative number to calculate the sum of digits of it : "))
result = sum_of_digits(num)
if num >= 0:
print(f"The sum of digits of {num} is {result}.")
else:
print("Please, enter a non-negative number.")
except ValueError:
print("Invalid input. Please, enter a valid non-negative integer....")