CodeKata_week1

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

๐Ÿ“ ๋ฌธ์ œ1

two_sumํ•จ์ˆ˜์— ์ˆซ์ž ๋ฆฌ์ŠคํŠธ์™€ 'ํŠน์ • ์ˆ˜'๋ฅผ ์ธ์ž๋กœ ๋„˜๊ธฐ๋ฉด, ๋”ํ•ด์„œ 'ํŠน์ • ์ˆ˜'๊ฐ€ ๋‚˜์˜ค๋Š” index๋ฅผ ๋ฐฐ์—ด์— ๋‹ด์•„ returnํ•ด ์ฃผ์„ธ์š”.

nums: ์ˆซ์ž ๋ฐฐ์—ด
target: ๋‘ ์ˆ˜๋ฅผ ๋”ํ•ด์„œ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ํ•ฉ๊ณ„
return: ๋‘ ์ˆ˜์˜ index๋ฅผ ๊ฐ€์ง„ ์ˆซ์ž ๋ฐฐ์—ด

์˜ˆ๋ฅผ๋“ค์–ด,
nums์€ [4, 9, 11, 14]
target์€ 13
nums[0] + nums[1] = 4 + 9 = 13 ์ด์ฃ ?
๊ทธ๋Ÿฌ๋ฉด [0, 1]์ด return ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

target์œผ๋กœ ๋ณด๋‚ด๋Š” ํ•ฉ๊ณ„์˜ ์กฐํ•ฉ์€ ๋ฐฐ์—ด ์ „์ฒด ์ค‘์— 2๊ฐœ ๋ฐ–์— ์—†๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

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

def two_sum(nums, target):
    answer = []
    for i in nums:
        for j in nums:
            if i + j == target:
                answer.append(nums.index(i))
                answer.append(nums.index(j))

    return sorted(list(set(answer)))
  • ์ด์ค‘for๋ฌธ์„ ํ†ตํ•ด nums ์š”์†Œ๋“ค์„ ์„œ๋กœ ๋”ํ•ด์ฃผ๋Š” case๋ฅผ ๋งŒ๋“ค๊ณ  if๋ฌธ์„ ํ†ตํ•ด target๊ณผ ์ผ์น˜ํ•˜๋Š”์ง€๋ฅผ ํ™•์ธํ–ˆ๋‹ค. ์ผ์น˜ํ•  ๊ฒฝ์šฐ ๊ฐ๊ฐ์˜ ์š”์†Œ๋“ค์˜ index๋ฅผ answer ๋ฆฌ์ŠคํŠธ์— appendํ•ด์ฃผ์—ˆ๋‹ค.


    ๐Ÿ“ ๋ฌธ์ œ2

    reverse ํ•จ์ˆ˜์— ์ •์ˆ˜์ธ ์ˆซ์ž๋ฅผ ์ธ์ž๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค.
    ๊ทธ ์ˆซ์ž๋ฅผ ๋’ค์ง‘์–ด์„œ returnํ•ด์ฃผ์„ธ์š”.

    x: ์ˆซ์ž
    return: ๋’ค์ง‘์–ด์ง„ ์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜!

    ์˜ˆ๋“ค ๋“ค์–ด,
    x: 1234
    return: 4321
    x: -1234
    return: -4321
    x: 1230
    return: 321

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

