From e1be1d54f451adfe7b94c88b63b101106418454e Mon Sep 17 00:00:00 2001 From: MK-09-coder Date: Wed, 23 Oct 2024 13:40:14 +0530 Subject: [PATCH 1/2] quick sort --- My-initial_pull/quick_sort.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 My-initial_pull/quick_sort.py diff --git a/My-initial_pull/quick_sort.py b/My-initial_pull/quick_sort.py new file mode 100644 index 0000000..53e8a26 --- /dev/null +++ b/My-initial_pull/quick_sort.py @@ -0,0 +1,16 @@ +def quick_sort(arr): + if len(arr) <= 1: + return arr + pivot = arr[len(arr) // 2] + left = [x for x in arr if x < pivot] + middle = [x for x in arr if x == pivot] + right = [x for x in arr if x > pivot] + return quick_sort(left) + middle + quick_sort(right) + +# Example usage: +if __name__ == "__main__": + print("Enter the Array:\n") + sample_array = list(map(int,input().split())) + print("Original array:", sample_array) + sorted_array = quick_sort(sample_array) + print("Sorted array:", sorted_array) From 66d47c45a0d5eec83dde7f72efed8c259a80f37e Mon Sep 17 00:00:00 2001 From: MK-09-coder Date: Wed, 23 Oct 2024 13:42:26 +0530 Subject: [PATCH 2/2] quick sort and tic tac toe --- My-initial_pull/tic-tac-toe.py | 49 ++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 My-initial_pull/tic-tac-toe.py diff --git a/My-initial_pull/tic-tac-toe.py b/My-initial_pull/tic-tac-toe.py new file mode 100644 index 0000000..ff3c085 --- /dev/null +++ b/My-initial_pull/tic-tac-toe.py @@ -0,0 +1,49 @@ +def print_board(board): + for row in board: + print(" | ".join(row)) + print("-" * 5) + +def check_winner(board, player): + # Check rows, columns and diagonals for a win + for row in board: + if all([cell == player for cell in row]): + return True + for col in range(3): + if all([board[row][col] == player for row 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 check_draw(board): + return all([cell != " " for row in board for cell in row]) + +def tic_tac_toe(): + board = [[" " for _ in range(3)] for _ in range(3)] + current_player = "X" + + while True: + print_board(board) + row = int(input(f"Player {current_player}, enter the row (0, 1, 2): ")) + col = int(input(f"Player {current_player}, enter the column (0, 1, 2): ")) + + if board[row][col] != " ": + print("Cell already taken, try again.") + continue + + board[row][col] = current_player + + if check_winner(board, current_player): + print_board(board) + print(f"Player {current_player} wins!") + break + + if check_draw(board): + print_board(board) + print("It's a draw!") + break + + current_player = "O" if current_player == "X" else "X" + +if __name__ == "__main__": + tic_tac_toe() \ No newline at end of file