CodeKata_week2

Lee, Chankyuยท2021๋…„ 10์›” 31์ผ
0
post-thumbnail
post-custom-banner

๐Ÿ“ ๋ฌธ์ œ1

๋กœ๋งˆ์ž์—์„œ ์ˆซ์ž๋กœ ๋ฐ”๊พธ๊ธฐ 1~3999 ์‚ฌ์ด์˜ ๋กœ๋งˆ์ž s๋ฅผ ์ธ์ž๋กœ ์ฃผ๋ฉด ๊ทธ์— ํ•ด๋‹นํ•˜๋Š” ์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ์„ธ์š”.

๋กœ๋งˆ ์ˆซ์ž๋ฅผ ์ˆซ์ž๋กœ ํ‘œ๊ธฐํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000

๋กœ๋งˆ์ž๋ฅผ ์ˆซ์ž๋กœ ์ฝ๋Š” ๋ฐฉ๋ฒ•์€ ๋กœ๋งˆ์ž๋ฅผ ์™ผ์ชฝ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ๋”ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. III = 3 XII = 12 XXVII = 27์ž…๋‹ˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ 4๋ฅผ ํ‘œํ˜„ํ•  ๋•Œ๋Š” IIII๊ฐ€ ์•„๋‹ˆ๋ผ IV ์ž…๋‹ˆ๋‹ค. ๋’ค์˜ ์ˆซ์ž์—์„œ ์•ž์˜ ์ˆซ์ž๋ฅผ ๋นผ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 9๋Š” IX์ž…๋‹ˆ๋‹ค.

I๋Š” V์™€ X์•ž์— ์™€์„œ 4, 9 X๋Š” L, C์•ž์— ์™€์„œ 40, 90 C๋Š” D, M์•ž์— ์™€์„œ 400, 900

๋‚˜์˜ ํ’€์ด

def roman_to_num(s):
    answer = 0
    string = ''
    roman = {
        "I":1,
        "V":5,
        "X":10,
        "L":50,
        "C":100,
        "D":500,
        "M":1000
    }   
    for i in range(len(s)-1, -1, -1):
        string = s[i]
        if "V" in string or "X" in string:
            roman["I"] = -1
        elif "L" in string or "C" in string:
            roman["X"] = -10
        elif "D" in string or "M" in string:
            roman["C"] = -100
        answer += roman[s[i]]
    
    return answer
  • ์ด ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ ์ฝ”๋“œ ์ž‘์„ฑ๋ณด๋‹ค๋Š” ๋ฌธ์ž์—ด์˜ ๊ทœ์น™์„ ์ฐพ๊ณ  ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์„ ์ฐพ๋Š”๊ฒƒ์ด ๋‹ต ๋„์ถœ์˜ 80%์ •๋„๋ฅผ ์ฐจ์ง€ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.
    ๋‚ด๊ฐ€ ์ƒ๊ฐํ•œ ํ’€์ด๋ฒ•์˜ key point๋Š” input ๊ฐ’ s๋ฅผ for๋ฌธ์œผ๋กœ ๋Œ๋ฆฌ๋˜, ์—ญ์ˆœ์œผ๋กœ ์š”์†Œ๋ฅผ ๊บผ๋‚ด๋Š” ๊ฒƒ์ด๋‹ค. ๋กœ๋งˆ์–ด์˜ ๊ทœ์น™์ƒ I, X, C๊ฐ€ ํŠน์ • ๋ฌธ์ž ์•ž์— ์˜ฌ๋•Œ๋Š” ๋งˆ์ด๋„ˆ์Šค๋กœ ์ž‘์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์—ญ์ˆœ์œผ๋กœ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ ค์„œ if๋ฌธ์œผ๋กœ ๊ทœ์น™์„ ์ ์šฉํ•˜์˜€๋‹ค.

๐Ÿ“ ๋ฌธ์ œ2

์ˆซ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์—ด์ธ nums๋ฅผ ์ธ์ž๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

์ˆซ์ž์ค‘์—์„œ ๊ณผ๋ฐ˜์ˆ˜(majority, more than a half)๊ฐ€ ๋„˜์€ ์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ์„ธ์š”.

์˜ˆ๋ฅผ ๋“ค์–ด,
nums = [3,2,3]
return 3

nums = [2,2,1,1,1,2,2]
return 2

๊ฐ€์ •
nums ๋ฐฐ์—ด์˜ ๊ธธ์ด๋Š” ๋ฌด์กฐ๊ฑด 2 ์ด์ƒ์ž…๋‹ˆ๋‹ค.

๋‚˜์˜ ํ’€์ด

#ํ’€์ด1
def more_than_half(nums):
    for i in nums:
        if nums.count(i) > len(nums)//2:
           return i 
