https://school.programmers.co.kr/learn/courses/30/lessons/181934

 

๋ฌธ์ œ

 

๋ฌธ์ œ ์„ค๋ช…

๋ฌธ์ž์—ด์— ๋”ฐ๋ผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‘ ์ˆ˜์˜ ํฌ๊ธฐ๋ฅผ ๋น„๊ตํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

๋‘ ์ˆ˜๊ฐ€ n๊ณผ m์ด๋ผ๋ฉด
">", "=" : n >= m
"<", "=" : n <= m
">", "!" : n > m
"<", "!" : n < m
๋‘ ๋ฌธ์ž์—ด ineq์™€ eq๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ineq๋Š” "<"์™€ ">"์ค‘ ํ•˜๋‚˜๊ณ , eq๋Š” "="์™€ "!"์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‘ ์ •์ˆ˜ n๊ณผ m์ด ์ฃผ์–ด์งˆ ๋•Œ, n๊ณผ m์ด ineq์™€ eq์˜ ์กฐ๊ฑด์— ๋งž์œผ๋ฉด 1์„ ์•„๋‹ˆ๋ฉด 0์„ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

์ œํ•œ ์‚ฌํ•ญ

  • 1 ≤ n, m ≤ 100

 

์ž…์ถœ๋ ฅ ์˜ˆ

ineq eq n m result
"<" "=" 20 50 1
">" "!" 41 78 0

 

 

์†Œ์Šค ์ฝ”๋“œ
# 1
def solution(ineq, eq, n, m):
    answer = 0      
    
    if (n>m) and (ineq==">"):
        answer = 1
    elif (n<m) and (ineq=="<"):
        answer = 1
    elif (n==m) and (eq=="="):
        answer= 1
    
    return answer

 

ํ’€์ด # 1

  • ์ˆซ์ž n,m์˜ ํฌ๊ธฐ๋ฅผ ๋น„๊ตํ•˜๊ณ  ineq์™€ ๊ฐ™์€์ง€ ํ™•์ธ
  • ๋‘ ์ˆซ์ž๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” eq๋ฅผ ๋น„๊ตํ•˜์—ฌ ํ™•์ธ

 

# 2
def solution(ineq, eq, n, m):
    answer = 0      
    
    eq = eq.replace("!","")
    answer = int(eval(str(n)+ineq+eq+str(m)))
    
    return answer

 

ํ’€์ด #2

  • eq๊ฐ€ "!"์ธ ๊ฒฝ์šฐ ๊ณต๋ฐฑ์œผ๋กœ ๋ฐ”๊พธ๊ธฐ
    • replace : ๋ฌธ์ž์—ด์„ ๋ณ€๊ฒฝํ•˜๋Š” ํ•จ์ˆ˜
    • ๋ณ€์ˆ˜.replace("๋ณ€๊ฒฝ ์ „ ๋ฌธ์ž์—ด", "๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ์€ ๋ฌธ์ž์—ด")
  • ์ˆซ์ž์™€ ์—ฐ์‚ฐ์ž๋ฅผ ๋‚˜์—ดํ•˜์—ฌ ์‹คํ–‰
    • eval : ๋ฌธ์ž์—ด์„ ์‹(expression)์œผ๋กœ ๋ฐ›์•„์„œ ์‹คํ–‰ํ•˜๋Š” ํ•จ์ˆ˜
    • ์‹์„ ๋ฌธ์ž์—ด๋กœ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด ์ˆซ์ž๋ฅผ string ํƒ€์ž…์œผ๋กœ ๋ณ€๊ฒฝ
  • True / False๋ฅผ ์ˆซ์ž๋กœ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด int ํƒ€์ž…์œผ๋กœ ๋ณ€๊ฒฝ

 

 

๋ฌธ์ž์—ด ๋ณ€๊ฒฝํ•˜๋Š” ๊ฑฐ ์ƒ๊ฐ๋„ ๋ชปํ–ˆ๋Š”๋ฐ ์„ธ์ƒ์— ๋˜‘๋˜‘ํ•œ ์‚ฌ๋žŒ์ด ๋งŽ๋‹ค...๐Ÿ˜ฎ

+ Recent posts