Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Quick Sort and Tic Tac Toe #50

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions My-initial_pull/quick_sort.py
Original file line number Diff line number Diff line change
@@ -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)
49 changes: 49 additions & 0 deletions My-initial_pull/tic-tac-toe.py
Original file line number Diff line number Diff line change
@@ -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()