Answer to Question #347413 in Python for madhu

Question #347413

Ravan's dream is to win Tic-Tac-Toe championship.to accomplish this he practices alone at home to learn the strategies of the game . your task is to identify the cell where Ravan should place his third piece so that he wins the game. Assume that each cell in the tic-tac-toe board is marked with a number as shown in the below

tic tac toe

0 1 2

3 4 5

6 7 8

input:

The first line of input contain two space-seperated integers representing the cells where Ravan has placed his first two pieces

output:

output should be a single line integer representing the cell where ravan should place his final piece

explanation

sample Output 1 :

ravan's first two moves are 0 and 1 so he not 2 to win the game .because it will complete the first row so the out put is 2

sample Output 2 :

ravan's first two moves are 0 and 3 so he not 6 to win the game .because it will complete the first row so the out put is 6


1
Expert's answer
2022-06-02T15:14:29-0400
# inp is the input data, 2 numbers separated by a space
inp = input("ravan's first two moves")
# ind defines the separator of these numbers in the string, by condition it is a space
ind = inp.index(' ')
# a and b are the created variables in which the input numbers will be written
# this is done for a convenient solution to the task
a, b = int(inp[:ind]), int(inp[ind+1:])
# I created a board for the convenience of checking by winning data and displaying the missing cell to win
board = (a, b)

# win_coord this is a tuple with winning options
win_coord = ((0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7), (2, 5, 8), (0, 4, 8), (2, 4, 6))

# using this loop, the input data with the data specified in the win_coord is checked,
# I also decided to take into account that the user can enter not only 0 1, but 1 0,
# and many other options for winning positions, so I swapped the indices through the elif for verification
for each in win_coord:
    if board[0] == each[0] and board[1] == each[1]:
        print(each[2])
    elif board[0] == each[1] and board[1] == each[0]:
        print(each[2])
    elif board[0] == each[1] and board[1] == each[2]:
        print(each[0])
    elif board[0] == each[2] and board[1] == each[1]:
        print(each[0])
    elif board[0] == each[0] and board[1] == each[2]:
        print(each[1])
    elif board[0] == each[2] and board[1] == each[0]:
        print(each[1])

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS