Python 81

[Algorithm] ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜, ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜

๐Ÿ™„ ์ตœ๋Œ€ ๊ณต์•ฝ์ˆ˜ GCD(Greatest Common Divisor)๐Ÿ™„ ์ตœ์†Œ ๊ณต๋ฐฐ์ˆ˜ LCM(Least Common Multiple) def gcd(a,b): #์ตœ๋Œ€๊ณต์•ฝ์ˆ˜ if b==0: return a return gcd(b,a%b)if __name__=='__main__' : A,B=map(int, input().split()) print(int(A*B/gcd(A,B))) #์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜

[Algorithm] ์†Œ์ˆ˜ ๊ตฌํ•˜๊ธฐ

๐Ÿ˜Š๋ฐฑ์ค€_์†Œ์ˆ˜ ๊ตฌํ•˜๊ธฐ) https://www.acmicpc.net/problem/1929๐Ÿ˜Šํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค_์†Œ์ˆ˜์ฐพ๊ธฐ)https://programmers.co.kr/learn/courses/30/lessons/42839 ์œ„์™€ ๊ฐ™์€ ๋ฌธ์ œ๋“ค์„ ํ’€๋ฉฐ ์†Œ์ˆ˜ ์ฐพ๊ธฐ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ ์›๋ž˜ ๋ฐฉ์‹์œผ๋กœ ํ’€๋ฉด์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒƒ์ด๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.  ๐Ÿ™„ ์ดˆ๊ธฐ ์ฝ”๋“œ)for i in range(M,N+1): #์†Œ์ˆ˜์ฐพ๊ธฐ if i==2: #2๋Š” ์†Œ์ˆ˜ arr.append(i) for j in range(2,i): if i%j==0: #๋‚˜๋ˆ ์ง€๋Š” ๊ฒƒ์ด ์žˆ์œผ๋ฉด ์†Œ์ˆ˜๊ฐ€ ์•„๋‹˜ break if j==i-1: #๋‚˜๋ˆ ์ง€๋Š” ๊ฒƒ์ด ์—†์œผ๋ฉด ์†Œ..

[Python] heapq

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ฉด์„œ ์ฒ˜์Œ์œผ๋กœ heapq๋ฅผ ์ ‘ํ–ˆ๋‹ค. ๐Ÿ˜‰ heapq ๋ž€? ์šฐ์„ ์ˆœ์œ„ ํ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ผ๊ณ ๋„ ํ•˜๋Š” ํž™ ํ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๊ตฌํ˜„์„ ์ œ๊ณตํ•œ๋‹ค.ํž™์€ ๋ชจ๋“  ๋ถ€๋ชจ ๋…ธ๋“œ๊ฐ€ ์ž์‹๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฐ’์„ ๊ฐ–๋Š” ์ด์ง„ํŠธ๋ฆฌ์ด๋‹ค. ๐Ÿ˜‰ heapq importimport heapq ๐Ÿ˜‰ heapq ํ•จ์ˆ˜heap=[] ์›์†Œ ์ถ”๊ฐ€#์›์†Œ ์ถ”๊ฐ€heapq.heappush(heap, ์›์†Œ ๊ฐ’)heapq.heappush(heap,(-์›์†Œ ๊ฐ’, ์›์†Œ ๊ฐ’)) #์ตœ๋Œ€ ํž™ ๊ตฌํ˜„์›์†Œ ์ œ๊ฑฐ#์›์†Œ ์ œ๊ฑฐheapq.heappop(heap) #heap์—์„œ ๊ฐ€์žฅ ์ž‘์€ ํ•ญ๋ชฉ ํŒ

[Python] lambda

ํŒŒ์ด์ฌ์œผ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ•ด๊ฒฐํ•  ๋•Œ ์ข…์ข… ๋ดค๋˜ ์นœ๊ตฌ์ด๋‹ค!๋ฌผ๋ก  ์‚ฌ์šฉํ•  ๋•Œ๋งˆ๋‹ค ๊ณ„์† ๊ฒ€์ƒ‰์„ ํ–ˆ์ง€๋งŒ...์ด์ œ๋ผ๋„ ์ •๋ฆฌํ•ด๋ณด๋ คํ•œ๋‹ค! ๐Ÿ˜‰ ๋žŒ๋‹ค (lambda) lambda ์ธ์ž: ํ‘œํ˜„์‹์ด๋Ÿฐ ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. ๋ฌผ๋ก  ์ด๋ ‡๊ฒŒ๋งŒ ์จ๋†“๋Š”๋‹ค๋ฉด ๋ชจ๋ฅด๋Š” ๊ฒŒ ๋‹น์—ฐํ•˜๋‹ค! ๋ช‡ ๊ฐ€์ง€ ์˜ˆ์‹œ๋„ ํ•จ๊ป˜ ์ ์–ด๋‘๊ฒ ๋‹ค!  numbers.sort(key=lambda x:x*4, reverse=True)  ๋‹ค์ค‘ ์กฐ๊ฑด# ๊ธฐ์ค€ ์•ž์— -๋ฅผ ๋ถ™์ด๋ฉด ๋‚ด๋ฆผ ์ฐจ์ˆœarr.sort(key=lambda x:(-int(x[1]), int(x[2]),-int(x[3]),x[0]))

