[๐Ÿค”Code Kata]1week-2

22412 ๋‹จ์–ด pythonWeCodecodekataWeCode

-DAY3-

1์ฃผ์ฐจ ์ตœ๊ณ ๋‚œ์ด๋„...๋”ฐํ‘๐Ÿ˜ญ

<๋ฌธ์ œ>
String ํ˜•์ธ str ์ธ์ž์—์„œ ์ค‘๋ณต๋˜์ง€ ์•Š์€ ์•ŒํŒŒ๋ฒณ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์ œ์ผ ๊ธด ๋‹จ์–ด์˜ ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ์„ธ์š”.
str: ํ…์ŠคํŠธ
return: ์ค‘๋ณต๋˜์ง€ ์•Š์€ ์•ŒํŒŒ๋ฒณ ๊ธธ์ด (์ˆซ์ž ๋ฐ˜ํ™˜)

์˜ˆ๋ฅผ ๋“ค์–ด

  • str = "abcabcabc"(return์€ 3)ใ€€=> 'abc' ๊ฐ€ ์ œ์ผ ๊ธธ๊ธฐ ๋•Œ๋ฌธ
  • str = "aaaaa"(return์€ 1)ใ€€=> 'a' ๊ฐ€ ์ œ์ผ ๊ธธ๊ธฐ ๋•Œ๋ฌธ
  • str = "sttrg"(return์€ 3)ใ€€=> 'trg' ๊ฐ€ ์ œ์ผ ๊ธธ๊ธฐ ๋•Œ๋ฌธ

์ ‘๊ทผ๋ฐฉ๋ฒ•

์ ‘๊ทผ์ด๊ณ ๋ญ๊ณ  ์ฝ”๋“œ์นดํƒ€ 1์‹œ๊ฐ„์ค‘ ๋ฌธ์ œ๋ฅผ ์ดํ•ดํ•˜๋Š”๋ฐ 40๋ถ„์„ ์ผ๋‹ค ๐Ÿ˜ญ๐Ÿ˜ญ ๋‚˜๋Š” ๋ฐ”๋ณด์ธ๊ฐ€...
๋ฌธ์ œ๋ฅผ ์ดํ•ดํ•˜๊ณ  ๋จผ์ € ๋“  ์ƒ๊ฐ์€ ์ค‘๋ณต๋˜๋Š” ๋‹จ์–ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‹จ์–ด๋“ค์„ ์Šฌ๋ผ์ด์‹ฑํ•ด์•ผ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด์˜€๋‹ค.
๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค๊ณ , ์Šฌ๋ผ์ด์‹ฑ์„ ํ•œ๋’ค ๊ฐ ๋‹จ์–ด์ค‘์—์„œ ๊ฐ€์žฅ ๊ธด๊ฑธ ๊ณ ๋ฅด๋ฉด ๋˜์ง€ ์•Š์„๊นŒ? ๋ผ๊ณ  ์ ‘๊ทผํ•ด ๋ณด์•˜๋‹ค

def get_len_of_str(s):
  a = []
  for i in s:

์ฒ˜์ฐธํ•˜๊ฒŒ ๋‚จ๊ฒจ์ง„ ๋‚ด์ฝ”๋“œ...
์Šฌ๋ผ์ด์‹ฑํ•œ ๋‹จ์–ด๋ฅผ ๊ฐ๊ฐ ๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ ์ถ”์ถœํ•˜๋Š”๋ฒ•์€ ์•Œ์ง€๋งŒ.. ์ด๊ฑธ ๋”ฐ๋กœ ๋”ฐ๋กœ ๋ฆฌ์ŠคํŠธ์— ๋„ฃ์„์ค„ ๋ชฐ๋ผ ํ•œ์ฐธ์„ ํ—ค๋งค๋‹ค ์ฝ”๋“œ์นดํƒ€๊ฐ€ ๋์ด๋‚ฌ๋‹ค...ใ… ใ… 

๋‹ค๋ฅธ ๋™๊ธฐ๋ถ„์˜ ํ•ด๊ฒฐ๋ฐฉ๋ฒ•

def get_len_of_str(s):
    word = ''  
    word_list = [] 
    if s == '':
        return 0
    for i in s:
        if i not in word:
            word += i
            if i == s[-1]:
                print(word)
                word_list.append(word)
        else:
            word_list.append(word)
    answer = len((max(word_list, key=len)))
  
    return answer

