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

15814 ๋‹จ์–ด pythonWeCodecodekataWeCode

์‹œ๊ฐ„์— ์ซ“๊ธฐ๊ณ , ๋ญ”๊ฐ€ ๊ผญ ์ •๋‹ต์„ ์ƒ๊ฐํ•ด ๋‚ด์•ผ๋œ๋‹ค๋Š”๊ฒŒ ์ข‹์€๊ฒŒ ์•„๋‹Œ๊ฑธ์•„๋Š”๋ฐ.. ๋‚˜๋Š” ๋งค๋ฒˆ ์ •๋‹ต์„ ๋ชป ๋งž์ถ”๋Š” ๋‚ด๊ฐ€ ๋ฐ”๋ณด๊ฐ™๋‹ค๋Š” ์ž๊ดด๊ฐ์ด ๋“ค๋”๋ผ ใ… ใ… 

-DAY4-

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

์˜ˆ๋ฅผ ๋“ค๋ฉด
nums = [1,1,1,2,2,3],k = 2
->return [1,2]
nums = [1],k = 1
->return [1]

์ ‘๊ทผ๋ฐฉ๋ฒ•

์ฒ˜์Œ์— ๋‚˜๋Š” ๊ฐ ์ˆ˜๋ฅผ countํ•ด์„œ ๊ฐ€์žฅ ๋†’์€ ์ˆœ์„œ์ธ ์• ๋“ค์„ k๋งŒํผ ๋ถˆ๋Ÿฌ์˜ฌ๊นŒ๋ผ๊ณ  ์ƒ๊ฐ์„ํ–ˆ๋‹ค.

def top_k(nums, k):
  dic = {}
  for i in nums:
    if i not in dic:
      dic[i] = 1
    else:
      dic[i] += 1

์—ฌ๊ธฐ์„œ ๋ง‰ํ˜€๋ฒ„๋ฆผ... ๊ฐ€์žฅ ๋†’์€ ์ˆœ์„œ๋ฅผ ๋ฝ‘์•„๋‚ด๋Š”๊ฑธ ์ฐพ๋‹ค๊ฐ€ most_common๊ณผ Counterํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค๋Š”๊ฑธ ๋ฏผ์ฒ ๋‹˜ํ•œํ…Œ ๋“ฃ๊ณ  ์‚ฌ์šฉํ–ˆ๋‹ค.

from collections import Counter

def top_k(nums, k):
  a = Counter(nums)
  b = a.most_common(k)
  
  c = []
  for i in range(k):
    c.append(b[i][0])
  return c

๋ช‡๊ฐ€์ง€ ์ƒˆ๋กœ์šด ๊ฐœ๋…์ด ๋‚˜์™€์„œ ํ•œ๋ฒˆ ์ •๋ฆฌํ•ด ๋ณด๊ฒ ๋‹ค

<collections ๋ชจ๋“ˆ - Counter>

์ปจํ…Œ์ด๋„ˆ์— ๋™์ผํ•œ ๊ฐ’์˜ ์ž๋ฃŒ๊ฐ€ ๋ช‡๊ฐœ์ธ์ง€ ํŒŒ์•…ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.
<์ž…๋ ฅ๊ฐ’>
1.๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ๊ฐœ์ˆ˜๋ฅผ collections.Counter()๋ฅผ ์ด์šฉํ•˜๋ฉด Dictionaryํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

import collections
lst = ['aa', 'cc', 'dd', 'aa', 'bb', 'ee']
print(collections.Counter(lst))
#๊ฒฐ๊ณผ
Counter({'aa': 2, 'cc': 1, 'dd': 1, 'bb': 1, 'ee': 1})

2.๋”•์…”๋„ˆ๋ฆฌ์˜ ๊ฒฝ์šฐ ๋™์ผํ•˜๊ฒŒ ๋”•์…”๋„ˆ๋ฆฌ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜๋˜๋ฉฐ value์˜ ๊ฐ’์ด ๋งŽ์€ ๊ฒƒ ๋ถ€ํ„ฐ ์ถœ๋ ฅํ•ด์ค€๋‹ค.

3."๊ฐ’=๊ฐœ์ˆ˜"ํ˜•ํƒœ๋„ ์ž…๋ ฅ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด, collections.Counter(a=2, b=3, c=2)๋Š”
['a', 'a', 'b', 'b', 'b', 'c', 'c']์™€ ๊ฐ™๋‹ค.
๊ฒฐ๊ณผ๋Š”{'b': 3, 'c': 2, 'a': 2}๋กœ ๋‚˜์˜จ๋‹ค.

4.๋ฌธ์ž์—ด(String)์€ {๋ฌธ์ž:๊ฐœ์ˆ˜}์˜ ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜๋œ๋‹ค.

<most_common๋ฉ”์„œ๋“œ>

๋ณดํ†ต์˜ ๊ฒฝ์šฐ, ์ฃผ์–ด์ง„ ๋‹จ์–ด์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ๋“ฑ์žฅํ•˜๋Š” ์•ŒํŒŒ๋ฒณ๊ณผ ๊ทธ ์•ŒํŒŒ๋ฒณ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•œ๋‹ค.

from collections import Counter

def find_max(word):
    counter = Counter(word)
    max_count = -1
    for letter in counter:
        if counter[letter] > max_count:
            max_count = counter[letter]
            max_letter = letter
    return max_letter, max_count

