[Python] ์ˆ˜ ์กฐ์ž‘ํ•˜๊ธฐ 1

2025. 4. 3. 11:47ใ†[๐Ÿ’ปPython] pearl's python ๋ณ‘์•„๋ฆฌ ํƒˆ์ถœ๊ธฐ ๐Ÿฃ

๋ฌธ์ œ 

์ •์ˆ˜ n๊ณผ ๋ฌธ์ž์—ด control์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. control์€ "w", "a", "s", "d"์˜ 4๊ฐœ์˜ ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, control์˜ ์•ž์—์„œ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ๋ฌธ์ž์— ๋”ฐ๋ผ n์˜ ๊ฐ’์„ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค.
"w" : n์ด 1 ์ปค์ง‘๋‹ˆ๋‹ค."s" : n์ด 1 ์ž‘์•„์ง‘๋‹ˆ๋‹ค."d" : n์ด 10 ์ปค์ง‘๋‹ˆ๋‹ค."a" : n์ด 10 ์ž‘์•„์ง‘๋‹ˆ๋‹ค.
์œ„ ๊ทœ์น™์— ๋”ฐ๋ผ n์„ ๋ฐ”๊ฟจ์„ ๋•Œ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ๋‚˜์˜ค๋Š” n์˜ ๊ฐ’์„ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด ์ฃผ์„ธ์š”.

์ฝ”๋“œ
def solution(n, control):
    result = n
    for i in control:
        if i == "w":
            result += 1
        elif i == "s":
            result -= 1 
        elif i == "d":
            result += 10 
        else:
            result -= 10
    return result
 # ๋” ์‰ฌ์šด ์ฝ”๋“œ 
def solution(n, control):
    answer = n
    c = { 'w':1, 's':-1, 'd':10, 'a':-10}
    for i in control:
        answer += c[i]
    return answer