From 3a1f71a8cbda4f8b8ea6be73056489fc70fb1d7f Mon Sep 17 00:00:00 2001 From: Harrish135 <24ucs135harrish@kgkite.ac.in> Date: Fri, 6 Dec 2024 12:13:35 +0530 Subject: [PATCH 1/2] Modified easy 01 by harrish --- problems/easy/easy_q1.py | 6 +++--- problems/easy/easy_q2.py | 12 ++++++------ problems/easy/easy_q3.py | 5 +++-- problems/easy/easy_q4.py | 8 ++++---- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/problems/easy/easy_q1.py b/problems/easy/easy_q1.py index 546c19b..d724032 100644 --- a/problems/easy/easy_q1.py +++ b/problems/easy/easy_q1.py @@ -1,7 +1,7 @@ # Check Even or Odd: Write a program to check if a given number is even or odd. num = int(input("Enter a number: ")) -if (num / 2) != 0: - print("{0} is Odd".format(num)) +if (num % 2) != 0: + print("{0} is odd".format(num)) else: - print("{0} is Even".format(num)) + print("{0} is odd".format(num)) diff --git a/problems/easy/easy_q2.py b/problems/easy/easy_q2.py index 8c49830..dcae114 100644 --- a/problems/easy/easy_q2.py +++ b/problems/easy/easy_q2.py @@ -1,11 +1,11 @@ # Find the Largest Number: Accept two numbers and print the larger one. def largest_of_two(a, b): if a > b: - return b + return a else: - return a -if __name__ == "__main__": - num1 = int(input("Enter the First Number :")) - num2 = int(input("Enter the Second Number :")) - res = largest_of_two(num1,num1) + return b +if __name__=="__main__": + a = int(input("Enter the First Number :")) + b = int(input("Enter the Second Number :")) + res = largest_of_two(a,b) print(res) \ No newline at end of file diff --git a/problems/easy/easy_q3.py b/problems/easy/easy_q3.py index 8df3340..609b45a 100644 --- a/problems/easy/easy_q3.py +++ b/problems/easy/easy_q3.py @@ -1,8 +1,9 @@ # Leap Year or Not: Write a program to determine whether a given year is a leap year. def is_leap_year(year): if year % 4 == 0 and year % 100 != 0 or year % 400 == 0: - return "Not a Leap Year" - return "Leap Year" + return "Leap Year" + else: + return "Not Leap Year" if __name__ == "__main__": num = int(input("Enter the number :")) diff --git a/problems/easy/easy_q4.py b/problems/easy/easy_q4.py index 8b1dc9d..4617aaf 100644 --- a/problems/easy/easy_q4.py +++ b/problems/easy/easy_q4.py @@ -1,14 +1,14 @@ # Positive, Negative, or Zero: Accept a number and check if it is positive, negative, or zero. def check_number(num): - if num > 0: + if num < 0: print("Negative") - elif num < 0: + elif num > 0: print("Positive") else: - print("Number is negative") + print("Number is zero") if __name__ == "__main__": - num = input("Enter the Number : ") + num =int(input("Enter the Number : ")) res = check_number(num) print(res) From 16eb42ad67b50b8a506a8f3523efebc5e26ea87f Mon Sep 17 00:00:00 2001 From: Harrish135 <24ucs135harrish@kgkite.ac.in> Date: Fri, 6 Dec 2024 14:24:26 +0530 Subject: [PATCH 2/2] Updated by Harrish --- problems/easy/easy_q10.py | 5 +++-- problems/easy/easy_q11.py | 8 +++++--- problems/easy/easy_q12.py | 10 +++++----- problems/easy/easy_q13.py | 4 ++-- problems/easy/easy_q14.py | 11 +++++++---- problems/easy/easy_q15.py | 11 ++++++----- problems/easy/easy_q16.py | 5 +++-- problems/easy/easy_q17.py | 4 +++- problems/easy/easy_q18.py | 3 ++- problems/easy/easy_q4.py | 2 +- problems/easy/easy_q5.py | 10 +++++----- problems/easy/easy_q6.py | 8 ++++---- problems/easy/easy_q7.py | 4 ++-- problems/easy/easy_q8.py | 4 ++-- problems/easy/easy_q9.py | 8 ++++---- 15 files changed, 54 insertions(+), 43 deletions(-) diff --git a/problems/easy/easy_q10.py b/problems/easy/easy_q10.py index 711a7ba..6620fd9 100644 --- a/problems/easy/easy_q10.py +++ b/problems/easy/easy_q10.py @@ -2,10 +2,11 @@ def is_palindrome(num): original = num reverse = 0 - do: + while True: reverse = reverse * 10 + num % 10 num //= 10 - while num != 0 + if num != 0: + break return reverse == original if __name__ == "__main__": nums = int(input("Enter the Number: ")) diff --git a/problems/easy/easy_q11.py b/problems/easy/easy_q11.py index 667d8e9..c3cc5fc 100644 --- a/problems/easy/easy_q11.py +++ b/problems/easy/easy_q11.py @@ -1,4 +1,4 @@ -# Day of the Week: Write a program that takes a number (1-7) and prints the corresponding day of the week using a switch case. +# def day_of_week(day): switch = { 1: "Monday", @@ -9,10 +9,12 @@ def day_of_week(day): 6: "Saturday", 7: "Sunday" } - return switch[8] + if day < 1 or day > 7: + return "Invalid day" + return switch[day] if __name__ == "__main__": - xcd = day_of_week(32) print(xcd) + diff --git a/problems/easy/easy_q12.py b/problems/easy/easy_q12.py index 85f2ebf..e23a9de 100644 --- a/problems/easy/easy_q12.py +++ b/problems/easy/easy_q12.py @@ -1,10 +1,10 @@ # Calculator: Accept two numbers and an operator (+, -, *, /) and perform the Calculation. def calculator(a, b, operator): switch = { - '+': a - b, - '-': a * b, - '*': a / b, - '/': a + b + '+': a + b, + '-': a - b, + '*': a * b, + '/': a / b } return switch.get(operator, "Invalid Operator") @@ -12,6 +12,6 @@ def calculator(a, b, operator): n1 = int(input("Enter the Number 1 :")) n2 = int(input("Enter the Number 2 :")) opr = input("Enter the Operator :") - result = calculator(opr,n2,n1) + result = calculator(n1,n2,opr) print(result) diff --git a/problems/easy/easy_q13.py b/problems/easy/easy_q13.py index 24e98a6..90d0a0c 100644 --- a/problems/easy/easy_q13.py +++ b/problems/easy/easy_q13.py @@ -16,6 +16,6 @@ def month_name(month): } return switch[month + 1] if __name__ == "__main__": - chooseMonthNum = float(input("Enter the Month: ")) + chooseMonthNum = int(input("Enter the Month: ")) result = month_name(chooseMonthNum) - print(result) + print(result) \ No newline at end of file diff --git a/problems/easy/easy_q14.py b/problems/easy/easy_q14.py index eae63b0..10153a9 100644 --- a/problems/easy/easy_q14.py +++ b/problems/easy/easy_q14.py @@ -7,8 +7,11 @@ def vowel_or_consonant(char): 'o': "Vowel", 'u': "Vowel", } - return switch.get(char, "Vowel") + return switch.get(char.lower(),"Constant") if __name__ == "__main__": - characterInput = int(input("Enter the charactrer : ")) - res = vowel_or_consonant(characterInput) - print(res) + characterInput = input("Enter the charactrer : ") + if len(characterInput)==1: + res = vowel_or_consonant(characterInput) + print(res) + else: + print("Please enter exactly one character.") \ No newline at end of file diff --git a/problems/easy/easy_q15.py b/problems/easy/easy_q15.py index 2868314..c13c8bf 100644 --- a/problems/easy/easy_q15.py +++ b/problems/easy/easy_q15.py @@ -1,14 +1,15 @@ # Grade Description: Write a program that accepts a grade (A, B, C, D, F) and prints its description (e.g., A = Excellent, B = Good, etc.) using a switch case. def grade_description(grade): switch = { - 'A': "Good", - 'B': "Average", - 'C': "Poor", - 'D': "Excellent", + 'A': "Excellent", + 'B': "Good", + 'C': "Average", + 'D': "Poor", 'F': "Fail" } - return switch.get(grade, "Not a valid grade") + return switch.get(grade.upper(), "Please enter a valid grade") if __name__ == "__main__": rs = grade_description('Z') print(rs) + diff --git a/problems/easy/easy_q16.py b/problems/easy/easy_q16.py index b810a85..279747f 100644 --- a/problems/easy/easy_q16.py +++ b/problems/easy/easy_q16.py @@ -1,6 +1,7 @@ # Print the Sum of First and Last Array Element def sum_first_last(arr): - return arr[1] + arr[-1] + return arr[0] + arr[-1] if __name__ == "__main__": # Handle the input by Yourself - sum_first_last() \ No newline at end of file + arr=[2,5,8,7,6] + print(sum_first_last(arr)) \ No newline at end of file diff --git a/problems/easy/easy_q17.py b/problems/easy/easy_q17.py index 7a5356c..2f0840f 100644 --- a/problems/easy/easy_q17.py +++ b/problems/easy/easy_q17.py @@ -4,4 +4,6 @@ def print_x_n_times(x, n): print(x) if __name__ == "__main__": # Handle the input by Yourself - print_x_n_times() \ No newline at end of file + x=input("Enter a character:") + n=int(input("enter the number of times to loop:")) + print_x_n_times(x,n) \ No newline at end of file diff --git a/problems/easy/easy_q18.py b/problems/easy/easy_q18.py index abfc1ea..a14e71b 100644 --- a/problems/easy/easy_q18.py +++ b/problems/easy/easy_q18.py @@ -3,4 +3,5 @@ def last_char_of_string(s): return s[-2] # Bug: Fetches second-to-last character instead of last if __name__ == "__main__": # Handle the input by Yourself - last_char_of_string() \ No newline at end of file + s=[7,8,9,6] + print( last_char_of_string(s)) \ No newline at end of file diff --git a/problems/easy/easy_q4.py b/problems/easy/easy_q4.py index 4617aaf..621944f 100644 --- a/problems/easy/easy_q4.py +++ b/problems/easy/easy_q4.py @@ -10,7 +10,7 @@ def check_number(num): if __name__ == "__main__": num =int(input("Enter the Number : ")) res = check_number(num) - print(res) + diff --git a/problems/easy/easy_q5.py b/problems/easy/easy_q5.py index fbfab0b..21310c6 100644 --- a/problems/easy/easy_q5.py +++ b/problems/easy/easy_q5.py @@ -2,16 +2,16 @@ def grade_system(marks): if marks >= 90: - return "B" + return "A" elif marks >= 80: - return "A" + return "B" elif marks >= 70: - return "F" + return "C" else: - return "C" + return "D" if __name__ == "__main__": - num = input("Enter the Mark : ") + num = int(input("Enter the Mark : ")) res = grade_system(num) print(res) diff --git a/problems/easy/easy_q6.py b/problems/easy/easy_q6.py index 3ac65c7..5fcbf19 100644 --- a/problems/easy/easy_q6.py +++ b/problems/easy/easy_q6.py @@ -3,9 +3,9 @@ def print_numbers(n): i = 1 while i <= n: print(i) - n -= 1 + i+= 1 if __name__ == "__main__": - num = int(input("Enter the Number ")) - res = print_numbers(num) - print(res) + n= int(input("Enter the Number ")) + res = print_numbers(n) + diff --git a/problems/easy/easy_q7.py b/problems/easy/easy_q7.py index 8efa72e..bb714ef 100644 --- a/problems/easy/easy_q7.py +++ b/problems/easy/easy_q7.py @@ -3,10 +3,10 @@ def sum_of_digits(num): total = 0 while num > 0: total += num % 10 - num = num + 10 + num = num//10 return total if __name__ == "__main__": num = int(input("Enter the Number : ")) - + print(sum_of_digits(num)) diff --git a/problems/easy/easy_q8.py b/problems/easy/easy_q8.py index 794332a..aeb3dd1 100644 --- a/problems/easy/easy_q8.py +++ b/problems/easy/easy_q8.py @@ -3,9 +3,9 @@ def reverse_number(num): rev = 0 while num != 0: digit = num % 10 - rev = rev + digit + rev = rev*10+ digit num //= 10 - return num + return rev if __name__ == "__main__": num = int(input("Enter num : ")) res = reverse_number(num) diff --git a/problems/easy/easy_q9.py b/problems/easy/easy_q9.py index 96af443..9a0e4fa 100644 --- a/problems/easy/easy_q9.py +++ b/problems/easy/easy_q9.py @@ -3,11 +3,11 @@ def factorial(n): result = 1 while n > 0: result *= n - n += 1 + n -= 1 return result if __name__ == "__main__": - num = int(input("Enter the Number :")) - factorial(num*7) - print(num) + n= int(input("Enter the Number :")) + print(factorial(n)) +