๋น„์Šทํ•œ ๋…ผ๋ฆฌ๋กœ ์ ‘๊ทผํ•˜์‹  ๋™๊ธฐ๋ถ„์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๊ณ  ๋จธ๋ฆฌ๋ฅผ ํƒ์ณค๋Š”๋ฐ ใ… ใ… !
๋ฐ”๋กœ word = '' ๋ผ๋Š” ๊ฑฐ์˜€๋‹ค..
๋‚˜๋Š” ์ง€๊ธˆ๊นŒ์ง€ ๋นˆ ๋ฌธ์ž์—ด์ด๋ผ๋Š” ์ƒ๊ฐ์„ ๋‹จ ํ•œ๋ฒˆ๋„ ํ•ด๋ณธ์ ์ด ์—†์—ˆ๋Š”๋ฐ ํ•ด๋‹ต์ด ์—ฌ๊ธฐ์žˆ์–ด์„œ ใ… ใ…  ๋‹ต๋‹ตํ–ˆ๋˜ ์†์ด ๋ปฅ ๋šซ๋ฆฐ๊ธฐ๋ถ„์ด์˜€๋‹ค(๋ฉ‹์ง„ ์›ํฌ๋‹˜..๐Ÿ‘)

solution

def get_len_of_str(s):
    dct = {}
    max_so_far = curr_max = start = 0
    for index, i in enumerate(s):
    	if i in dct and dct[i] >= start:
            max_so_far = max(max_so_far, curr_max)
            curr_max = index - dct[i]
            start = dct[i] + 1
        else:
            curr_max += 1
        dct[i] = index
    return max(max_so_far, curr_max)

์•„์˜ˆ ๋ชจ๋ฅด๋Š” ๋งค์„œ๋“œ๊ฐ€ ๋‚˜์™€์„œ ์ฐพ์•„๋ณด์•˜๋‹ค

Enumerate๋ž€?
์ด ํ•จ์ˆ˜๋Š” ์ˆœ์„œ๊ฐ€ ์žˆ๋Š” ์ž๋ฃŒํ˜•(๋ฆฌ์ŠคํŠธ, ํŠœํ”Œ, ๋ฌธ์ž์—ด)์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•„ ์ธ๋ฑ์Šค ๊ฐ’์„ ํฌํ•จํ•˜๋Š” enumerate ๊ฐ์ฒด๋ฅผ ๋Œ๋ ค์ค€๋‹ค.

for i, name in enumerate(['body', 'foo', 'bar']):
...     print(i, name)
...
0 body
1 foo
2 bar

์œ„ ์˜ˆ์ œ์™€ ๊ฐ™์ด enumerate๋ฅผ for๋ฌธ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด ์ž๋ฃŒํ˜•์˜ ํ˜„์žฌ ์ˆœ์„œ(index)์™€ ๊ทธ ๊ฐ’์„ ์‰ฝ๊ฒŒ ์•Œ ์ˆ˜ ์žˆ๋‹ค. for๋ฌธ์ฒ˜๋Ÿผ ๋ฐ˜๋ณต๋˜๋Š” ๊ตฌ๊ฐ„์—์„œ ๊ฐ์ฒด๊ฐ€ ํ˜„์žฌ ์–ด๋Š ์œ„์น˜์— ์žˆ๋Š”์ง€ ์•Œ๋ ค ์ฃผ๋Š” ์ธ๋ฑ์Šค ๊ฐ’์ด ํ•„์š”ํ• ๋•Œ enumerate ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋งค์šฐ ์œ ์šฉ

max_so_far = curr_max = start = 0๋ผ๊ณ  ๋™์‹œ์— ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•ด์ค€๋’ค ๋”•์…”๋„ˆ๋ฆฌ์™€ enumerate๋ฅผ ์ด์šฉํ•ด ๊ฐ’์„ ๋‚ด๋Š” ๋ฐฉ๋ฒ•์ด๋‚˜์™”๋Š”๋ฐ
100ํผ์„ผํŠธ ๋”ฑ ์™€๋‹ฟ๋Š”๊ฑด ์•„๋‹ˆ๊ณ  ์ƒ๋‹นํžˆ ๋ณต์žกํ•œ๋ฐ?๋ผ๊ณ  ์ƒ๊ฐํ–ˆ์ง€๋งŒ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ƒ๊ฐํ•˜๋Š” ์‹œ๊ฐ„์ด๋ผ ๋„ˆ๋ฌด ๋ฉ‹์žˆ๊ฒŒ ์ฝ”๋“œ๋ฅผ ์งฐ๋‹ค๊ณ  ๊ฐํƒ„ํ•˜๊ธฐ ๋ฐ”๋นด๋‹ค..๐Ÿ‘๐Ÿ‘
๋‚˜๋„ ๊ผญ...!! ์–ธ์  ๊ฐ€๋Š”!!!๐Ÿ”ฅ