[Python] list

Python3์˜ list์— ๊ด€ํ•ด ๋„์ ๋„์ โœ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1) list  โ‘  list ์„ ์–ธx=[] #list ์„ ์–ธ ๋ฆฌ์ŠคํŠธ ์„ ์–ธ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์•„์ฃผ ๊ฐ„๋‹จ!! x=[i for i in range(5)] #[0, 1, 2, 3, 4]  ๋ฐ˜๋ณต๋ฌธ ์‚ฌ์šฉํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ ์„ ์–ธํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.    โ‘ก list ์›์†Œ ์ถ”๊ฐ€x.append(5) #list ์›์†Œ ์ถ”๊ฐ€ append๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ์— ์›์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.  x.insert(1,"hi") #['a', 'hi', 'b', 'c', 'd'] insert(์›์†Œ ์ถ”๊ฐ€ํ•  index, ์ถ”๊ฐ€ํ•  ์›์†Œ ๊ฐ’)    โ‘ข list ์›์†Œ ์‚ญ์ œ  ๋ฆฌ์ŠคํŠธ ์›์†Œ๋ฅผ ์‚ญ์ œํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹น  ex) x=["a", "b", "c"..

01์›” 13์ผ ๋ชจ๊ฐ์ฝ”_์™€ํŒฌํ˜ธ 6ํšŒ์ฐจ ๊ฒฐ๊ณผ ๋ณด๊ณ ์„œ

2021๋…„ 01์›” 13์ผ ์ˆ˜์š”์ผ 20:00~23:00 ๐Ÿ”ฅ programmers 2๋‹จ๊ณ„ ๋ฌธ์ œ ํ•ด๊ฒฐ ๋ชฉ๋ก ๐Ÿ”ฅ ๋” ๋งต๊ฒŒ - ํž™(Heap) - ์ €๋ฒˆ์— ํšจ์œจ์„ฑ ์‹คํŒจ๋กœ ์ธํ•˜์—ฌ ๋‹ค์‹œ ๋„์ „ํ•œ ๊ฒฐ๊ณผ ํšจ์œจ์„ฑ ํ†ต๊ณผ๋ฅผ ํ•˜์ง€ ๋ชปํ•จ - ์ฐพ์•„๋ณธ ๊ฒฐ๊ณผ heapq๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๊ฒฐํ•˜๋ฉด ํšจ์œจ์„ฑ์„ ํ†ต๊ณผํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•จ ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ - ๋ฌธ์ œ์˜ ๋‚ด์šฉ์€ ์ดํ•ดํ–ˆ์œผ๋‚˜ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š”๋ฐ ํ˜ผ๋ž€์ด ์™€์„œ ํ•ด๊ฒฐ ์ค‘... - ๋‹ค์Œ์— ๊ผญ ์„ฑ๊ณตํ•˜๊ธฐ๋ฅผ.. ๋” ๋งต๊ฒŒ ๋ฌธ์ œ ํ’€์ด def solution(scoville, K): answer = 0 scoville.sort() #์ •๋ ฌ while scoville[0]

01์›” 11์ผ ๋ชจ๊ฐ์ฝ”_์™€ํŒฌํ˜ธ 5ํšŒ์ฐจ ๊ฒฐ๊ณผ ๋ณด๊ณ ์„œ

