본문 바로가기

Python_Matter/Solve

Python Learn the basics Quiz 63

반응형

Q>

Tic-Tac-Toe, sometimes also known as Xs and Os, is a game for two players (X and O) who take turns marking the spaces in a 3×3 grid.

(Tic-Tac-Toe는 때로는 Xs와 Os라고도하며, 2 명의 플레이어 (X와 O)가 3 × 3 그리드에서 공백을 표시하는 게임입니다.)

The player who succeeds in placing three respective marks in a horizontal, vertical, or diagonal rows (NW-SE and NE-SW) wins the game.

(수평, 수직 또는 대각선 (NW-SE 및 NE-SW)에 3 개의 각 마크를 넣으면 성공한 플레이어가 게임에서 승리합니다.)

But we will not be playing this game.

(그러나 우리는이 게임을하지 않을 것입니다.)

You will be the referee for this games results.

(이 경기 결과에 대한 심판이됩니다.)

You are given a result of a game and you must determine if the game ends in a win or a draw as well as who will be the winner.

(게임의 결과가 주어지며 게임이 승패에서 끝나는 지 여부와 승자가 누구인지를 결정해야합니다.)

Make sure to return "X" if the X-player wins and "O" if the O-player wins.

( X- 플레이어가 승리하면 "X"를, O- 플레이어가 이기면 "O"를 반환하십시오.)

If the game is a draw, return "D".

(게임이 추첨 인 경우 "D"를 반환합니다)

A game's result is presented as a list of strings, where "X" and "O" are players' marks and "." is the empty cell.

(게임 결과는 문자열 목록으로 표시됩니다. 여기서 "X"및 "O"는 플레이어의 표시이며 "." 빈 셀입니다.)

 

Input: A game result as a list of strings (unicode).

        (문자열 결과 목록 (유니 코드)입니다.)

 

Output: "X", "O" or "D" as a string.

           ("X", "O"또는 "D"를 문자열로 사용합니다.)

 

Example:

 

checkio([
    "X.O",
    "XX.",
    "XOO"]) == "X"
checkio([
    "OO.",
    "XOX",
    "XOX"]) == "O"
checkio([
    "OOX",
    "XXO",
    "OXX"]) == "D"

 

How it is used: The concepts in this task will help you when iterating data types.

                     (이 태스크의 개념은 데이터 유형을 반복 할 때 도움이됩니다.)

                     They can also be used in game algorithms, allowing you to know how to check results.

                     (또한 게임 알고리즘에서 사용할 수 있으므로 결과를 확인하는 방법을 알 수 있습니다)

 

Precondition: There is either one winner or a draw.
                   (우승자 또는 무승부가 있습니다.)

                   len(game_result) == 3
                   all(len(row) == 3 for row in game_result)

 

A>

from typing import List

# x . o
# X X o
# X O O
# == X

def checkio(game_result: List[str]) -> str:
    for x in [0, 1, 2]:
        str = game_result[0][x] + game_result[1][x] + game_result[2][x]
        if str == 'XXX':
            return 'X'
        elif str == 'OOO':
            return 'O'
    for y in [0, 1, 2]:
        str = game_result[y]
        if str == 'XXX':
            return 'X'
        elif str == 'OOO':
            return 'O'
    str = game_result[0][0] + game_result[1][1] + game_result[2][2]
    if str == 'XXX':
        return 'X'
    elif str == 'OOO':
        return 'O'
    str = game_result[0][2] + game_result[1][1] + game_result[2][0]
    if str == 'XXX':
        return 'X'
    elif str == 'OOO':
        return 'O'
    return 'D'

if __name__ == '__main__':
    print("Example:")
    print(checkio(["X.O",
                   "XX.",
                   "XOO"]))

    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert checkio([
        "X.O",
        "XX.",
        "XOO"]) == "X", "Xs wins"
    assert checkio([
        "OO.",
        "XOX",
        "XOX"]) == "O", "Os wins"
    assert checkio([
        "OOX",
        "XXO",
        "OXX"]) == "D", "Draw"
    assert checkio([
        "O.X",
        "XX.",
        "XOO"]) == "X", "Xs wins again"
    print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")

 

O>

Example:
X
Coding complete? Click 'Check' to review your tests and earn cool rewards!

Process finished with exit code 0

 

S>

https://py.checkio.org/

반응형

'Python_Matter > Solve' 카테고리의 다른 글

Python Learn the basics Quiz 65  (0) 2019.06.14
Python Learn the basics Quiz 64  (0) 2019.06.14
Python Learn the basics Quiz 62  (0) 2019.06.14
Python Learn the basics Quiz 61  (0) 2019.06.12
Python Learn the basics Quiz 60  (0) 2019.06.12