Coding/Basic Skills(Python)

[Programmers, Level 0] 캐릭터 좌표(Character Coordination)

xeohyuni 2023. 4. 23. 12:04

Problem

Mussuk is playing RPG. The game has an up, down, left, right rudder, and when you press each key, you move up, down, left, and right. For example, if you press up at [0,0], the coordinates of the character are [0,1], if you press down, [0,-1], if you press left, [-1,0], if you press right, [1,0]. The parameters are given the array keyinput of the arrow keys entered by the awkward and the size board of the map. Always complete the solution function so that the character returns the coordinates [x, y] of the character after all the keystrokes are finished when the character starts at [0,0].

[0, 0] is located in the center of the board. For example, if the board has a width of 9, the character can move up to [-4, 0] to the left and up to [4, 0] to the right.

Restriction

  • The board is given in the form [horizontal, vertical].
  • The board's horizontal and vertical sizes are odd.
  • Ignores directional key input beyond the size of the board.
  • 0 ≤ keyinput length ≤ 50
  • 1 ≤ board[0] ≤ 99
  • 1 ≤ board[1] ≤ 99
  • "Keyinput" is always given only up, down, left, and right.
Input Board Output
["left", "right", "up", "right", "right"] [11, 11] [2, 1]
["down", "down", "down", "down", "down"] [7, 9] [0, -4]

Input/Output Example Description

Input #1

From [0, 0], one unit to the left, one unit to the right, one unit up, two units to the right -> the coordinates are [2, 1].

--------------------------------------------------------------------------------------------------------------------

Input #2

The coordinates moved down five spaces from [0, 0] are [0, -5], but the map's vertical size is 9, so you cannot move down more than four spaces. Therefore, return [0, -4].

Code

def solution(keyinput, board):
    answer = [0,0]#현재 좌표 상태
    for key in keyinput:
        if key == "up" and board[1]//2 >= answer[1]+1:#위로 가기
            answer[1] += 1# 현재 [0,0]의 y좌표에 더해주기
        elif key == "down" and -(board[1]//2) <= answer[1]-1:#아래로 가기
            answer[1] -= 1# 현재 [0,0]의 y좌표에 빼주기
        elif key == "left" and -(board[0]//2) <= answer[0]-1:#왼쪽으로 가기
            answer[0] -= 1# 현재 [0,0]의 x좌표에 뺴주기
        elif key == "right" and board[0]//2 >= answer[0]+1:#오른족으로 가기
            answer[0]  += 1# 현재 [0,0]의 x좌표에 더해주기
    return answer#출력

EXPLANATION

The key point of this porblem is to determine if conditional statement balances each other when using 'and'.

If one of the statement is false, the program won't work in the if statement --> error.

Revising And & Or in python

Let's say that false statement = 0 and true statement = 0

And: when statements are togther and either one of the statement is false, the result always(must!!) end up with false

0&0 = false(0) because both statement is false

0&1 = false(0) because one of the statement is false

1&0 = false(0) because one of the statement is false

1&1 = True(1) because both statement is true

Or: when statements are together and either on of the statement is true, the resukt always(must!!) end up with true

0&0 = false(0)

1&0 = true(1)

0&1 = true(1)

1&1 = true(1)

Check your understanding: 

*|| means or*

 Q: 0 & 0 || 1 & 1 || 0 & 0 || 0 & 1 || 0

Hint: group each equation

.

.

.

.

.

.

.

Answer/explanation:

Answer = 0 --> false