์•ŒํŒŒ๋ฒณ์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋งŽ์€ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•ด ๋ฆฌํ„ดํ•ด์ฃผ๋Š” ํ•จ์ˆ˜๋„ ์กด์žฌํ•˜๋Š”๋ฐ Counter์•ˆ์— ํฌํ•จ์ด ๋˜์–ด์žˆ๋‹ค(๊ฐœ์ด๋“๐Ÿ‘)

from collections import Counter

Counter('hello world').most_common()

#๊ฒฐ๊ณผ๊ฐ’
#[('l', 3), ('o', 2), ('h', 1), ('e', 1), (' ', 1), ('w', 1), ('r', 1), ('d', 1)]

most_common()์˜ ()๊ด„ํ˜ธ์— ์ˆซ์ž๋ฅผ ๋„ฃ์œผ๋ฉด ๊ทธ ์ˆซ์ž๋งŒํผ์˜ ๋ฐ์ดํ„ฐ๋งŒ ๋ฝ‘์•„ ๋‚ผ ์ˆ˜๋„ ์žˆ๋‹ค!

Solution

def top_k(nums, k):
    count = {}
    for n in nums:
        count[n] = count.get(n, 0) + 1
    bucket = [[] for _ in range(len(nums)+1)]
    for n, freq in count.items():
        bucket[freq].append(n)
    ret = []
    for n_list in bucket[::-1]:
        if n_list:
            ret.extend(n_list)
            if len(ret) == k:
                return ret

-DAY5-

<๋ฌธ์ œ>
์ธ์ž์ธ height๋Š” ์ˆซ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
๊ทธ๋ž˜ํ”„๋กœ ์ƒ๊ฐํ•œ๋‹ค๋ฉด y์ถ•์˜ ๊ฐ’์ด๊ณ , ๋†’์ด ๊ฐ’์„ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
์•„๋ž˜์˜ ๊ทธ๋ž˜ํ”„๋ผ๋ฉด height ๋ฐฐ์—ด์€ [1, 8, 6, 2, 5, 4, 8, 3, 7] ์ž…๋‹ˆ๋‹ค.

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

์ ‘๊ทผ๋ฐฉ๋ฒ•

๋ฌธ์ œ๋ฅผ ๋ณธ ์ˆœ๊ฐ„์˜ ๋‚˜ : ?????????
๋™๊ธฐ๋‹˜ ๊ณ ๋ง™์Šต๋‹ˆ๋‹ค..
์ง„์งœ.. ๋ฌธ์ œ์ดํ•ด๋ฅผ ๋ชปํ•ด์„œ ์ ‘๊ทผ์„ ๋ชปํ•˜๋Š” ํ•œ์ฃผ์˜€๋‹ค ใ… ใ…  ๋‚˜๋Š” ๋ฐ”๋ณด..ใ… ใ… ใ… 

def get_max_area(height):
  answer=[]
  for i in range(len(height)):
      for j in range(i+1,len(height)):
          square=(j-i)*min(height[i],height[j])
          answer.append(square)
  return max(answer)

๋จผ์ € ๋นˆ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค๊ณ  ๋†’์ด๋ฅผ ๊ตฌํ•˜๊ธฐ์œ„ํ•ด for loop๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค. ๋ฉด์ ์„ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ณ„๋กœ ๋‹ค ๋งŒ๋“ ๋’ค list์— ๋„ฃ๋Š” ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ–ˆ๋‹ค
์ด๋ฒˆ์—๋Š” ์ˆ˜์›”ํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ–ˆ๋Š”๋ฐ!! ๋ชจ๋ฒ”๋‹ต์•ˆ์„ ๋ณด๊ณ  ์ƒˆ๋กœ์šด ๊ฑธ ์ƒ๊ฐํ•ด ๋ณผ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.

Solution

def get_max_area(height):
	l = 0
	r = len(height) -1
	area = 0
	while l < r: 
		area = max(area, min(height[l],height[r]) * (r - l))
		if height[l] < height[r]: 
			l += 1
		else:
		    r -= 1
	return area

ํ•จ๊ป˜ ๋‹ต์•ˆ์„ ๋ณธ ๋™๊ธฐ๋ถ„๋“ค๊ณผ ์ด์•ผ๊ธฐํ•˜๋ฉฐ ๊นจ๋‹ฌ์€ ์ ์ด ์žˆ๋‹ค.
๋‹จ์ˆœํžˆ ์ •๋‹ต์„ ๋ถ€๋ฅด๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ์ข€๋” ์ปดํ“จํ„ฐ์  ์ž…์žฅ์—์„œ ๋ณผ๊ฒฝ์šฐ
for loops๋ฅผ ๋‘๋ฒˆ ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ์‹œ๊ฐ„์ด ์˜ค๋ž˜๊ฑธ๋ฆฌ๋Š” ๊ตฌ๋ฌธ์ด ๋œ๋‹ค๋Š” ์ ์ด๋‹ค. ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ž€๊ฒŒ ์ด๋Ÿฐ๊ฑฐ๊ตฌ๋‚˜~ ๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ค๋˜ ์‹œ๊ฐ„์ด์˜€๋‹ค.

๋‹ค์Œ์ฃผ๋„ ํ™”์ดํŒ…!!๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ

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