Skip to content
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
26 changes: 26 additions & 0 deletions Python Projects/GuessNumber.py
Original file line number Diff line number Diff line change
@@ -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()
50 changes: 50 additions & 0 deletions Python Projects/MergeSortedLinkedLists.py
Original file line number Diff line number Diff line change
@@ -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)
44 changes: 44 additions & 0 deletions Python Projects/SimpleCalculator.py
Original file line number Diff line number Diff line change
@@ -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
48 changes: 48 additions & 0 deletions Python Projects/TicTacCode.py
Original file line number Diff line number Diff line change
@@ -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()
26 changes: 26 additions & 0 deletions Python Projects/guess_the_number.py
Original file line number Diff line number Diff line change
@@ -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()