0% found this document useful (0 votes)
7 views3 pages

Programs With Code and Output

The document presents three Python programs: the Water Jug Problem, which measures 5 liters using 3 and 4-liter jugs; a Tic Tac Toe game for two players; and the 8 Queens Problem, which uses backtracking to place 8 queens on a chessboard without threatening each other. Each section includes the aim of the program, the code implementation, and sample output demonstrating the program's functionality. These examples illustrate basic problem-solving techniques in Python programming.

Uploaded by

SUMIT KUMAR
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Programs With Code and Output

The document presents three Python programs: the Water Jug Problem, which measures 5 liters using 3 and 4-liter jugs; a Tic Tac Toe game for two players; and the 8 Queens Problem, which uses backtracking to place 8 queens on a chessboard without threatening each other. Each section includes the aim of the program, the code implementation, and sample output demonstrating the program's functionality. These examples illustrate basic problem-solving techniques in Python programming.

Uploaded by

SUMIT KUMAR
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

WATER JUG PROBLEM

Aim:
Write a Python program to implement water jug problem with two jugs of 3 liters and 4 liters to
measure 5 liters of water.

Code:
x = 0
y = 0
print("JugX\tJugY\tTotal")

while x + y != 5:
if x == 0:
x = 3
elif y < 4:
transfer = min(x, 4-y)
x -= transfer
y += transfer
else:
y = 0
print(f"{x}\t{y}\t{x+y}")

print("Goal reached: 5 liters measured!")

Sample Output:
JugX JugY Total
3 0 3
0 3 3
3 3 6
2 4 6
2 0 2
3 2 5
Goal reached: 5 liters measured!

TIC TAC TOE GAME


Aim:
Write a Python program to implement Tic Tac Toe game for two players.

Code:
board = ['1','2','3','4','5','6','7','8','9']

def display():
print(board[0],"|",board[1],"|",board[2])
print("--+--+--")
print(board[3],"|",board[4],"|",board[5])
print("--+--+--")
print(board[6],"|",board[7],"|",board[8])

def win(p):
return ((board[0]==board[1]==board[2]==p) or
(board[3]==board[4]==board[5]==p) or
(board[6]==board[7]==board[8]==p) or
(board[0]==board[3]==board[6]==p) or
(board[1]==board[4]==board[7]==p) or
(board[2]==board[5]==board[8]==p) or
(board[0]==board[4]==board[8]==p) or
(board[2]==board[4]==board[6]==p))

for turn in range(9):


display()
player = 'X' if turn%2==0 else 'O'
choice = int(input(f"Player {player}, choose position: "))
if board[choice-1] in ['X','O']:
print("Invalid move")
continue
board[choice-1] = player
if win(player):
display()
print("Player",player,"wins!")
break
else:
print("Game Draw!")

Sample Output:
Tic Tac Toe (2 Players)
Player X and Player O
1 | 2 | 3
--+--+--
4 | 5 | 6
--+--+--
7 | 8 | 9
Player X wins!

8 QUEENS PROBLEM
Aim:
Write a Python program to implement 8 queens problem using backtracking.

Code:
N = 8
board = [[0 for _ in range(N)] for _ in range(N)]

def isSafe(r,c):
for i in range(r):
if board[i][c]==1:
return False
i=r-1; j=c-1
while i>=0 and j>=0:
if board[i][j]==1:
return False
i-=1; j-=1
i=r-1; j=c+1
while i>=0 and j<N:
if board[i][j]==1:
return False
i-=1; j+=1
return True

def solve(r):
if r==N:
return True
for c in range(N):
if isSafe(r,c):
board[r][c]=1
if solve(r+1):
return True
board[r][c]=0
return False

solve(0)
for row in board:
print(*row)

Sample Output:
1 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 1 0 0
0 0 1 0 0 0 0 0
0 0 0 0 0 0 1 0
0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0

You might also like