From 3ebefe4f4ce0d88565792a2d8f7bbb3851ab5e70 Mon Sep 17 00:00:00 2001 From: Ishan Tharindu <73461067+gitgunawardhana@users.noreply.github.com> Date: Sat, 7 Oct 2023 17:06:17 +0530 Subject: [PATCH 1/5] Create TicTacCode.py --- Python Projects/TicTacCode.py | 48 +++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Python Projects/TicTacCode.py diff --git a/Python Projects/TicTacCode.py b/Python Projects/TicTacCode.py new file mode 100644 index 0000000..6c40f2d --- /dev/null +++ b/Python Projects/TicTacCode.py @@ -0,0 +1,48 @@ +def print_board(board): + for row in board: + print(" | ".join(row)) + print("-" * 9) + +def check_winner(board, player): + # Check rows, columns, and diagonals + for i in range(3): + if all([cell == player for cell in board[i]]) or all([board[j][i] == player for j in range(3)]): + return True + if all([board[i][i] == player for i in range(3)]) or all([board[i][2 - i] == player for i in range(3)]): + return True + return False + +def is_board_full(board): + return all([cell != " " for row in board for cell in row]) + +def main(): + board = [[" " for _ in range(3)] for _ in range(3)] + players = ["X", "O"] + current_player_idx = 0 + + print("Welcome to Tic-Tac-Toe!") + print_board(board) + + while True: + row = int(input("Enter the row (0, 1, or 2): ")) + col = int(input("Enter the column (0, 1, or 2): ")) + + if board[row][col] != " ": + print("That position is already taken. Try again.") + continue + + board[row][col] = players[current_player_idx] + print_board(board) + + if check_winner(board, players[current_player_idx]): + print(f"Player {players[current_player_idx]} wins! Congratulations!") + break + + if is_board_full(board): + print("It's a draw! Good game!") + break + + current_player_idx = 1 - current_player_idx # Switch player + +if __name__ == "__main__": + main() From 84a174eeeefc859107832610cbafae88e1c2710f Mon Sep 17 00:00:00 2001 From: Ishan Tharindu <73461067+gitgunawardhana@users.noreply.github.com> Date: Sat, 7 Oct 2023 17:11:07 +0530 Subject: [PATCH 2/5] Create SimpleCalculator.py --- Python Projects/SimpleCalculator.py | 44 +++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Python Projects/SimpleCalculator.py diff --git a/Python Projects/SimpleCalculator.py b/Python Projects/SimpleCalculator.py new file mode 100644 index 0000000..a7d7667 --- /dev/null +++ b/Python Projects/SimpleCalculator.py @@ -0,0 +1,44 @@ +def add(x, y): + return x + y + +def subtract(x, y): + return x - y + +def multiply(x, y): + return x * y + +def divide(x, y): + if y != 0: + return x / y + else: + return "Cannot divide by zero!" + +print("Simple Calculator") +print("Select operation:") +print("1. Add") +print("2. Subtract") +print("3. Multiply") +print("4. Divide") + +while True: + choice = input("Enter choice (1/2/3/4): ") + + if choice in ["1", "2", "3", "4"]: + num1 = float(input("Enter first number: ")) + num2 = float(input("Enter second number: ")) + + if choice == "1": + print("Result: ", add(num1, num2)) + elif choice == "2": + print("Result: ", subtract(num1, num2)) + elif choice == "3": + print("Result: ", multiply(num1, num2)) + elif choice == "4": + print("Result: ", divide(num1, num2)) + else: + print("Invalid Input") + + another_calculation = input("Do you want to perform another calculation? (yes/no): ") + if another_calculation.lower() != "yes": + print("Thank you for using the calculator. Goodbye!") + break From 6a36c42d76aa95269928916830b3bdcb63325ced Mon Sep 17 00:00:00 2001 From: Ishan Tharindu <73461067+gitgunawardhana@users.noreply.github.com> Date: Sat, 7 Oct 2023 17:12:16 +0530 Subject: [PATCH 3/5] Create GuessNumber.py --- Python Projects/GuessNumber.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Python Projects/GuessNumber.py diff --git a/Python Projects/GuessNumber.py b/Python Projects/GuessNumber.py new file mode 100644 index 0000000..b094564 --- /dev/null +++ b/Python Projects/GuessNumber.py @@ -0,0 +1,26 @@ +import random + +def guess_the_number(): + print("Welcome to Guess the Number Game!") + number_to_guess = random.randint(1, 100) + attempts = 0 + + while True: + try: + user_guess = int(input("Enter your guess (between 1 and 100): ")) + attempts += 1 + + if user_guess < 1 or user_guess > 100: + print("Please enter a number between 1 and 100.") + elif user_guess < number_to_guess: + print("Too low! Try again.") + elif user_guess > number_to_guess: + print("Too high! Try again.") + else: + print(f"Congratulations! You guessed the number in {attempts} attempts!") + break + except ValueError: + print("Invalid input! Please enter a valid number.") + +if __name__ == "__main__": + guess_the_number() From 56a96a316bc37b2a289fbb7238c21e49d226536f Mon Sep 17 00:00:00 2001 From: Ishan Tharindu <73461067+gitgunawardhana@users.noreply.github.com> Date: Sat, 7 Oct 2023 17:12:53 +0530 Subject: [PATCH 4/5] Create guess_the_number.py --- Python Projects/guess_the_number.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Python Projects/guess_the_number.py diff --git a/Python Projects/guess_the_number.py b/Python Projects/guess_the_number.py new file mode 100644 index 0000000..b094564 --- /dev/null +++ b/Python Projects/guess_the_number.py @@ -0,0 +1,26 @@ +import random + +def guess_the_number(): + print("Welcome to Guess the Number Game!") + number_to_guess = random.randint(1, 100) + attempts = 0 + + while True: + try: + user_guess = int(input("Enter your guess (between 1 and 100): ")) + attempts += 1 + + if user_guess < 1 or user_guess > 100: + print("Please enter a number between 1 and 100.") + elif user_guess < number_to_guess: + print("Too low! Try again.") + elif user_guess > number_to_guess: + print("Too high! Try again.") + else: + print(f"Congratulations! You guessed the number in {attempts} attempts!") + break + except ValueError: + print("Invalid input! Please enter a valid number.") + +if __name__ == "__main__": + guess_the_number() From ca2ef6ab013f208b826c2aaaaf8a5bf7b4f7d2ef Mon Sep 17 00:00:00 2001 From: Ishan Tharindu <73461067+gitgunawardhana@users.noreply.github.com> Date: Sat, 7 Oct 2023 17:14:01 +0530 Subject: [PATCH 5/5] Create MergeSortedLinkedLists.py --- Python Projects/MergeSortedLinkedLists.py | 50 +++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Python Projects/MergeSortedLinkedLists.py diff --git a/Python Projects/MergeSortedLinkedLists.py b/Python Projects/MergeSortedLinkedLists.py new file mode 100644 index 0000000..847c0d7 --- /dev/null +++ b/Python Projects/MergeSortedLinkedLists.py @@ -0,0 +1,50 @@ +# Definition for singly-linked list. +class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next + +def merge_sorted_lists(l1, l2): + dummy = ListNode(-1) + current = dummy + + while l1 is not None and l2 is not None: + if l1.val < l2.val: + current.next = l1 + l1 = l1.next + else: + current.next = l2 + l2 = l2.next + + current = current.next + + # If one of the lists is not empty, append it to the merged list + if l1 is not None: + current.next = l1 + elif l2 is not None: + current.next = l2 + + return dummy.next + +# Helper function to print the merged linked list +def print_list(head): + current = head + while current: + print(current.val, end=" -> ") + current = current.next + print("None") + +# Example usage +# Creating sorted linked lists: 1 -> 3 -> 5 and 2 -> 4 -> 6 +list1 = ListNode(1, ListNode(3, ListNode(5))) +list2 = ListNode(2, ListNode(4, ListNode(6))) + +print("First sorted list:") +print_list(list1) + +print("Second sorted list:") +print_list(list2) + +merged_list = merge_sorted_lists(list1, list2) +print("Merged sorted list:") +print_list(merged_list)