def reverse(number):

    if number > 0 :
        a = str(number)[::-1]
        return int(a)

    elif number < 0 :
        a = str(number)[1:]
        b = a[::-1]
        return int(b) * -1

    else :
        return 0  
  • input๋œ number ๊ฐ’์„ ๋’ค์ง‘๋Š”๊ฑด ์Šฌ๋ผ์ด์‹ฑ์„ ์‚ฌ์šฉํ•˜์˜€๋‹ค. ๋‹ค๋งŒ number๊ฐ€ ์–‘์ˆ˜์ผ ๊ฒฝ์šฐ์—๋Š” index=0 ์š”์†Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๋’ค์ง‘์€ ํ›„ intํ˜•์œผ๋กœ ๋ฐ”๋กœ returnํ•˜์˜€๊ณ , ์Œ์ˆ˜์ผ ๊ฒฝ์šฐ์—๋Š” index=1 ์š”์†Œ์˜ ์š”์†Œ๋ถ€ํ„ฐ ๋’ค์ง‘์—ˆ๋‹ค. index=0 ์ธ ์š”์†Œ๋ถ€ํ„ฐ ๋’ค์ง‘์„ ๊ฒฝ์šฐ '-' ๋ถ€ํ˜ธ๋„ ๋’ค๋กœ ๊ฐ€๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋งˆ์ง€๋ง‰์— -1์„ ๊ณฑํ•ด์ฃผ๋ฉด ๋‹ค์‹œ ์•ž์— '-'๋ถ€ํ˜ธ๊ฐ€ ์ƒ๊ธด๋‹ค.


    ๐Ÿ“ ๋ฌธ์ œ3

    String ํ˜•์ธ str ์ธ์ž์—์„œ ์ค‘๋ณต๋˜์ง€ ์•Š์€ ์•ŒํŒŒ๋ฒณ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์ œ์ผ ๊ธด ๋‹จ์–ด์˜ ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ์„ธ์š”.

    ex)
    str: ํ…์ŠคํŠธ
    return: ์ค‘๋ณต๋˜์ง€ ์•Š์€ ์•ŒํŒŒ๋ฒณ ๊ธธ์ด (์ˆซ์ž ๋ฐ˜ํ™˜)

    str = "abcabcabc"
    return ์€ 3
    => 'abc' ๊ฐ€ ์ œ์ผ ๊ธธ๊ธฐ ๋•Œ๋ฌธ

    str = "aaaaa"
    return ์€ 1
    => 'a' ๊ฐ€ ์ œ์ผ ๊ธธ๊ธฐ ๋•Œ๋ฌธ

    str = "sttrg"
    return ์€ 3
    => 'trg' ๊ฐ€ ์ œ์ผ ๊ธธ๊ธฐ ๋•Œ๋ฌธ

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

  def get_len_of_str(s):
      results = ''
      s_len = 0
      for i in range(len(s)): 
          if s[i] in results:
              results = s[i]
          elif s[i] not in results:
              results += s[i]
          if len(results) > s_len:
              s_len = len(results)

      return s_len
  • ์ด ๋ฌธ์ œ์˜ ์ฒซ๋ฒˆ์งธ key point๋Š” input ๊ฐ’ s๋ฅผ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ์š”์†Œ i๋ฅผ ๋น„์–ด์žˆ๋Š” ๋ฌธ์ž์—ด ๋ณ€์ˆ˜ results ๊ฐ’์— ๋„ฃ์–ด์ฃผ๋˜, ๋™์ผํ•œ i ๊ฐ’์ด ์ด๋ฏธ results์— ์žˆ์œผ๋ฉด results ์ดˆ๊ธฐํ™”(results = s[i])ํ•˜๊ณ  ์—†์œผ๋ฉด ์š”์†Œ ์ถ”๊ฐ€(results += s[i]) ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

  • ๋‘๋ฒˆ์งธ key point๋Š” results์˜ ๊ธธ์ด๊ฐ€ s_len ๊ฐ’๋ณด๋‹ค ํฌ๋ฉด s_len์— results์˜ ๊ธธ์ด๋ฅผ ํ• ๋‹นํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋Š” ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ์ œ์ผ ๊ธด ๋‹จ์–ด์˜ ๊ธธ์ด๋ฅผ s_len ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ์•Œ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค. ์‰ฝ๊ฒŒ ๋งํ•ด์„œ results์˜ ๊ธธ์ด๊ฐ€ ์ตœ๊ณ  ๊ธฐ๋ก์„ ๊ฒฝ์‹ ํ–ˆ์„ ๋•Œ s_len ๊ฐ’์ด ๋ณ€ํ•˜๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.


    ๐Ÿ“ ๋ฌธ์ œ4

    ์ˆซ์ž์ธ num์„ ์ธ์ž๋กœ ๋„˜๊ฒจ์ฃผ๋ฉด, ๋’ค์ง‘์€ ๋ชจ์–‘์ด num๊ณผ ๋˜‘๊ฐ™์€์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ์„ธ์š”.

    num: ์ˆซ์ž
    return: true or false (๋’ค์ง‘์€ ๋ชจ์–‘์ด num์™€ ๋˜‘๊ฐ™์€์ง€ ์—ฌ๋ถ€)

    num = 123
    return false
    => ๋’ค์ง‘์€ ๋ชจ์–‘์ด 321 ์ด๊ธฐ ๋•Œ๋ฌธ
    num = 1221
    return true
    => ๋’ค์ง‘์€ ๋ชจ์–‘์ด 1221 ์ด๊ธฐ ๋•Œ๋ฌธ
    num = -121
    return false
    => ๋’ค์ง‘์€ ๋ชจ์–‘์ด 121- ์ด๊ธฐ ๋•Œ๋ฌธ
    num = 10
    return false
    => ๋’ค์ง‘์€ ๋ชจ์–‘์ด 01 ์ด๊ธฐ ๋•Œ๋ฌธ

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

  def same_reverse(num):
      if num < 0:
          return False
      num2 = list(str(num))
      num2.reverse()
      num2 = "".join(num2)
      if num == int(num2):
          return True
      else:
          return False
  • num๊ฐ’์œผ๋กœ ์Œ์ˆ˜๊ฐ’์ด input ๋˜๋ฉด ๋ฌด์กฐ๊ฑด False์ด๊ธฐ ๋•Œ๋ฌธ์— if๋ฌธ์œผ๋กœ ์กฐ๊ฑด์„ ๊ฑธ์–ด๋‘๊ณ  ์‹œ์ž‘ํ•˜์˜€๋‹ค. num๊ฐ’์œผ๋กœ intํƒ€์ž…์ด input๋˜๊ธฐ ๋•Œ๋ฌธ์— strint ํƒ€์ž…์˜ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•œ ํ›„ reverse()ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์š”์†Œ๋ฅผ ๋’ค์ง‘๊ณ  join()ํ•จ์ˆ˜๋กœ ๋‹ค์‹œ ํ•˜๋‚˜์˜ ์š”์†Œ๋กœ ํ•ฉ์ณ์ฃผ์—ˆ๋‹ค.


    ๐Ÿ“ ๋ฌธ์ œ5

    strs์€ ๋‹จ์–ด๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
    ๊ณตํ†ต๋œ ์‹œ์ž‘ ๋‹จ์–ด(prefix)๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ์„ธ์š”.

    ์˜ˆ๋ฅผ ๋“ค์–ด,
    strs = ['start', 'stair', 'step']
    return์€ 'st'

    strs = ['start', 'wework', 'today']
    return์€ ''

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