2021๋…„ 01์›” 11์ผ ์›”์š”์ผ 20:00~23:00 ๐Ÿ”ฅ programmers 2๋‹จ๊ณ„ ๋ฌธ์ œ ํ•ด๊ฒฐ ๋ชฉ๋ก ๐Ÿ”ฅ ์ด์ง„ ๋ณ€ํ™˜ ๋ฐ˜๋ณตํ•˜๊ธฐ - ์›”๊ฐ„ ์ฝ”๋“œ ์ฑŒ๋ฆฐ์ง€ ์‹œ์ฆŒ1 - ํŒŒ์ด์ฌ ๋‚ด์žฅ ํ•จ์ˆ˜ foramt์„ ์ด์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ•ด๊ฒฐ ๊ฐ€๋Šฅ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜ - ์ฒ˜์Œ์— ์žฌ๊ท€๋กœ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ -> ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ, ์‹œ๊ฐ„ ์ดˆ๊ณผ ๋ฐœ์ƒ - ์žฌ๊ท€๊ฐ€ ์•„๋‹Œ list๋กœ ๋‹ค์‹œ ๊ตฌํ˜„ํ•˜์—ฌ ํ†ต๊ณผ ( ์žฌ๊ท€ ์™ธ์— ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด ์ƒ๊ฐ๋‚˜์ง€ ์•Š์•„ ๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด ์ฐธ๊ณ  ) JadenCase ๋ฌธ์ž์—ด ๋งŒ๋“ค๊ธฐ - ์ด ๋ฌธ์ œ์—์„œ ๊ณ ๋ คํ•ด์•ผ ํ•  ์ : ๊ณต๋ฐฑ์ด ์—ฐ๋‹ฌ์•„ ์˜ฌ ์ˆ˜ ์žˆ์Œ N๊ฐœ์˜ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜ - programmers 1๋‹จ๊ณ„์˜ ' ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜์™€ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜'๋ฅผ ํ•ด๊ฒฐํ–ˆ์œผ๋ฉด ์ด๊ฒƒ์„ ์ด์šฉํ•˜์—ฌ ์‰ฝ๊ฒŒ ํ•ด๊ฒฐ ๊ฐ€๋Šฅ - list์˜ ๊ธธ์ด๊ฐ€ 0์ด ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜์—ฌ ๋‘ ์ˆ˜์˜ ์ตœ์†Œ ๊ณต๋ฐฐ์ˆ˜๋ฅผ ๊ตฌํ•จ ์ง์ง€์–ด ์ œ๊ฑฐํ•˜..

01์›” 6์ผ ๋ชจ๊ฐ์ฝ”_์™€ํŒฌํ˜ธ 4ํšŒ์ฐจ ๊ฒฐ๊ณผ ๋ณด๊ณ ์„œ

2021๋…„ 01์›” 6์ผ ์ˆ˜์š”์ผ 20:00~23:00 ๐Ÿ”ฅ programmers 2๋‹จ๊ณ„ ๋ฌธ์ œ ํ•ด๊ฒฐ ๋ชฉ๋ก ๐Ÿ”ฅ -> 1๋‹จ๊ณ„๋ฅผ ํ•ด๊ฒฐํ•  ๋•Œ ๋ณด๋‹ค ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ ธ๋‹ค. ์Šคํ‚ฌ ํŠธ๋ฆฌ - Summer/Winter Coding(~2018) - ์„ ํ–‰ ์Šคํ‚ฌ ์ˆœ์„œ skill๊ณผ ์ƒ๊ด€์—†๋Š” ๊ฒฝ์šฐ๋ฅผ ๊ณ ๋ คํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. ๋” ๋งต๊ฒŒ - ํž™(heap) - ์ •ํ™•์„ฑ์€ ๋‹ค ํ†ต๊ณผํ–ˆ์ง€๋งŒ ํšจ์œจ์„ฑ์€ ์‹คํŒจ (ํŒŒ์ด์ฌ ๋‚ด์žฅ ํ•จ์ˆ˜ sort๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์‹œ๊ฐ„ ์ดˆ๊ณผ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ) ์Šคํ‚ฌ ํŠธ๋ฆฌ ๋ฌธ์ œ ํ’€์ด def solution(skill, skill_trees): answer = 0 for i in skill_trees: count=0 temp=[] for j in range(len(i)): #๊ฐ’ ํ•˜๋‚˜์”ฉ ์ฝ์–ด์„œ for k in range(len(skill))..

01์›” 4์ผ ๋ชจ๊ฐ์ฝ”_์™€ํŒฌํ˜ธ 3ํšŒ์ฐจ ๊ฒฐ๊ณผ ๋ณด๊ณ ์„œ