-DAY4-

์ˆซ์ž์ธ 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 ์ด๊ธฐ ๋•Œ๋ฌธ

์ ‘๊ทผ๋ฐฉ๋ฒ•

๋จผ์ € ์ƒ๊ฐ๋ณด๋‹ค ์ข€ ๊ฐ„๋‹จํ•œ๋ฐ? ๋ผ๊ณ  ์ƒ๊ฐ์„ ํ–ˆ๋‹ค(๊ฐใ…Ž..)
DAY2์—์„œ ๋งŒ๋‚œ ๋ฌธ์ œ๊ฐ€ ๋จธ๋ฆฟ์†์—์„œ ์Š›~โญ๏ธ๋– ์˜ฌ๋ž๊ธฐ ๋•Œ๋ฌธ! [::-1]๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด์˜€๋Š”๋ฐ ์ด๊ฑธ ์ด๋ฒˆ์—๋„ ์‚ฌ์šฉํ•ด๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค

def same_reverse(num):
  re = str(num)[::-1]
  
  if re == str(num):
    return True
  else:
    return False


์ „๋‚  ์ƒ๊ฐ๋ชปํ•˜๋Š” ์น ํ‘ผ์ด....

solution

def same_reverse(num):
	num =str(num)
	is_palindrome = True
	for i in range(len(num) // 2):
		if num[i] != num[-1 - i]:
			is_palindrome = False
			
	return is_palindrome

range๋ฅผ ์ด์šฉํ•˜๊ณ  ์ธ๋ฑ์Šค๋ฅผ ์•ž๊ณผ, ์ œ์ผ ๋งˆ์ง€๋ง‰์„ ๋น„๊ตํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ’€์ดํ–ˆ๋‹ค.
์ด๋Ÿฐ์ƒ๊ฐ์ด ์žˆ๋‹ค๋‹ˆ..?
์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ž€๊ฒŒ ์ง„์งœ ๊นŠ๊ฒŒ ์ƒ๊ฐํ•ด์•ผํ•˜๋Š”๊ตฌ๋‚˜ ์‹ถ๋‹ค..ใ…Ž..
์ฝ”๋”ฉ๋„์žฅ์—๋„ ๋™์ผํ•œ ๋ฌธ์ œ ํ’€์ด๋ฒ•์ด ์žˆ์œผ๋‹ˆ ๋‚˜์ค‘์— ๋‹ค์‹œ๋ณด๊ณ  ๊ผญ ์ฒด๋“ํ•˜์ž!

-DAY5-

๋‚ด๊ธฐ์ค€ ์ด๋ฒˆ์ฃผ ์ตœ์ตœ์ตœ์ตœ์ตœ๊ณ ๋‚œ๋„์˜€๋‹ค๐Ÿ˜ž๐Ÿ˜ž๐Ÿ˜ž

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

์˜ˆ๋ฅผ ๋“ค์–ด

  • strs = ['start', 'stair', 'step']
    return์€ 'st'
  • strs = ['start', 'wework', 'today']
    return์€ ''

์ ‘๊ทผ๋ฐฉ๋ฒ•

def get_prefx(strs):
  if strs ==[]:
    return โ€˜โ€™
  else:
    prefix = โ€˜โ€™
    strs = sorted(strs)
    for i in (strs[0]):
      if strs[-1].startswith(prefix+i):
        prefix += i
      else:
         break
    return prefix

์ฝ”๋“œ์นดํƒ€ ๊ฐ™์ดํ•œ ๋™๊ธฐ๋ถ„์ด ๊ฒ€์ƒ‰ํ•ด์„œ ์ฐพ์•„๋‚ธ๊ฒŒ startswith()๋ผ๋Š” ๋ฉ”์„œ๋“œ์˜€๋Š”๋ฐ
์•„์ง๋„ ์ดํ•ด๋ฅผ ์ž˜ ๋ชปํ•ด์„œ ์ •๋ฆฌํ•ด๋ณด์•˜๋‹ค

str(or tuple).startswith(์‹œ์ž‘ํ•˜๋Š”๋ฌธ์ž, ์‹œ์ž‘์ง€์ )
startswith๋Š” ๋ฌธ์ž์—ด์ด ํŠน์ •๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ์•Œ๋ ค์ฃผ๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค. ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜๋ฉฐ ๊ฒฐ๊ณผ๊ฐ’์€ True/False๋กœ ๋ฐ˜ํ™˜๋œ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ์ธ์ž๊ฐ’์—๋Š” tuple ๋ฐ–์— ๋“ค์–ด๊ฐ€์ง€ ์•Š๋Š”๋ฐ list๋‚˜ dict์„ ๋„ฃ๋Š”๋‹ค๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋‚œ๋‹ค.(tuple์„ ์‚ฌ์šฉํ•  ๋•Œ์—๋Š” string์—์„œ ์‹œ์ž‘ํ•˜๋Š” ๋ฌธ์ž์—ด์ด tuple ์žˆ์œผ๋ฉด true๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค. )

string = "hello startswith"
print(string.startswith("hello"))
# >>True
string = "hello startswith"
print(string.startswith("Hello"))
# >>False

ํŠน์ • ๋ฌธ์ž์—ด๋กœ ๋๋‚˜๋Š”์ง€ ์•Œ์•„๋ณด๋Š” endswith(๋๋‚˜๋Š”๋ฌธ์ž, ๋ฌธ์ž์—ด์˜์‹œ์ž‘, ๋ฌธ์ž์—ด์˜๋)๋งค์„œ๋“œ๋„ ์žˆ๋‹ค

๋‹ค๋ฅธ ๋™๊ธฐ๋ถ„์˜ ํ’€์ด

def get_prefix(strs):
  sorted_strs = sorted(strs)
  if strs == []:
    return ''
  if sorted_strs[0][0] != sorted_strs[-1][0]:
    return ''
  if len(sorted_strs[0]) > len(sorted_strs[-1]):
    shortest = len(sorted_strs[-1])
  elif len(sorted_strs[0]) <= len(sorted_strs[-1]):
    shortest = len(sorted_strs[0])
  answer = []
  for i in range(shortest):
    if sorted_strs[0][i] == sorted_strs[-1][i]:
      answer.append(sorted_strs[0][i])
  print(answer)  
  return ''.join(answer)

์ด๊ฒƒ๋„ ๋„ˆ๋ฌด ์‹ ๊ธฐํ–ˆ๋‹ค!
sorted_strs[0][0]๋ผ๋Š”๊ฒƒ๋„ ์ฒ˜์Œ ๋ณด๊ณ !!

sorted๋กœ ๋จผ์ € ์•ŒํŒŒ๋ฒณ์ˆœ์œผ๋กœ ๋ถ„๋ฆฌํ•œ๋’ค if๋ฌธ์œผ๋กœ ์ฒซ๋ฒˆ์งธ ๋‹จ์–ด์™€ ์ œ์ผ ๋งˆ์ง€๋ง‰ ๋‹จ์–ด์˜ ์ฒซ๊ธ€์ž๊ฐ€ ๋™์ผํ•œ์ง€๋ถ€ํ„ฐ ๋”ฐ์ง€๋Š” ์‹์œผ๋กœ ๋”ฐ์ง€๊ณ  ๋“ค์–ด๊ฐ”๋‹ค.
if๋ฌธ์ด ๋งŽ๊ณ  ๊ธธ๋‹ค๊ณ  ํ•˜์…จ์ง€๋งŒ ๋‚ด๊ฐ€ ๋ณด๊ธฐ์—๋Š” ์ •๋ง ์ž˜ ์งœ์‹ ๊ฑฐ ๊ฐ™์•„์„œ!! ๊ณต๋ถ€ํ•˜๊ฒŒ ๋‹ฌ๋ผ๊ณ  ํ–ˆ๋‹ค

solution

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

๊ฐ™์ด ์ฝ”๋“œ์นดํƒ€ํ•œ ๋™๊ธฐ๋ถ„์˜ ๋‹ต๊ณผ 100ํผ์„ผํŠธ ์ผ์น˜! (์™€์šฐ)
์ ‘๊ทผ๋ฐฉ๋ฒ•์€ ์ดํ•ด๋ฅผ ํ–ˆ๋Š”๋ฐ ์•„์ง ๋‚ด๊ฐ€ ์ฝ”๋“œ 100%๋งŒ๋“ค์–ด๋‚ด์ง€๋Š” ๋ชปํ•  ๊ฒƒ ๊ฐ™์•„์„œ ๋‹ค์Œ์ฃผ์— ๋˜ ๋‹ค์‹œ ๋ณต์Šตํ•˜๊ธฐ๋กœ!

ํ™”์ด๋ง~~~!!

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