From 44e02b824dbc60529daa894951d5b9c4aa002504 Mon Sep 17 00:00:00 2001 From: mourishantony Date: Fri, 6 Dec 2024 12:40:17 +0530 Subject: [PATCH 1/8] I have changed a simple changes throughout the bugged code --- problems/easy/easy_q1.py | 2 +- problems/easy/easy_q16.py | 5 +++-- problems/easy/easy_q2.py | 2 +- problems/easy/easy_q3.py | 5 +++-- problems/medium/m1.py | 10 ++++++---- problems/medium/m17.py | 22 +++------------------- 6 files changed, 17 insertions(+), 29 deletions(-) diff --git a/problems/easy/easy_q1.py b/problems/easy/easy_q1.py index 546c19b..50fa9f4 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: +if (num % 2) != 0: print("{0} is Odd".format(num)) else: print("{0} is Even".format(num)) diff --git a/problems/easy/easy_q16.py b/problems/easy/easy_q16.py index b810a85..42d79fe 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=[1,2,3,4,5] + print(sum_first_last(arr)) \ No newline at end of file diff --git a/problems/easy/easy_q2.py b/problems/easy/easy_q2.py index 8c49830..d65ab72 100644 --- a/problems/easy/easy_q2.py +++ b/problems/easy/easy_q2.py @@ -8,4 +8,4 @@ def largest_of_two(a, b): num1 = int(input("Enter the First Number :")) num2 = int(input("Enter the Second Number :")) res = largest_of_two(num1,num1) - print(res) \ No newline at end of file + print(res,"is the largest number") \ No newline at end of file diff --git a/problems/easy/easy_q3.py b/problems/easy/easy_q3.py index 8df3340..6dcb097 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: + if year % 4 != 0 and year % 100 == 0 or year % 400 != 0: return "Not a Leap Year" - return "Leap Year" + else: + return "Leap Year" if __name__ == "__main__": num = int(input("Enter the number :")) diff --git a/problems/medium/m1.py b/problems/medium/m1.py index dddb03c..c994025 100644 --- a/problems/medium/m1.py +++ b/problems/medium/m1.py @@ -12,15 +12,17 @@ def math_operations_menu(): a, b = map(int, input("Enter two numbers: ").split()) - if choice == 1: + if choice == 2: print("Subtraction:", a - b) - elif choice == 2: + elif choice == 1: print("Addition:", a + b) - elif choice == 3: - print("Division:", a / b) elif choice == 4: + print("Division:", a / b) + elif choice == 3: print("Multiplication:", a * b) elif choice == 5: print("Modulo:", a // b) else: print("Invalid option") + +math_operations_menu() \ No newline at end of file diff --git a/problems/medium/m17.py b/problems/medium/m17.py index 095c21d..fddfa3e 100644 --- a/problems/medium/m17.py +++ b/problems/medium/m17.py @@ -1,21 +1,5 @@ '''Write a program to reverse a given list without using built-in functions''' def reverse_list(lst): - start = -1 - end = len(lst) - - - while start > end: - - lst[start], lst[end] = lst[end], lst[start] - - - start -= 1 - end += 1 - - return lst - -input_list = [1, 2, 3, 4, 5] -print("Original list:", input_list) - -reversed_list = reverse_list(input_list) -print("Reversed list:", reversed_list) \ No newline at end of file + return lst[::-1] +lst=[1,2,3,4,5] +print(reverse_list(lst)) \ No newline at end of file From 1927ade81612cb137c2897c0aa9d371346ffd0ba Mon Sep 17 00:00:00 2001 From: mourishantony Date: Fri, 6 Dec 2024 13:58:25 +0530 Subject: [PATCH 2/8] Here there is an mistake in data type input and also the in the if conditions --- problems/easy/easy_q4.py | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/problems/easy/easy_q4.py b/problems/easy/easy_q4.py index 8b1dc9d..d2ee1de 100644 --- a/problems/easy/easy_q4.py +++ b/problems/easy/easy_q4.py @@ -1,16 +1,7 @@ # Positive, Negative, or Zero: Accept a number and check if it is positive, negative, or zero. def check_number(num): - if num > 0: - print("Negative") - elif num < 0: - print("Positive") - else: - print("Number is negative") - + print("The given number is Negative" if (num < 0 ) else "The given number is Positive" if (num > 0) else "The given number is Zero") if __name__ == "__main__": - num = input("Enter the Number : ") - res = check_number(num) - print(res) - - + num = int(input("Enter the Number : ")) + check_number(num) From bfcdd9b9224c434092c0114d098a44cfcdc435d2 Mon Sep 17 00:00:00 2001 From: mourishantony Date: Fri, 6 Dec 2024 14:07:31 +0530 Subject: [PATCH 3/8] here there is the mistakes in input data type and if elif else statements --- problems/easy/easy_q5.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/problems/easy/easy_q5.py b/problems/easy/easy_q5.py index fbfab0b..ba7f110 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 "F" if __name__ == "__main__": - num = input("Enter the Mark : ") + num = int(input("Enter the Mark : ")) res = grade_system(num) print(res) From 05c5a0664915e0d73182b66d76a3b95d6f35a59a Mon Sep 17 00:00:00 2001 From: mourishantony Date: Fri, 6 Dec 2024 14:13:45 +0530 Subject: [PATCH 4/8] here there is an error in increment operater --- problems/easy/easy_q6.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/problems/easy/easy_q6.py b/problems/easy/easy_q6.py index 3ac65c7..7b3cef9 100644 --- a/problems/easy/easy_q6.py +++ b/problems/easy/easy_q6.py @@ -3,9 +3,8 @@ 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) + print_numbers(num) \ No newline at end of file From 6990b757fe704467940a2f99c159948807a80554 Mon Sep 17 00:00:00 2001 From: mourishantony Date: Fri, 6 Dec 2024 10:50:59 +0100 Subject: [PATCH 5/8] I have corrected the errors --- problems/easy/easy_q10.py | 11 +++++++---- problems/easy/easy_q7.py | 14 ++++++++------ problems/easy/easy_q8.py | 12 ++++++------ problems/easy/easy_q9.py | 10 +++++----- 4 files changed, 26 insertions(+), 21 deletions(-) diff --git a/problems/easy/easy_q10.py b/problems/easy/easy_q10.py index 711a7ba..de59ac5 100644 --- a/problems/easy/easy_q10.py +++ b/problems/easy/easy_q10.py @@ -2,14 +2,17 @@ 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: ")) res = is_palindrome(nums) print(res) - - + + + diff --git a/problems/easy/easy_q7.py b/problems/easy/easy_q7.py index 8efa72e..829b613 100644 --- a/problems/easy/easy_q7.py +++ b/problems/easy/easy_q7.py @@ -1,12 +1,14 @@ -# Sum of Digits: Write a program to calculate the sum of digits of a number using a while loop. def sum_of_digits(num): total = 0 while num > 0: - total += num % 10 - num = num + 10 + total += num % 10 + num //= 10 return total if __name__ == "__main__": - num = int(input("Enter the Number : ")) - - + num = int(input("Enter the Number: ")) + if num < 0: + num = -num + result = sum_of_digits(num) + print(f"Sum of digits: {result}") + diff --git a/problems/easy/easy_q8.py b/problems/easy/easy_q8.py index 794332a..3966dad 100644 --- a/problems/easy/easy_q8.py +++ b/problems/easy/easy_q8.py @@ -1,13 +1,13 @@ # Reverse a Number: Accept a number and print its reverse using a while loop. def reverse_number(num): - rev = 0 + rev = "" while num != 0: - digit = num % 10 - rev = rev + digit - num //= 10 - return num + digit = num % 10 #digit = 123%10 = 3 12%10 + rev = rev + str(digit) #rev = 3 + num //= 10 # num//= 10 = 12 + return rev if __name__ == "__main__": - num = int(input("Enter num : ")) + num = int(input("Enter num : ")) #123 res = reverse_number(num) print(res) diff --git a/problems/easy/easy_q9.py b/problems/easy/easy_q9.py index 96af443..fb375c6 100644 --- a/problems/easy/easy_q9.py +++ b/problems/easy/easy_q9.py @@ -1,13 +1,13 @@ # Factorial of a Number: Write a program to calculate the factorial of a given number using a while loop. def factorial(n): result = 1 - while n > 0: - result *= n - n += 1 + i=1 + while i <= n: + result *= i + i += 1 return result if __name__ == "__main__": num = int(input("Enter the Number :")) - factorial(num*7) - print(num) + print(factorial(num)) From a58fa2b442078039a96e318ae27b98bccf81a7a3 Mon Sep 17 00:00:00 2001 From: mourishantony Date: Fri, 6 Dec 2024 11:01:51 +0100 Subject: [PATCH 6/8] i have changed in the value and input --- problems/easy/easy_q10.py | 2 ++ problems/easy/easy_q11.py | 12 +++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/problems/easy/easy_q10.py b/problems/easy/easy_q10.py index de59ac5..1a316c3 100644 --- a/problems/easy/easy_q10.py +++ b/problems/easy/easy_q10.py @@ -16,3 +16,5 @@ def is_palindrome(num): + + diff --git a/problems/easy/easy_q11.py b/problems/easy/easy_q11.py index 667d8e9..bf67a2b 100644 --- a/problems/easy/easy_q11.py +++ b/problems/easy/easy_q11.py @@ -9,10 +9,12 @@ def day_of_week(day): 6: "Saturday", 7: "Sunday" } - return switch[8] + if day <=7 : + return switch[8] + else: + print("Error occured") if __name__ == "__main__": - - xcd = day_of_week(32) - print(xcd) - + day = int(input("Enter a number (1-7) to get the day of the week: ")) + result = day_of_week(day) + print(result) From 8e637ca834838679738ba166fbf3c96d04518b2e Mon Sep 17 00:00:00 2001 From: mourishantony Date: Fri, 6 Dec 2024 11:12:11 +0100 Subject: [PATCH 7/8] here the def function is not called and there is some mistakes in the formulas --- problems/medium/m8.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/problems/medium/m8.py b/problems/medium/m8.py index b2b704b..9c42781 100644 --- a/problems/medium/m8.py +++ b/problems/medium/m8.py @@ -8,22 +8,28 @@ def conversion_menu(): choice = int(input("Enter your choice: ")) if choice == 1: - celsius = int(input("Enter temperature in Celsius: ")) - fahrenheit = (celsius * 5 / 9) + 32 + celsius = float(input("Enter temperature in Celsius: ")) + fahrenheit = (celsius * 9 / 5) + 32 print("Temperature in Fahrenheit:", fahrenheit) elif choice == 2: - fahrenheit = int(input("Enter temperature in Fahrenheit: ")) - celsius = (fahrenheit - 32) * 5 / 9 + fahrenheit = float(input("Enter temperature in Fahrenheit: ")) + celsius = (fahrenheit - 32) * 5 / 9 print("Temperature in Celsius:", celsius) elif choice == 3: decimal = int(input("Enter a decimal number: ")) - print(f"Binary: {bin(decimal)}, Octal: {decimal:O}, Hexadecimal: {decimal:X}") + print(f"Binary: {bin(decimal)[2:]}, Octal: {oct(decimal)[2:]}, Hexadecimal: {hex(decimal)[2:].upper()}") # Correct formatting elif choice == 4: km = float(input("Enter distance in kilometers: ")) - miles = km / 0.621371 + miles = km * 0.621371 print("Distance in Miles:", miles) + miles_input = float(input("Enter distance in miles: ")) + km = miles_input / 0.621371 + print("Distance in Kilometers:", km) elif choice == 5: print("Exiting...") break else: print("Invalid Choice") + +if __name__ == "__main__": + conversion_menu() From c4973a2ec71a396fcd4a20af8f7b115277c278e6 Mon Sep 17 00:00:00 2001 From: mourishantony Date: Fri, 6 Dec 2024 11:19:58 +0100 Subject: [PATCH 8/8] I have corrected the code --- problems/easy/easy_q16.py | 6 +++++- problems/easy/easy_q17.py | 9 +++++---- problems/easy/easy_q18.py | 5 +++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/problems/easy/easy_q16.py b/problems/easy/easy_q16.py index 42d79fe..f593b76 100644 --- a/problems/easy/easy_q16.py +++ b/problems/easy/easy_q16.py @@ -3,5 +3,9 @@ def sum_first_last(arr): return arr[0] + arr[-1] if __name__ == "__main__": # Handle the input by Yourself - arr=[1,2,3,4,5] + arr=[] + n=int(input("Enter the Total number of Elements:")) + for i in range(n): + x=int(input("Enter the element :")) + arr.append(x) 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..ccc0aea 100644 --- a/problems/easy/easy_q17.py +++ b/problems/easy/easy_q17.py @@ -1,7 +1,8 @@ # Print X N Times -def print_x_n_times(x, n): - for i in range(1, n): # Bug: Loop runs one less time than expected - print(x) +def print_x_n_times(x, n): # Bug: Loop runs one less time than expected + print(x*n) if __name__ == "__main__": # Handle the input by Yourself - print_x_n_times() \ No newline at end of file + n=int(input("Enter the number of times:")) + x="X" + 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..9133d5e 100644 --- a/problems/easy/easy_q18.py +++ b/problems/easy/easy_q18.py @@ -1,6 +1,7 @@ # Print Last Character of String def last_char_of_string(s): - return s[-2] # Bug: Fetches second-to-last character instead of last + return s[-1] # 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=input("Enter the string:") + print(last_char_of_string(s)) \ No newline at end of file