Coding/Code in the Morning

211103_Code in the morning

๊น€์œ ๋‹ˆ์ฝ˜ 2021. 11. 3. 10:02

์ ํ”„ํˆฌํŒŒ์ด์ฌ 05์žฅ ์—ฐ์Šต๋ฌธ์ œ

Q1

๋‹ค์Œ์€ Calculator ํด๋ž˜์Šค์ด๋‹ค.

class Calculator:
    def __init__(self):
        self.value = 0

    def add(self, val):
        self.value += val

์œ„ ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•˜๋Š” UpgradeCalculator๋ฅผ ๋งŒ๋“ค๊ณ  ๊ฐ’์„ ๋บ„ ์ˆ˜ ์žˆ๋Š” minus ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด ๋ณด์ž. ์ฆ‰ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋™์ž‘ํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค.

cal = UpgradeCalculator()
cal.add(10)
cal.minus(7)

print(cal.value) # 10์—์„œ 7์„ ๋บ€ 3์„ ์ถœ๋ ฅ

ํ’€์ด: 

๋”๋ณด๊ธฐ

 

class UpgradeCalculator(Calculator):
    def minus(self, val):
        self.value -= val


cal = UpgradeCalculator()
cal.add(10)
cal.minus(7)

print(cal.value)

 

Q2

๊ฐ์ฒด๋ณ€์ˆ˜ value๊ฐ€ 100 ์ด์ƒ์˜ ๊ฐ’์€ ๊ฐ€์งˆ ์ˆ˜ ์—†๋„๋ก ์ œํ•œํ•˜๋Š” MaxLimitCalculator ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์ž. ์ฆ‰ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋™์ž‘ํ•ด์•ผ ํ•œ๋‹ค.

 

cal = MaxLimitCalculator()
cal.add(50) # 50 ๋”ํ•˜๊ธฐ
cal.add(60) # 60 ๋”ํ•˜๊ธฐ

print(cal.value) # 100 ์ถœ๋ ฅ

๋‹จ ๋ฐ˜๋“œ์‹œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ Calculator ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•ด์„œ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค.

class Calculator:
    def __init__(self):
        self.value = 0

    def add(self, val):
        self.value += val

ํ’€์ด: 

๋”๋ณด๊ธฐ
class MaxiLimitCalculator:
    def __init__(self):
        self.value = 0

    def add(self,val):
        self.value += val
        if self.value >= 100 :
            self.value = 100

Q3

๋‹ค์Œ ๊ฒฐ๊ณผ๋ฅผ ์˜ˆ์ธกํ•ด ๋ณด์ž.

ํ•˜๋‚˜.

>>> all([1, 2, abs(-3)-3])

ํ•ด์„ค:

abs(-3)์€ -3์˜ ์ ˆ๋Œ€๊ฐ’์ด๋ฏ€๋กœ, all([1, 2, 0])์ด ๋˜๊ณ , 

๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ๊ฐ’ ์ค‘ 0์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, all ๋‚ด์žฅํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ๊ฐ’์€ False๊ฐ€ ๋œ๋‹ค. ํ•จ์ˆ˜ all ์€ iterable ๋‚ด์˜ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ์ฐธ์ด๊ฑฐ๋‚˜ ํ˜น์€ iterable ์ด ๋น„์–ด ์žˆ๋‹ค๋ฉด True ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ์—๋Š” False ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. ์ฆ‰, iterable ๋‚ด์˜ ์š”์†Œ ์ค‘ ๋‹จ ํ•˜๋‚˜๋ผ๋„ ๊ฑฐ์ง“์ธ ๊ฒฝ์šฐ์—๋Š” False ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ํŒŒ์ด์ฌ์—์„œ๋Š” 0์„ False ๋กœ 1์€ True ๋กœ ์ธ์‹ํ•œ๋‹ค. 

 

 

๋‘˜.

>>> chr(ord('a')) == 'a'

ํ’€์ด: 

ord('a')์˜ ๊ฒฐ๊ณผ๋Š” 97์ด ๋˜์–ด chr(97)๋กœ ์น˜ํ™˜๋˜๊ณ , 

chr(97)์€ ๋‹ค์‹œ 'a'๊ฐ€ ๋˜๋ฏ€๋กœ,  'a'=='a' ๊ฐ€ ๋˜์–ด True

ord(๋ฌธ์ž)
ํ•˜๋‚˜์˜ ๋ฌธ์ž๋ฅผ ์ธ์ž๋กœ ๋ฐ›๊ณ  ํ•ด๋‹น ๋ฌธ์ž์— ํ•ด๋‹นํ•˜๋Š” ์œ ๋‹ˆ์ฝ”๋“œ ์ •์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
ord('a')๋ฅผ ๋„ฃ์œผ๋ฉด ์ •์ˆ˜ 97์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
chr(์ •์ˆ˜)
ํ•˜๋‚˜์˜ ์ •์ˆ˜๋ฅผ ์ธ์ž๋กœ ๋ฐ›๊ณ  ํ•ด๋‹น ์ •์ˆ˜์— ํ•ด๋‹นํ•˜๋Š” ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.


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

211126_Code in the morning  (0) 2021.11.26
211123_Code int the morning  (0) 2021.11.23
211101_Code in the morning  (0) 2021.11.01
211029_Code in the morning  (0) 2021.10.29
211027_Code in the morning  (0) 2021.10.27