2021๋…„ 01์›” 4์ผ ์›”์š”์ผ 20:00~23:00 ๐Ÿ”ฅ programmers 1๋‹จ๊ณ„ ๋ฌธ์ œ ํ•ด๊ฒฐ ๋ชฉ๋ก ๐Ÿ”ฅ [1์ฐจ] ๋‹คํŠธ ๊ฒŒ์ž„ - 2018 KAKAO BLIND RECRUITMENT - ์˜ˆ์ „์— ํ’€์–ด๋ณผ ๊ธฐํšŒ๊ฐ€ ์žˆ์–ด ํ•œ๋ฒˆ ํ•ด๋ดค์—ˆ๋˜ ๋ฌธ์ œ์˜€๋‹ค. ์˜ˆ์ „์—๋Š” ์–ด๋ ค์› ์—ˆ์ง€๋งŒ ์ง€๊ธˆ ๋‹ค์‹œ ํ’€์–ด๋ณด๋‹ˆ ์ƒ๊ฐ๋ณด๋‹ค ์–ด๋ ต์ง€ ์•Š์€ ๋ฌธ์ œ์˜€๋‹ค - ๋‹จ์ˆœ ๋ฐ˜๋ณต๋ฌธ๊ณผ ์กฐ๊ฑด๋ฌธ์œผ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋˜ ๋ฌธ์ œ ์ฒด์œก๋ณต - ํƒ์š•๋ฒ• - ๋‹จ์ˆœ ๋ฐ˜๋ณต๋ฌธ๊ณผ ์กฐ๊ฑด๋ฌธ์œผ๋กœ ๋ฌธ์ œ ํ•ด๊ฒฐ ๊ฐ€๋Šฅ - ์–‘ ๋์ธ ๊ฒฝ์šฐ์™€ ์ค‘๊ฐ„์ธ ๊ฒฝ์šฐ๋ฅผ ๋‚˜๋ˆ„์–ด ์ƒ๊ฐํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ•ด๊ฒฐ ๊ฐ€๋Šฅํ•œ ๋ฌธ์ œ์˜€๋‹ค ๋ชจ์˜๊ณ ์‚ฌ - ์™„์ „ ํƒ์ƒ‰ - 2ํšŒ์ฐจ๋•Œ ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ ๋ฐœ์ƒํ•œ ๊ฒƒ ํ•ด๊ฒฐ -> ์ •๋‹ต ํ™•์ธํ•˜๋Š” ๊ฒƒ์„ ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ ๋ฌธ์ž์—ด ๋‚ด ๋งˆ์Œ๋Œ€๋กœ ์ •๋ ฌํ•˜๊ธฐ - ๋ฌธ์ œ๋งŒ ์ฝ์—ˆ์„ ๋•Œ ๋ณต์žกํ•ด ๋ณด์˜€์ง€๋งŒ ์กฐ๊ฑด์„ ์ž˜ ๋‚˜๋ˆ„์–ด ๊ตฌํ˜„ํ•˜๋ฉด ํ•ด๊ฒฐ ๊ฐ€๋Šฅ -..

12์›”28์ผ ๋ชจ๊ฐ์ฝ”_์™€ํŒฌํ˜ธ 2ํšŒ์ฐจ ๊ฒฐ๊ณผ๋ณด๊ณ ์„œ

2020๋…„ 12์›” 28์ผ ์›”์š”์ผ 20:00~23:00 ๐Ÿ”ฅ programmers 1๋‹จ๊ณ„ ๋ฌธ์ œ ํ•ด๊ฒฐ ๋ชฉ๋ก ๐Ÿ”ฅ [1์ฐจ] ๋น„๋ฐ€์ง€๋„ - ์ด์ง„์ˆ˜ and ๊ณ„์‚ฐ์œผ๋กœ ํ•ด๊ฒฐ ์†Œ์ˆ˜ ์ฐพ๊ธฐ - ์ฒ˜์Œ์— ํ•˜๋‚˜ํ•˜๋‚˜ ๊ฒ€์‚ฌํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ์„๋•Œ ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋‹ค - ๊ฒ€์ƒ‰์„ ํ•ด ๋ณธ ํ›„ ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€๋‹ค - set์„ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€๋‹ค. ๋ชจ์˜๊ณ ์‚ฌ - ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๐Ÿค› [1์ฐจ] ๋น„๋ฐ€์ง€๋„ ๋ฌธ์ œ ํ’€์ด def solution(n, arr1, arr2): answer = [] for i in range(n): result="" temp=(bin(arr1[i] | arr2[i])) #์ด์ง„์ˆ˜ and if(len(temp) 0 ์ฑ„์šฐ๊ธฐ tempzero="" for j in range(n+2-len(temp)): te..