#ํ’€์ด2
def more_than_half(nums):
     return [i for i in nums if nums.count(i)>len(nums)//2][0]
  • ๋ฆฌ์ŠคํŠธ๋‚ด ํŠน์ • ์š”์†Œ์˜ ๊ฐฏ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” count() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ input๋˜๋Š” ๋ฆฌ์ŠคํŠธ์˜ ์ „์ฒด ๊ธธ์ด์˜ ์ ˆ๋ฐ˜๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ๋ฅผ ๋ฆฌํ„ดํ•˜์—ฌ ํ’€์ดํ•˜์˜€๋‹ค.
  • ๋‘ ๋ฒˆ์งธ ํ’€์ด๋Š” ์›๋ฆฌ๋Š” ๋™์ผํ•˜๋˜ ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋ณด์•˜๋‹ค. ๋ฆฌ์ŠคํŠธ ํ˜•์‹์œผ๋กœ ๋ฐ˜ํ™˜๋˜๊ธฐ๋•Œ๋ฌธ์— ๋์— ์ธ๋ฑ์‹ฑ์„ ํ•˜์˜€๋‹ค.

๐Ÿ“ ๋ฌธ์ œ3

s๋Š” ์—ฌ๋Ÿฌ ๊ด„ํ˜ธ๋“ค๋กœ ์ด๋ฃจ์–ด์ง„ String ์ธ์ž์ž…๋‹ˆ๋‹ค. s๊ฐ€ ์œ ํšจํ•œ ํ‘œํ˜„์ธ์ง€ ์•„๋‹Œ์ง€ true/false๋กœ ๋ฐ˜ํ™˜ํ•ด์ฃผ์„ธ์š”.

์ข…๋ฅ˜๋Š” '(', ')', '[', ']', '{', '}' ์œผ๋กœ ์ด 6๊ฐœ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๊ฒฝ์šฐ ์œ ํšจํ•ฉ๋‹ˆ๋‹ค.

ํ•œ ๋ฒˆ ๊ด„ํ˜ธ๋ฅผ ์‹œ์ž‘ํ–ˆ์œผ๋ฉด, ๊ฐ™์€ ๊ด„ํ˜ธ๋กœ ๋๋‚ด์•ผ ํ•œ๋‹ค. ๊ด„ํ˜ธ ์ˆœ์„œ๊ฐ€ ๋งž์•„์•ผ ํ•œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.
s = "()"
return true

s = "()[]{}"
return true

s = "(]"
return false

s = "([)]"
return false

s = "{[]}"
return true

๋‚˜์˜ ํ’€์ด

def is_valid(string):
    open = ["[", "{", "("]
    close = ["]", "}", ")"]
    list1 = []

    if len(string) % 2 == 1 or string[0] in close or string[-1] in open:
        return False
        
    for i in range(len(string)): 
        if string[i] in open: 
            list1.append(string[i]) 
        else: 
            if open.index(list1[-1]) == close.index(string[i]): 
                list1.pop()   
            else:
                return False
    return True
  • ์—ด๋ฆฐ ๊ด„ํ˜ธ๋ฅผ open ๋ณ€์ˆ˜์— ๋„ฃ๊ณ  ๋‹ซํžŒ ๊ด„ํ˜ธ๋“ค์„ close ๋ณ€์ˆ˜์— ๋„ฃ๊ณ  ์‹œ์ž‘ํ•˜์˜€๋‹ค. ์šฐ์„  string์˜ ์ „์ฒด ๊ธธ์ด๊ฐ€ ํ™€์ˆ˜์ผ๋•Œ, ๋‹ซํžŒ ๊ด„ํ˜ธ๊ฐ€ ์ œ์ผ ์•ž์— ์žˆ์„๋•Œ, ๊ทธ๋ฆฌ๊ณ  ์—ด๋ฆฐ ๊ด„ํ˜ธ๊ฐ€ ๊ฐ€์žฅ ๋’ค์— ์žˆ์„๋•Œ๋Š” ๋ฌด์กฐ๊ฑด False๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์กฐ๊ฑด๋ฌธ์„ ์ž‘์„ฑํ•˜์˜€๋‹ค.
  • input๋˜๋Š” string์„ ์กฐ๊ฑด๋ฌธ์„ ๋Œ๋ฆฌ๋˜, string[i]๊ฐ’์ด open ๋ฆฌ์ŠคํŠธ ์š”์†Œ์— ํ•ด๋‹น๋˜๋ฉด list1์— append ํ•œ๋‹ค. string[i] ๊ฐ’์ด close ๋ฆฌ์ŠคํŠธ ์š”์†Œ ๊ฐ’์— ํ•ด๋‹น๋˜๋ฉด list1์— ๋“ค์–ด๊ฐ€์žˆ๋Š” ์š”์†Œ์™€ ์ธ๋ฑ์Šค ๊ฐ’(๊ฐ๊ฐ open, close ๋ฆฌ์ŠคํŠธ์—์„œ์˜ ์ธ๋ฑ์Šค ๊ฐ’)์„ ๋น„๊ตํ•œ๋‹ค. ๊ฒฐ๊ณผ๊ฐ€ ๊ฐ™์œผ๋ฉด list1์˜ ๊ฐ€์žฅ ๋’ท ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.(์ธ๋ฑ์Šค ๊ฐ’์ด ๊ฐ™๋‹ค๋Š”๊ฑด ์„œ๋กœ์˜ ์ง์ด๋ผ๋Š” ๊ฒƒ์„ ์˜๋ฏธ!)

๐Ÿ“ ๋ฌธ์ œ4

nums๋Š” ์ˆซ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
๊ฐ€์žฅ ์ž์ฃผ ๋“ฑ์žฅํ•œ ์ˆซ์ž๋ฅผ k ๊ฐœ์ˆ˜๋งŒํผ return ํ•ด์ฃผ์„ธ์š”.

nums = [1,1,1,2,2,3],
k = 2

return [1,2]

nums = [1]
k = 1

return [1]

๋‚˜์˜ ํ’€์ด

def top_k(nums, k):
    answer = []
    while k > 0:
        k -= 1
        a = max(nums, key = lambda x : nums.count(x))
        answer.append(a)
        while a in nums:
            nums.remove(a)
    return answer
  • ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ์ค‘ ๊ฐ€์žฅ ๊ฐฏ์ˆ˜๊ฐ€ ๋งŽ์€ ์š”์†Œ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” max()ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์˜€๋‹ค. ๊ทธ๋ฆฌ๊ณ  key ๊ฐ’์œผ๋กœ lambda ํ•จ์ˆ˜์™€ count()ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•˜์˜€๋‹ค. max()ํ•จ์ˆ˜์˜ key ๊ฐ’์—๋Š” ํ•จ์ˆ˜๊ฐ€ ๋“ค์–ด ๊ฐˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— lambda ํ•จ์ˆ˜๋ฅผ ์š”๊ธดํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • input๋˜๋Š” k ๋งŒํผ ์š”์†Œ๋ฅผ ๋ฝ‘์•„๋‚ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— while ๋ฌธ๊ณผ remove()ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ตœ์ข… ๋‹ต์•ˆ์„ ์™„์„ฑํ•˜์˜€๋‹ค. while๋ฌธ์˜ ์ ์ ˆํ•œ ํ™œ์šฉ๋ฒ•์— ๋Œ€ํ•ด ์ข€ ๋” ์ต์ˆ™ํ•ด์ง€์ž. ์•„์ง์€ for๋ฌธ์— ์น˜์ค‘ํ•˜๋Š” ๊ฒฝํ–ฅ์ด ์žˆ๋‹ค.

๐Ÿ“ ๋ฌธ์ œ5

์ธ์ž์ธ height๋Š” ์ˆซ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.๊ทธ๋ž˜ํ”„๋กœ ์ƒ๊ฐํ•œ๋‹ค๋ฉด y์ถ•์˜ ๊ฐ’์ด๊ณ , ๋†’์ด ๊ฐ’์„ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์•„๋ž˜์˜ ๊ทธ๋ž˜ํ”„๋ผ๋ฉด height ๋ฐฐ์—ด์€ [1, 8, 6, 2, 5, 4, 8, 3, 7] ์ž…๋‹ˆ๋‹ค.

https://storage.googleapis.com/replit/images/1555380144403_97221ca23fbb92beaae5b6c800ceb5c8.pn

์ € ๊ทธ๋ž˜ํ”„์— ๋ฌผ์„ ๋‹ด๋Š”๋‹ค๊ณ  ์ƒ๊ฐํ•˜๊ณ , ๋ฌผ์„ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ๋„“์€ ๋ฉด์ ์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์ฃผ์„ธ์š”.

๊ฐ€์ •
๋ฐฐ์—ด์˜ ๊ธธ์ด๋Š” 2์ด์ƒ์ž…๋‹ˆ๋‹ค.

๋‚˜์˜ ํ’€์ด

def get_max_area(height):
    dic = {}
    for i in range(len(height)):
        for j in range(len(height)):
            width = abs(j - i)
            dic[width] = min(height[i], height[j])
    return max([(i[0]*i[1]) for i in dic.items()])
  • input ๋˜๋Š” height ๊ฐ’ ์š”์†Œ๋“ค์˜ index ๊ฐ’์˜ ์ฐจ์ด๋Š” ์‚ฌ๊ฐํ˜•์˜ ๊ฐ€๋กœ๊ธธ์ด(width)๋ฅผ ์˜๋ฏธํ•˜๊ณ  ๋‘ ์š”์†Œ์ค‘ ์ž‘์€ ๊ฐ’(min()ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„)์€ ์‚ฌ๊ฐํ˜•์˜ ์„ธ๋กœ๊ธธ์ด์— ํ•ด๋‹นํ•œ๋‹ค. ์ด์ค‘ for๋ฌธ๊ณผ ๋”•์…”๋„ˆ๋ฆฌ(key๊ฐ’:width, value๊ฐ’: ์„ธ๋กœ)๋กœ ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ๋ฅผ ๋งค์นญํ•˜์—ฌ dic.items()ํ•จ์ˆ˜์™€ max()ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋‹ต์„ ๊ตฌํ•˜์˜€๋‹ค.
profile
Backend Developer - "Growth itself contains the germ of happiness"
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€