#1์ฐจ๋‹ต์•ˆ(์˜ค๋‹ต)
def get_prefix(strs):
    if not strs:
        return ''
    short_word = min(strs, key=len)
    answer = ''
    for i, j in enumerate(short_word):
        for word in strs:
            if short_word == word:
                return short_word
            elif word[i] != j:
                answer += short_word[:i]
                return answer
  • ์ด๋ฒˆ์ฃผ์˜ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ ๋‹ต๊ฒŒ ์ด๋ฒˆ์ฃผ ๋ฌธ์ œ์ค‘ ๊ฐ€์žฅ ๊ธด ์‹œ๊ฐ„ ๊ณ ๋ฏผ์„ ํ–ˆ๋˜ ๋ฌธ์ œ์ด๋‹ค. input๋˜๋Š” strs๊ฐ’๋“ค ์ค‘ ๊ฐ€์žฅ ์งง์€ ๋‹จ์–ด๋ฅผ min()ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ short_word ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๊ณ  ์‹œ์ž‘ํ•˜์˜€๋‹ค.
  • short_word๋ฅผ enumerate ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ index๊ฐ’๊ณผ ์š”์†Œ ๊ฐ’์„ ๊บผ๋‚ด๊ณ  ๋‹ค๋ฅธ strs ์š”์†Œ๋“ค๊ณผ ๋น„๊ตํ•˜์—ฌ ๋นˆ ๋ฌธ์ž์—ด ๋ณ€์ˆ˜ answer์— ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€๊ณ ์ž ํ•˜์˜€๋‹ค.
  • Input๊ฐ’์ด strs = ['a', 'b', 'c] ์ธ ๊ฒฝ์šฐ์— answer = '' ์ด ๋‚˜์™€์•ผ ํ•˜๋Š”๋ฐ 'a' ๊ฐ€ return ๋˜์–ด ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋ฅผ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•˜์˜€๋‹ค.
    ์ฒซ๋ฒˆ์งธ word ๊ฐ’์ด short_word ๊ฐ’๊ณผ ๊ฐ™์€ 'a' ๊ฐ’์ด ํ˜ธ์ถœ๋˜์–ด ์ฒซ๋ฒˆ์งธ if ๋ฌธ์ด ์ž‘๋™๋˜๊ณ  ๋ฐ”๋กœ ๋‹ต์œผ๋กœ ๋ฆฌํ„ด ๋˜๋Š” ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฒผ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
#2์ฐจ๋‹ต์•ˆ(์ •๋‹ต)
  def get_prefix(strs):
      if not strs:
          return ''
      short_word = min(strs, key=len)
      answer = ''
      for i, j in enumerate(short_word):
          for word in strs:
              if word[i] != j:
                  return answer
          answer += j
      return answer
  • ์œ„์˜ ์˜ค๋ฅ˜๋ฅผ ์ˆ˜์ •ํ•˜๊ณ ์ž ์˜ค๋‹ต ์ฝ”๋“œ์™€ ์ „์ฒด์ ์ธ ๋กœ์ง์€ ๋™์ผ ํ•˜๋‚˜ answer ๋ณ€์ˆ˜์— j ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๋Š” ์ฝ”๋“œ์™€ return ์ฝ”๋“œ์˜ ์œ„์น˜๋ฅผ ๋ณ€๊ฒฝํ•˜์˜€๋‹ค. ๊ทธ๋ฆฌ๊ณ  short_word ์™€ word์˜ ๊ฐ’์ด ๋™์ผํ•œ ์ผ€์ด์Šค๋ฅผ ๊ตณ์ด ๋„ฃ์–ด์ค„ ํ•„์š”๊ฐ€ ์—†๋‹ค๊ณ  ํŒ๋‹จ๋˜์–ด ๋นผ์ฃผ์—ˆ๋‹ค.
# ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด
def get_prefix(strs):
    if len(strs) == 0:
        return '' 
    res = ''
    strs = sorted(strs)
    for i in strs[0]:
        if strs[-1].startswith(res+i):
            res += i
        else:
            break
    return res
  • startswith() ํ•จ์ˆ˜๋Š” ๋ฌธ์ž์—ด์ด ํŠน์ •๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ์•Œ๋ ค์ฃผ๋ฉฐ,
    true๋‚˜ false ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋‘๋ฒˆ์งธ ์ธ์ž๋ฅผ ๋„ฃ์Œ์œผ๋กœ์จ ์ฐพ๊ธฐ์‹œ์ž‘ํ•  ์ง€์ ์„ ์ •ํ• ์ˆ˜์žˆ๋‹ค.
profile
Backend Developer - "Growth itself contains the germ of happiness"
post-custom-banner

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