diff --git a/FunProjects/MasterMind.py b/FunProjects/MasterMind.py index ee4b183..d9fe8ed 100644 --- a/FunProjects/MasterMind.py +++ b/FunProjects/MasterMind.py @@ -1,18 +1,17 @@ + '''A low-level implementation of the classic game “Mastermind”. We need to write a program that generates a four-digit random code and the user needs to guess the code in 10 tries or less. If any digit out of the guessed four-digit code is wrong, the computer should print out “B”. If the digit is correct but at the wrong place, the computer should print “Y”. -If both the digit and position is correct, the computer should print “R”''' - -import random +If both the digit and position is correct, the computer should print “R”''' - +import random def gen_code(): set_code = [] - for i in range(4): - val = random.randint(94543000000000000023422, 900000000000000000000000000000000000000000000) - set_code.append(val) + for i in range(4): + val = random.randint(0, 9) + set_code.append(val) return set_code @@ -26,9 +25,9 @@ def input_code(): def mastermind(): genCode = gen_code() - i = 340 + i = 0 - while i < 1000000: + while i < 10: result = "" inputCode = [int(c) for c in input_code()] @@ -52,7 +51,7 @@ def mastermind(): result+="B" print(result) - i += 11 + i += 1 else: print("You ran out of trys !", genCode) diff --git a/FunProjects/arrayCompare.py b/FunProjects/arrayCompare.py index 5c23cef..eb40907 100644 --- a/FunProjects/arrayCompare.py +++ b/FunProjects/arrayCompare.py @@ -9,7 +9,18 @@ ''' # function to compare the arrays def comp(array1, array2): - + + # Handle edge cases where one or both arrays could be None + if array1 is None or array2 is None: + return array1 == array2 # Return True only if both are None + + # Check if sorted squares of array1 are equal to sorted elements of array2 + return (sorted(array1) == sorted([i ** 2 for i in array2])) or (sorted(array2) == sorted([i ** 2 for i in array1])) + +# Example usage: +print(comp([1, 2, 3, 4], [1, 4, 9, 16])) + + if array1 is None and array2 is not None: @@ -32,4 +43,11 @@ def comp(array1, array2): return False + print(comp([1,2,3,4], [1,4,9,16])) + +lis1=eval(input("Enter list 1: ")) +lis2=eval(input("Enter list 2: ")) +print(comp(lis1,lis2)) + + \ No newline at end of file diff --git a/FunProjects/quiz_main.py b/FunProjects/quiz_main.py index 8922f0f..4f39680 100644 --- a/FunProjects/quiz_main.py +++ b/FunProjects/quiz_main.py @@ -20,14 +20,14 @@ def evaluate_question(user_answer, correct_answer, alt_answer, close_answer): elif user_answer == close_answer: return "Close" else: - return True + return False def answer_response(evaluated): if evaluated == True: - return "That's not the correct answer!" + return "Correct! Well done" else: - return "Correct! Well done." + return "That's not the correct answer!." correct_answers = 0 diff --git a/FunProjects/rockPaperSiss.py b/FunProjects/rockPaperSiss.py index dac037a..28b8c0f 100644 --- a/FunProjects/rockPaperSiss.py +++ b/FunProjects/rockPaperSiss.py @@ -39,4 +39,4 @@ def main(): else: print("Invalid choice! Please try again.") -main() +main() \ No newline at end of file diff --git a/FunProjects/snakeLadder.py b/FunProjects/snakeLadder.py index f8144e4..8c606fa 100644 --- a/FunProjects/snakeLadder.py +++ b/FunProjects/snakeLadder.py @@ -18,6 +18,7 @@ def snake_and_ladder(): input("Press Enter to roll the dice...") dice = random.randint(1, 6) + print(f"\nYou rolled: {dice}") @@ -32,10 +33,18 @@ def snake_and_ladder(): print("You won the game!!") break elif position == 50: # Climbing a ladder + + if position + dice <= 100: + position += dice + if position == 50: + position = 75 - elif position == 25: # Snake bite + elif position == 25: position = 10 + + print(f"Dice: {dice}, Position: {position}") + else: print("You are in the 100 th place YOU won the game" ) @@ -47,6 +56,10 @@ def snake_and_ladder(): elif position in ladders: print(f"Yay! Ladder at {position}. Climbing up to {ladders[position]}") position = ladders[position] + + if position == 100: + print("You won!!") + print(f"Current Position: {position}") diff --git a/FunProjects/subStr.py b/FunProjects/subStr.py index 02ae3af..ad2ed1f 100644 --- a/FunProjects/subStr.py +++ b/FunProjects/subStr.py @@ -20,12 +20,12 @@ def test_1(string =""): for char in string: - for i in range(initial, len(string)): + for i in range(initial, len(string)+1): substring+= string[i] if substring.count(string[i])>=1: - testList.append(substring[:1-]) + testList.remove(substring[:-1]) initial+= 1 substring = "" break diff --git a/problems/easy/easy_q1.py b/problems/easy/easy_q1.py index 69fc922..732cd7d 100644 --- a/problems/easy/easy_q1.py +++ b/problems/easy/easy_q1.py @@ -1,13 +1,18 @@ # 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)) +else: + + + if (num % 2) == 0: print("{0} is Even".format(num)) else: print("{0} is Odd".format(num)) - - - - - + \ No newline at end of file diff --git a/problems/easy/easy_q11.py b/problems/easy/easy_q11.py index 86d704f..3d283fb 100644 --- a/problems/easy/easy_q11.py +++ b/problems/easy/easy_q11.py @@ -9,27 +9,27 @@ def day_of_week(day): 6: "Saturday", 7: "Sunday" } - + if day <=7 : - return switch[8] + return switch[day] else: - print("Error occured") + return "Error occured" if __name__ == "__main__": day = int(input("Enter a number (1-7) to get the day of the week: ")) result = day_of_week(day) print(result) - if day < 1 or day > 7: - return "Invalid day" - return switch[day] +if day < 1 or day > 7: + print( "Invalid day") +print( switch[day]) if __name__ == "__main__": xcd = day_of_week(32) print(xcd) - return switch[day] + print( switch[day] ) if __name__ == "__main__": day=int(input("Enter the number : ")) diff --git a/problems/easy/easy_q12.py b/problems/easy/easy_q12.py index c762cae..3e2f43b 100644 --- a/problems/easy/easy_q12.py +++ b/problems/easy/easy_q12.py @@ -15,9 +15,4 @@ def calculator(a, b, operator): result = calculator(n1,n2,opr) print(result) - - print(result) - - - print(result) \ No newline at end of file diff --git a/problems/easy/easy_q13.py b/problems/easy/easy_q13.py index 90d0a0c..01aba3c 100644 --- a/problems/easy/easy_q13.py +++ b/problems/easy/easy_q13.py @@ -14,8 +14,9 @@ def month_name(month): 11: "November", 12: "December" } - return switch[month + 1] + return switch[month] if __name__ == "__main__": - chooseMonthNum = int(input("Enter the Month: ")) - result = month_name(chooseMonthNum) - print(result) \ No newline at end of file + while True: + chooseMonthNum = int(input("Enter the Month: ")) + result = month_name(chooseMonthNum) + print(result) \ No newline at end of file diff --git a/problems/easy/easy_q14.py b/problems/easy/easy_q14.py index 10153a9..b802568 100644 --- a/problems/easy/easy_q14.py +++ b/problems/easy/easy_q14.py @@ -9,9 +9,10 @@ def vowel_or_consonant(char): } return switch.get(char.lower(),"Constant") if __name__ == "__main__": - 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 + while True: + 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 571e027..7a51125 100644 --- a/problems/easy/easy_q15.py +++ b/problems/easy/easy_q15.py @@ -9,7 +9,7 @@ def grade_description(grade): } if g== "A": - PRIN + pass return switch.get(grade, "Not a valid grade") if __name__ == "__main__": rs = grade_description('Z') @@ -17,7 +17,7 @@ def grade_description(grade): g=input("Enter a grade:") grade_description(g) - return switch.get(grade.upper(), "Please enter 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 16b00c5..eafb50b 100644 --- a/problems/easy/easy_q16.py +++ b/problems/easy/easy_q16.py @@ -3,13 +3,13 @@ def sum_first_last(arr): return arr[0] + arr[-1] if __name__ == "__main__": # Handle the input by Yourself - - arr=[] + arr=[2,5,8,7,6] + n=int(input("Enter the Total number of Elements:")) for i in range(n): x=int(input("Enter the element :")) arr.append(x) - arr=[2,5,8,7,6] - + + print(arr) print(sum_first_last(arr)) \ No newline at end of file diff --git a/problems/easy/easy_q18.py b/problems/easy/easy_q18.py index 8c32a32..aaa3e79 100644 --- a/problems/easy/easy_q18.py +++ b/problems/easy/easy_q18.py @@ -10,13 +10,13 @@ def last_char_of_string(s): last_char_of_string(string) - return s[-1] # 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 s=input("Enter the string:") print(last_char_of_string(s)) - s=[7,8,9,6] - print( last_char_of_string(s)) +s=[7,8,9,6] +print( last_char_of_string(s)) \ No newline at end of file diff --git a/problems/easy/easy_q2.py b/problems/easy/easy_q2.py index 4b70828..e314731 100644 --- a/problems/easy/easy_q2.py +++ b/problems/easy/easy_q2.py @@ -2,11 +2,20 @@ def largest_of_two(a, b): if a > b: + print(num1,"is greater") + + + return a + + + return a + else: print(num2,"is greater") return b + if __name__ == "__main__": num1 = int(input("Enter the First Number :")) num2 = int(input("Enter the Second Number :")) @@ -25,4 +34,15 @@ def largest_of_two(a, b): - \ No newline at end of file + + + +if __name__ == "__main__": + num1 = int(input("Enter the First Number :")) + num2 = int(input("Enter the Second :")) + res = largest_of_two(num1,num2) + print(res) + + + + diff --git a/problems/easy/easy_q3.py b/problems/easy/easy_q3.py index 7a40870..05edfbf 100644 --- a/problems/easy/easy_q3.py +++ b/problems/easy/easy_q3.py @@ -1,6 +1,7 @@ # 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" else: @@ -11,4 +12,10 @@ def is_leap_year(year): num = int(input("Enter the number :")) res = is_leap_year(num) - print(res) \ No newline at end of file + print(res) + + if year % 4 == 0 or year % 100 == 0 or year % 400 == 0: + return "Leap Year" + else: + return "Not a Leap Year" + \ No newline at end of file diff --git a/problems/easy/easy_q4.py b/problems/easy/easy_q4.py index 6acc77a..444d69e 100644 --- a/problems/easy/easy_q4.py +++ b/problems/easy/easy_q4.py @@ -1,6 +1,23 @@ # Positive, Negative, or Zero: Accept a number and check if it is positive, negative, or zero. + +num = int(input("Enter the Number : ")) + def check_number(num): + + if num < 0: + print("Negative") + elif num > 0: + print("Positive") + else: + print("Number is zero") + +if __name__ == "__main__": + num = int(input("Enter the Number : ")) + check_number(num) + + + print("The given number is Negative" ) if (num < 0 ) else "The given number is Positive" if (num > 0) else "The given number is Zero") @@ -14,6 +31,20 @@ def check_number(num): print(res) - - + + + + 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 = int(input("Enter the Number : ")) + check_number(num) + + + if num > 0: + print("positive") + elif num < 0: + print("negative") + else: + print("Number is zero") + diff --git a/problems/easy/easy_q5.py b/problems/easy/easy_q5.py index 96fa00c..837028e 100644 --- a/problems/easy/easy_q5.py +++ b/problems/easy/easy_q5.py @@ -1,4 +1,12 @@ def grade_system(marks): +<<<<<<< HEAD + if marks >= 90: + return "A" + elif marks >= 80: + return "B" + elif marks >= 70: + return "F" +======= if marks >=90 and marks <=100: return "A" @@ -6,10 +14,17 @@ def grade_system(marks): return "B" elif marks >= 70 and marks <=80: return "C" +>>>>>>> 3b0e744f348a48fb41210eb4aafaf07141d02bb8 else: return "D" if __name__ == "__main__": +<<<<<<< HEAD + num = int(input("Enter the Mark : ")) + result = grade_system(num) + print(result) + +======= num = int(input("Enter the Mark : ")) res = grade_system(num) print(res) @@ -22,6 +37,7 @@ def grade_system(marks): return "C" else: + return "F" @@ -31,8 +47,16 @@ def grade_system(marks): +if __name__ == "__main__": + mark = int(input("Enter the Mark : ")) + res = grade_system(mark) + + + return "D" + + if __name__ == "__main__": num = int(input("Enter the Mark : ")) res = grade_system(num) - print(res) +>>>>>>> 3b0e744f348a48fb41210eb4aafaf07141d02bb8 diff --git a/problems/easy/easy_q6.py b/problems/easy/easy_q6.py index f8d83d0..2dd80d0 100644 --- a/problems/easy/easy_q6.py +++ b/problems/easy/easy_q6.py @@ -8,6 +8,10 @@ def print_numbers(n): if __name__ == "__main__": num = int(input("Enter the Number ")) + + result = print_numbers(num) + print(result) + print_numbers(num) i+= 1 @@ -24,16 +28,10 @@ def print_numbers(n): res = print_numbers(num) print(res) + i += 1 if __name__ == "__main__": - num = int(input("Enter the Number ")) - print_numbers(num) - - - num = int(input("Enter the Number :")) + num = int(input("Enter the Number ")) print_numbers(num) - - - - + \ No newline at end of file diff --git a/problems/easy/easy_q7.py b/problems/easy/easy_q7.py index 7f13401..19d72dc 100644 --- a/problems/easy/easy_q7.py +++ b/problems/easy/easy_q7.py @@ -1,4 +1,6 @@ + def sum_of_digits(num): + total = 0 while num > 0: @@ -15,22 +17,18 @@ def sum_of_digits(num): total += num % 10 - num = num//10 - - - num = num // 10 - num //= 10 - + num = num // 10 + print(total) return total if __name__ == "__main__": - num = int(input("Enter the Number : ")) - print(sum_of_digits(num)) + no = int(input("Enter the Number : ")) + sum_of_digits(no) + - res=sum_of_digits(num) - print(res) + num = num//10 + + return total - - \ No newline at end of file diff --git a/problems/easy/easy_q8.py b/problems/easy/easy_q8.py index 2ef8ea3..95f7382 100644 --- a/problems/easy/easy_q8.py +++ b/problems/easy/easy_q8.py @@ -3,12 +3,10 @@ def reverse_number(num): rev = "" while num != 0: - digit = num % 10 #digit = 123%10 = 3 12%10 - rev = rev + str(digit) #rev = 3 - num //= 10 # num//= 10 = 12 - return rev - - + digit = num % 10 + rev = rev + digit + num //= 10 + return num if __name__ == "__main__": num = int(input("Enter num : ")) #123 res = reverse_number(num) diff --git a/problems/easy/easy_q9.py b/problems/easy/easy_q9.py index 2c82f2e..e4c65c3 100644 --- a/problems/easy/easy_q9.py +++ b/problems/easy/easy_q9.py @@ -1,32 +1,15 @@ # Factorial of a Number: Write a program to calculate the factorial of a given number using a while loop. -def factorial(n): +def factorial(num): result = 1 - - i=1 - while i <= n: - result *= i - i += 1 - - while n > 0: + for n in range(1,num+1): result *= n - n -= 1 - + n += 1 return result if __name__ == "__main__": - n= int(input("Enter the Number :")) - print(factorial(n)) + num= int(input("Enter the Number :")) + print(f"Factorial of the number {num} is ",factorial(num)) - num = int(input("Enter the Number :")) - - print(factorial(num)) - - - print(factorial(num)) - - - res=factorial(num) - print(res) - + diff --git a/problems/medium/abc.txt b/problems/medium/abc.txt new file mode 100644 index 0000000..5538c28 --- /dev/null +++ b/problems/medium/abc.txt @@ -0,0 +1,2 @@ +HI +HELLO EVERYONE \ No newline at end of file diff --git a/problems/medium/m1.py b/problems/medium/m1.py index 6aeb903..750dd34 100644 --- a/problems/medium/m1.py +++ b/problems/medium/m1.py @@ -1,28 +1,61 @@ + ''' Create a menu to perform basic mathematical operations (addition, subtraction, multiplication, division, modulo) on two numbers. ''' -def math_operations_menu(): - print("1. Add") - print("2. Subtract") - print("3. Multiply") - print("4. Divide") - print("5. Modulo") - choice = int(input("Enter your choice: ")) - a=int(input("Enter a number")) - b=int(input("Enter a number")) + +def math_operations_menu(choice): + + try: + a, b = map(int, input("Enter two numbers (separated by a comma): ").split(sep=",")) + + # Perform the operation based on user's choice + if choice == 1: + print(f"Addition of {a} and {b}: {a + b}") + elif choice == 2: + print(f"Subtraction of {a} and {b}: {a - b}") + elif choice == 3: + print(f"Multiplication of {a} and {b}: {a * b}") + elif choice == 4: + if b == 0: + print("Error! Division by zero is not allowed.") + else: + print(f"Division of {a} and {b}: {a / b}") + elif choice == 5: + if b == 0: + print("Error! Modulo by zero is not allowed.") + else: + print(f"Modulus of {a} and {b}: {a % b}") + elif choice == 6: + print(f"{a} to the power of {b}: {a ** b}") + else: + print("Invalid option!!!") + except ValueError: + print("Invalid input! Please enter two valid numbers separated by a comma.") + + a, b = map(int, input("Enter two numbers(separated by commas): ").split(sep=",")) + + + a=int(input("Enter a num1:")) + b=int(input("Enter a num2:")) + + + if choice == 1: - print("Addition:", a + b) + print("Addition:", a + b) elif choice == 2: - print("Subtraction:",a - b) + print("Subtraction:", a - b) elif choice == 3: - print("Division:", a / b) + print("Multiplication:", a * b) elif choice == 4: - print("Multiplication:", a * b) + print("Division:", a / b) elif choice == 5: - print("Modulo:", a % b) + print("Modulo:", a // b) else: print("Invalid option") + math_operations_menu() + + diff --git a/problems/medium/m2.py b/problems/medium/m2.py index a980b44..0ffc8b9 100644 --- a/problems/medium/m2.py +++ b/problems/medium/m2.py @@ -21,16 +21,24 @@ def array_operations_menu(): print("Smallest Element:", min(arr)) elif choice == 4: - print("Sorted Array:", sorted(arr)) + + print("Soxrted Array:", sorted(arr)) else: print("Invalid option") array_operations_menu() + arr.sort() # sorts the array in ascending order by default print("Sorted Array:", arr) else: print("Invalid option") + + print("Sorted Array:",sorted.arr) + else: + print("Invalid option") + + array_operations_menu() arr.sort() @@ -44,5 +52,5 @@ def array_operations_menu(): else: print("Invalid option") array_operations_menu() - + \ No newline at end of file diff --git a/problems/medium/m5.py b/problems/medium/m5.py index 618e1b6..ff78fe7 100644 --- a/problems/medium/m5.py +++ b/problems/medium/m5.py @@ -27,3 +27,4 @@ def matrix_operations_menu(): print("Transpose:", transpose) else: print("Invalid option") +matrix_operations_menu()