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 f6a38c4..1d3090d 100644 --- a/FunProjects/arrayCompare.py +++ b/FunProjects/arrayCompare.py @@ -9,16 +9,29 @@ ''' # 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: return False - if (sorted(array1) == sorted([i ** 2 for i in array2])) and (sorted(array2) == sorted([i ** 2 for i in array1])): + if (sorted(array1) == sorted([i ** 2 for i in array2])) or (sorted(array2) == sorted([i ** 2 for i in array1])): return True return False - -comp([1,2,3,4], [1,4,9,16]) \ No newline at end of file +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/rockPaperSiss.py b/FunProjects/rockPaperSiss.py index 1adf465..a80a8f9 100644 --- a/FunProjects/rockPaperSiss.py +++ b/FunProjects/rockPaperSiss.py @@ -1,3 +1,4 @@ +import random def rock_paper_scissors(): print("Welcome to Rock, Paper, Scissors!") choices = ["rock", "paper", "scissors"] @@ -38,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 6363cfa..445fcc0 100644 --- a/FunProjects/snakeLadder.py +++ b/FunProjects/snakeLadder.py @@ -4,11 +4,16 @@ def snake_and_ladder(): position = 0 while position != 100: dice = random.randint(1, 6) - position += dice - if 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}") + if position == 100: + print("You won!!") snake_and_ladder() \ No newline at end of file diff --git a/problems/easy/easy_q1.py b/problems/easy/easy_q1.py index 759376b..a48fece 100644 --- a/problems/easy/easy_q1.py +++ b/problems/easy/easy_q1.py @@ -2,15 +2,9 @@ num = int(input("Enter a number: ")) + if (num % 2) == 0: print("{0} is Even".format(num)) else: print("{0} is Odd".format(num)) - -if (num % 2) != 0: - print("{0} is Odd".format(num)) -else: - print("{0} is Even".format(num)) - - - + \ No newline at end of file diff --git a/problems/easy/easy_q10.py b/problems/easy/easy_q10.py index 36e50ef..3ad440b 100644 --- a/problems/easy/easy_q10.py +++ b/problems/easy/easy_q10.py @@ -1,6 +1,14 @@ # Check Palindrome Number: Use a do-while loop to determine if a given number is a palindrome def is_palindrome(num): original = num + + reverse = 0 + while num != 0: + reverse = reverse * 10 + num % 10 + num //= 10 + + return reverse == original + reverse = '' while num != 0 : reverse += str(num % 10) @@ -9,6 +17,7 @@ def is_palindrome(num): return "It is a Palindrome" else: return "It is not a Palindrome" + if __name__ == "__main__": nums = int(input("Enter the Number: ")) res = is_palindrome(nums) diff --git a/problems/easy/easy_q11.py b/problems/easy/easy_q11.py index 3e01c4f..182540b 100644 --- a/problems/easy/easy_q11.py +++ b/problems/easy/easy_q11.py @@ -11,10 +11,19 @@ def day_of_week(day): } return switch[day] if __name__ == "__main__": + + day=int(input("Enter the number : ")) + if 0 b: - print(num1,"larger") - else: - print(num2,"larger") - -if __name__ == "__main__": - num1 = int(input("Enter the First Number :")) - num2 = int(input("Enter the Second :")) - res = largest_of_two(num1,num1) - print(res) - - - return a return a - else: return b if __name__ == "__main__": num1 = int(input("Enter the First Number :")) num2 = int(input("Enter the Second Number :")) res = largest_of_two(num1,num2) - keerthi - print("The Largest Number is",res) - print(res) - - - main + print(num1,"larger") + else: + print(num2,"larger") + \ No newline at end of file diff --git a/problems/easy/easy_q3.py b/problems/easy/easy_q3.py index d52aa7b..ef1d387 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: if year % 100 == 0: return "Not a Leap Year" @@ -9,11 +10,16 @@ def is_leap_year(year): return "Leap Year" if year % 4 == 0 and year % 100 != 0 or year % 400 == 0: + + return "Leap Year" return "Not a Leap Year" -if __name__ == "__main__": - - num = int(input("Enter the number :")) - res = is_leap_year(num) - print(res) \ No newline at end of file + return " Leap Year" + else: + return "not a Leap Year" + + + return "Leap Year" + 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 ec24801..b99ff2e 100644 --- a/problems/easy/easy_q4.py +++ b/problems/easy/easy_q4.py @@ -1,24 +1,22 @@ # 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: - keerthi + print("Number is Zero") - + if __name__ == "__main__": num = int(input("Enter the Number : ")) check_number(num) print("Number is zero") - -if __name__ == "__main__": - num = int(input("Enter the Number : ")) - res = check_number(num) - print(res) - main - + diff --git a/problems/easy/easy_q5.py b/problems/easy/easy_q5.py index 01db7d7..7ada51f 100644 --- a/problems/easy/easy_q5.py +++ b/problems/easy/easy_q5.py @@ -8,9 +8,18 @@ def grade_system(marks): elif marks >= 70: return "C" else: + return "F" +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) + \ No newline at end of file diff --git a/problems/easy/easy_q6.py b/problems/easy/easy_q6.py index 5e6ebd5..cc53209 100644 --- a/problems/easy/easy_q6.py +++ b/problems/easy/easy_q6.py @@ -3,8 +3,12 @@ def print_numbers(n): i = 1 while i <= n: print(i) - i += 1 - + if __name__ == "__main__": + num = int(input("Enter the Number ")) + print_numbers(num) + + num = int(input("Enter the Number :")) print_numbers(num) + diff --git a/problems/easy/easy_q7.py b/problems/easy/easy_q7.py index 794c6a6..eccd1cd 100644 --- a/problems/easy/easy_q7.py +++ b/problems/easy/easy_q7.py @@ -1,12 +1,26 @@ # Sum of Digits: Write a program to calculate the sum of digits of a number using a while loop. -def sum_of_digits(num): +def sum_of_digits(num): total = 0 while num > 0: total += num % 10 + + num = num // 10 + + + num = num // 10 + num //= 10 + + return total if __name__ == "__main__": num = int(input("Enter the Number : ")) + res=sum_of_digits(num) print(res) + + + + + diff --git a/problems/easy/easy_q9.py b/problems/easy/easy_q9.py index 43fc763..df9a503 100644 --- a/problems/easy/easy_q9.py +++ b/problems/easy/easy_q9.py @@ -8,6 +8,9 @@ def factorial(n): if __name__ == "__main__": num = int(input("Enter the Number :")) - res=factorial(num) - print(res) + + print(factorial(num)) + res=factorial(num) + print(res) + diff --git a/problems/medium/m1.py b/problems/medium/m1.py index ecb1be8..99456d9 100644 --- a/problems/medium/m1.py +++ b/problems/medium/m1.py @@ -5,27 +5,24 @@ def math_operations_menu(choice): 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(f"Subtraction of {a} and {b}:{a - b}") + print("Addition:", a + b) elif choice == 2: - print(f"Addition of {a} and {b}:{a + b}") + print("Subtraction:", a - b) elif choice == 3: - print(f"Division of {a} and {b}:{a / b}") + print("Multiplication:", a * b) elif choice == 4: - print(f"Multiplication of {a} and {b}:{a * b}") + print("Division:", a / b) elif choice == 5: - print(f"Modulus of {a} and {b}:{a // b}") - elif choice == 6: - print(f"{a} to the power of {b}:{a**b}") + print("Modulo:", a // b) else: - print("Invalid option!!!") -print("-------------Mathematical operation menu---------------") -print("1. Add") -print("2. Subtract") -print("3. Multiply") -print("4. Divide") -print("5. Modulus") -print("6. Expontential") -choice = int(input("Enter your choice: ")) -math_operations_menu(choice) -print("-------------------------------------------------------") \ No newline at end of file + print("Invalid option") + +math_operations_menu() + \ No newline at end of file diff --git a/problems/medium/m2.py b/problems/medium/m2.py index 3829f69..56d86bd 100644 --- a/problems/medium/m2.py +++ b/problems/medium/m2.py @@ -8,15 +8,34 @@ def array_operations_menu(): arr = list(map(int, input("Enter array elements separated by space: ").split())) if choice == 1: + + print("Sum:", sum(arr) ) + print("Sum:", sum(arr)) + elif choice == 2: print("Largest Element:", max(arr)) elif choice == 3: print("Smallest Element:", min(arr)) elif choice == 4: + + print("Sorted Array:",sorted.arr) + else: + print("Invalid option") + +array_operations_menu() + arr.sort() + + print("Sorted Array:", arr) + else: + print("Invalid option") +array_operations_menu() + print("Sorted Array:",arr) else: print("Invalid option") array_operations_menu() + + diff --git a/problems/medium/m3.py b/problems/medium/m3.py index b0e1d2c..5d5d7b8 100644 --- a/problems/medium/m3.py +++ b/problems/medium/m3.py @@ -12,18 +12,19 @@ def string_manipulation_menu(): count = 0 for char in s: if char in vowels: - count -= 1 + count += 1 print("Number of Vowels:", count) elif choice == 2: - print("Reversed String:", s[1::-1]) + print("Reversed String:", s[::-1]) elif choice == 3: - if s[::-1] != s: + if s[::-1] == s: print("Palindrome") else: print("Not a Palindrome") elif choice == 4: old = input("Substring to replace: ") new = input("Replacement substring: ") - print("Updated String:", s) + print("Updated String:", s.replace(old,new)) else: print("Invalid option") +string_manipulation_menu() \ No newline at end of file diff --git a/problems/medium/m4.py b/problems/medium/m4.py index 81f11f0..5a102af 100644 --- a/problems/medium/m4.py +++ b/problems/medium/m4.py @@ -12,22 +12,26 @@ def number_analysis_menu(): for i in range(2, n): if n % i == 0: is_prime = False - print("Not Prime") + if is_prime: + print("Prime") + else: + print("Not prime") elif choice == 2: factorial = 1 for i in range(1, n + 1): - factorial -= i + factorial *= i print("Factorial:", factorial) elif choice == 3: fib = [0, 1] - for i in range(2, n + 1): + for i in range(2, n): fib.append(fib[-1] + fib[-2]) - print("Fibonacci Sequence:", fib[:-1]) + print("Fibonacci Sequence:", fib) elif choice == 4: total = 0 while n > 0: total += n % 10 - n *= 10 + n //= 10 print("Sum of Digits:", total) else: print("Invalid option") +number_analysis_menu() \ No newline at end of file