Coding/Code in the Morning

211123_Code int the morning

๊น€์œ ๋‹ˆ์ฝ˜ 2021. 11. 23. 10:37

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋‘ ์ •์ˆ˜ ์‚ฌ์ด์˜ ํ•ฉ 

 

 

๋ฌธ์ œ: 

  • ๋‘ ์ •์ˆ˜ ์‚ฌ์ด์˜ ํ•ฉ
๋ฌธ์ œ ์„ค๋ช…

๋‘ ์ •์ˆ˜ a, b๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ a์™€ b ์‚ฌ์ด์— ์†ํ•œ ๋ชจ๋“  ์ •์ˆ˜์˜ ํ•ฉ์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•˜์„ธ์š”.
์˜ˆ๋ฅผ ๋“ค์–ด a = 3, b = 5์ธ ๊ฒฝ์šฐ, 3 + 4 + 5 = 12์ด๋ฏ€๋กœ 12๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

์ œํ•œ ์กฐ๊ฑด
  • a์™€ b๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ๋‘˜ ์ค‘ ์•„๋ฌด ์ˆ˜๋‚˜ ๋ฆฌํ„ดํ•˜์„ธ์š”.
  • a์™€ b๋Š” -10,000,000 ์ด์ƒ 10,000,000 ์ดํ•˜์ธ ์ •์ˆ˜์ž…๋‹ˆ๋‹ค.
  • a์™€ b์˜ ๋Œ€์†Œ๊ด€๊ณ„๋Š” ์ •ํ•ด์ ธ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
์ž…์ถœ๋ ฅ ์˜ˆabreturn
3 5 12
3 3 3
5 3 12

 

ํ’€์ด: 

def solution(a, b):
    answer = 0
    if a<=b:
        for i in range (a, b+1):
            answer += i
    else:
        for i in range (b, a+1):
            answer += i
            
    return answer

 

 

๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด: 

def adder(a, b):
    # ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜์„ธ์š”
    if a > b: a, b = b, a

    return sum(range(a,b+1))

# ์•„๋ž˜๋Š” ํ…Œ์ŠคํŠธ๋กœ ์ถœ๋ ฅํ•ด ๋ณด๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.
print( adder(3, 5))

'Coding > Code in the Morning' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

211129_Code in the morning  (0) 2021.11.29
211126_Code in the morning  (0) 2021.11.26
211103_Code in the morning  (0) 2021.11.03
211101_Code in the morning  (0) 2021.11.01
211029_Code in the morning  (0) 2021.10.29