๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Programming/Coding Test

(11)
[Level 2] ์กฐ์ด์Šคํ‹ฑ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์กฐ์ด์Šคํ‹ฑ ์กฐ์ด์Šคํ‹ฑ์œผ๋กœ ์•ŒํŒŒ๋ฒณ ์ด๋ฆ„์„ ์™„์„ฑํ•˜์„ธ์š”. ๋งจ ์ฒ˜์Œ์—” A๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ex) ์™„์„ฑํ•ด์•ผ ํ•˜๋Š” ์ด๋ฆ„์ด ์„ธ ๊ธ€์ž๋ฉด AAA, ๋„ค ๊ธ€์ž๋ฉด AAAA ์กฐ์ด์Šคํ‹ฑ์„ ๊ฐ ๋ฐฉํ–ฅ์œผ๋กœ ์›€์ง์ด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. โ–ฒ - ๋‹ค programmers.co.kr def cnt_up_down(chr): cnt_from_a = abs(ord('A')-ord(chr)) cnt_from_z = abs(ord('Z')-ord(chr)+1) return(min(cnt_from_a, cnt_from_z)) def solution(name): cnt = 0 for i in name: cnt += cnt_up_down(i) lst_name = list(name) len_name = len(lst_name) if ls..
[Level 2] ํฐ ์ˆ˜ ๋งŒ๋“ค๊ธฐ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ํฐ ์ˆ˜ ๋งŒ๋“ค๊ธฐ programmers.co.kr def solution(num, k): max_num = [] for i, n in enumerate(num): while len(max_num) > 0 and max_num[-1] 0: max_num.pop() k -= 1 if k == 0: max_num += list(num[i:]) break max_num.append(n) max_num = max_num[:-k] if k > 0 else max_num return(''.join(max_num)) โ–ท ์ด ๋ฌธ์ œ๋Š” ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•˜์—ฌ ํ‘ธ๋Š” ๋ฌธ์ œ๋กœ, ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์˜ ๋‘ ๋ฒˆ์งธ ์ˆซ์ž๋ถ€ํ„ฐ ์ด์ „์˜ ์ˆซ์ž์™€ ๋น„๊ต๋ฅผ ํ†ตํ•ด ๊ฐ€์žฅ ํฐ ์ˆซ์ž๋ฅผ ๋งŒ๋“ค์–ด ๋‚˜๊ฐ€์•ผ ํ•œ๋‹ค. โ–ท ์œ„ ์ฝ”๋“œ์˜..
[Level 2] ๋ฌธ์ž์—ด ์••์ถ• ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋ฌธ์ž์—ด ์••์ถ• ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์ „๋ฌธ๊ฐ€๊ฐ€ ๋˜๊ณ  ์‹ถ์€ ์–ดํ”ผ์น˜๋Š” ๋ฌธ์ž์—ด์„ ์••์ถ•ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๊ณต๋ถ€๋ฅผ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ตœ๊ทผ์— ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๊ฐ„๋‹จํ•œ ๋น„์†์‹ค ์••์ถ• ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๊ณต๋ถ€๋ฅผ ํ•˜๊ณ  ์žˆ๋Š”๋ฐ, ๋ฌธ์ž programmers.co.kr def solution(s): len_s = len(s) cand = [] if len_s < 3: return len_s for u in range(1, int(len_s/2)+1): splt_s = [s[u*i:u*(i+1)] for i in range(int(len_s/u)+1)] comp_s = [] prev = splt_s.pop(0) cnt = 1 while splt_s: cur = splt_s.pop(0) if cur == prev: cnt +..
[Level 2] ๊ตฌ๋ช…๋ณดํŠธ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๊ตฌ๋ช…๋ณดํŠธ ๋ฌด์ธ๋„์— ๊ฐ‡ํžŒ ์‚ฌ๋žŒ๋“ค์„ ๊ตฌ๋ช…๋ณดํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌ์ถœํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ตฌ๋ช…๋ณดํŠธ๋Š” ์ž‘์•„์„œ ํ•œ ๋ฒˆ์— ์ตœ๋Œ€ 2๋ช…์”ฉ ๋ฐ–์— ํƒˆ ์ˆ˜ ์—†๊ณ , ๋ฌด๊ฒŒ ์ œํ•œ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์‚ฌ๋žŒ๋“ค์˜ ๋ชธ๋ฌด๊ฒŒ๊ฐ€ [70kg, 50kg, 80kg, 5 programmers.co.kr def solution(ppl, lim): ppl.sort() cnt = 0 while ppl: if len(ppl) == 1: cnt += 1 break cur_cnt = cnt for i in reversed(range(1, len(ppl))): if ppl[0]+ppl[i]
[Level 2] ์ „ํ™”๋ฒˆํ˜ธ ๋ชฉ๋ก (Python) ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ „ํ™”๋ฒˆํ˜ธ ๋ชฉ๋ก ์ „ํ™”๋ฒˆํ˜ธ๋ถ€์— ์ ํžŒ ์ „ํ™”๋ฒˆํ˜ธ ์ค‘, ํ•œ ๋ฒˆํ˜ธ๊ฐ€ ๋‹ค๋ฅธ ๋ฒˆํ˜ธ์˜ ์ ‘๋‘์–ด์ธ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค. ์ „ํ™”๋ฒˆํ˜ธ๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์„ ๊ฒฝ์šฐ, ๊ตฌ์กฐ๋Œ€ ์ „ํ™”๋ฒˆํ˜ธ๋Š” ์˜์„์ด์˜ ์ „ํ™”๋ฒˆํ˜ธ์˜ ์ ‘๋‘์‚ฌ์ž…๋‹ˆ๋‹ค. ๊ตฌ์กฐ๏ฟฝ๏ฟฝ programmers.co.kr def solution(p_b): for i in range(len(p_b)): for j in range(i+1, len(p_b)): if p_b[i] == p_b[j][:len(p_b[i])] or p_b[j] == p_b[i][:len(p_b[j])]: return False return True โ–ท ์ „ํ™”๋ฒˆํ˜ธ ๋ชฉ๋ก์— ์žˆ๋Š” ์ „ํ™”๋ฒˆํ˜ธ ๋‘ ๊ฐœ๋ฅผ ์„ ํƒํ•˜์—ฌ ๋น„๊ตํ•˜๋„๋ก for ๋ฌธ์„ ์ด์šฉํ•œ ์ด์ค‘๋ฃจํ”„๋ฅผ ๊ตฌํ˜„ํ•˜์˜€๋‹ค. โ–ท ์ฃผ์˜ํ•  ์ ์€ ์ „ํ™”๋ฒˆํ˜ธ์˜ ์ ‘๋‘์–ด๋ฅผ ๋น„๊ตํ•  ๋•Œ, ..
[Level 2] H-Index (Python) ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - H-Index H-Index๋Š” ๊ณผํ•™์ž์˜ ์ƒ์‚ฐ์„ฑ๊ณผ ์˜ํ–ฅ๋ ฅ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ง€ํ‘œ์ž…๋‹ˆ๋‹ค. ์–ด๋Š ๊ณผํ•™์ž์˜ H-Index๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ’์ธ h๋ฅผ ๊ตฌํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์œ„ํ‚ค๋ฐฑ๊ณผ1์— ๋”ฐ๋ฅด๋ฉด, H-Index๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ์–ด๋–ค ๊ณผํ•™์ž๊ฐ€ ๋ฐœํ‘œ programmers.co.kr def solution(c): candi_h = [] if not max(c): return 0 for h in range(1, max(c)+1): l_h = 0 u_h = 0 for i in range(len(c)): if c[i] >= h: u_h += 1 if c[i] = h) and (l_h
[Level 2] ์œ„์žฅ (Python) ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์œ„์žฅ programmers.co.kr def solution(clothes): clothes_num = {} for i in clothes: if i[1] in clothes_num: clothes_num[i[1]] += 1 else: clothes_num[i[1]] = 1 cnt = 1 for i in clothes_num.values(): cnt *= i+1 cnt -= 1 return cnt โ–ท ์ด ๋ฌธ์ œ๋Š” ํ‚ค์™€ ๊ฐ’์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ด์šฉํ•˜์—ฌ ํ’€ ์ˆ˜ ์žˆ๋‹ค. ์˜๋ฅ˜์˜ ์ข…๋ฅ˜๋ฅผ ํ‚ค๋กœ, ํ•ด๋‹น ์ข…๋ฅ˜์— ํฌํ•จ๋œ ์˜๋ฅ˜๋ฅผ ๊ฐ’์œผ๋กœ ๋‘”๋‹ค. ์ฒซ ๋ฒˆ์งธ for ๋ฌธ์€ ์ด๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ์ด๋‹ค. โ–ท ์ „์ฒด ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” ๊ฐ ํ•ด๋‹น ์˜๋ฅ˜์˜ ์ˆ˜์— 1์„ ๋”ํ•œ ํ›„, ๋ชจ๋‘ ๊ณฑํ•œ ๋’ค, 1์„ ๋นผ์ค€ ๊ฒƒ์„ ํ†ตํ•ด ๊ตฌํ•  ์ˆ˜ ์žˆ..
[Level 2] ๊ด„ํ˜ธ ๋ณ€ํ™˜ (Python) ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๊ด„ํ˜ธ ๋ณ€ํ™˜ ์นด์นด์˜ค์— ์‹ ์ž… ๊ฐœ๋ฐœ์ž๋กœ ์ž…์‚ฌํ•œ ์ฝ˜์€ ์„ ๋ฐฐ ๊ฐœ๋ฐœ์ž๋กœ๋ถ€ํ„ฐ ๊ฐœ๋ฐœ์—ญ๋Ÿ‰ ๊ฐ•ํ™”๋ฅผ ์œ„ํ•ด ๋‹ค๋ฅธ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ž‘์„ฑํ•œ ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ๋ฌธ์ œ์ ์„ ๋ฐœ๊ฒฌํ•˜๊ณ  ์ˆ˜์ •ํ•˜๋ผ๋Š” ์—…๋ฌด ๊ณผ์ œ๋ฅผ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. ์†Œ์Šค๋ฅผ ์ปด๏ฟฝ programmers.co.kr def is_correct(b): stack = [] for i in b: if i == '(': stack.append(i) elif stack: stack.pop() return not stack def detach(b): lst_b = list(b) cnt_l = 0 cnt_r = 0 for i in b: if i == '(': cnt_l += 1 else: cnt_r += 1 if cnt_l == cnt_r: break u = b[:cnt_l+cnt_r]..
[Level 2] ์‚ผ๊ฐ ๋‹ฌํŒฝ์ด (Python) ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์‚ผ๊ฐ ๋‹ฌํŒฝ์ด 5 [1,2,12,3,13,11,4,14,15,10,5,6,7,8,9] 6 [1,2,15,3,16,14,4,17,21,13,5,18,19,20,12,6,7,8,9,10,11] programmers.co.kr def solution(n): answer = [] mtx = [[0]*(i+1) for i in range(n)] i = -1 j = 0 num = 0 while True: for _ in range(n): num += 1 i += 1 mtx[i][j] = num n -= 1 if n == 0: break for _ in range(n): num += 1 j += 1 mtx[i][j] = num n -= 1 if n == 0: break for _ in range(n..
[Level 2] ์นดํŽซ (Python) ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์นดํŽซ Leo๋Š” ์นดํŽซ์„ ์‚ฌ๋Ÿฌ ๊ฐ”๋‹ค๊ฐ€ ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ค‘์•™์—๋Š” ๋…ธ๋ž€์ƒ‰์œผ๋กœ ์น ํ•ด์ ธ ์žˆ๊ณ  ํ…Œ๋‘๋ฆฌ 1์ค„์€ ๊ฐˆ์ƒ‰์œผ๋กœ ์น ํ•ด์ ธ ์žˆ๋Š” ๊ฒฉ์ž ๋ชจ์–‘ ์นดํŽซ์„ ๋ดค์Šต๋‹ˆ๋‹ค. Leo๋Š” ์ง‘์œผ๋กœ ๋Œ์•„์™€์„œ ์•„๊นŒ ๋ณธ ์นดํŽซ์˜ ๋…ธ๋ž€์ƒ‰๊ณผ ๏ฟฝ๏ฟฝ programmers.co.kr import math def solution(brown, yellow): total = brown + yellow m, n = [], [] for i in range(1, math.floor(math.sqrt(total)) + 1): if divmod(total, i)[1] == 0: n.append(i) m.append(int(total/i)) for i in range(len(n)): if 2*(m[i] + n[i]) - 4 == brown: ret..
[Level 2] ๋‹ค์Œ ํฐ ์ˆซ์ž (Python) ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋‹ค์Œ ํฐ ์ˆซ์ž ์ž์—ฐ์ˆ˜ n์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, n์˜ ๋‹ค์Œ ํฐ ์ˆซ์ž๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ ํ•ฉ๋‹ˆ๋‹ค. ์กฐ๊ฑด 1. n์˜ ๋‹ค์Œ ํฐ ์ˆซ์ž๋Š” n๋ณด๋‹ค ํฐ ์ž์—ฐ์ˆ˜ ์ž…๋‹ˆ๋‹ค. ์กฐ๊ฑด 2. n์˜ ๋‹ค์Œ ํฐ ์ˆซ์ž์™€ n์€ 2์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ–ˆ์„ ๋•Œ 1์˜ ๊ฐฏ์ˆ˜๊ฐ€ ๊ฐ™์Šต๋‹ˆ programmers.co.kr def solution(n): n_bin = bin(n)[2:] if len(set(n_bin)) == 1: return(int('10' + n_bin[1:], 2)) one_cnt = n_bin.count('1') while(True): n += 1 if bin(n)[2:].count('1') == one_cnt: return(n) โ–ท n์˜ ์ด์ง„์ˆ˜๊ฐ€ ๋ชจ๋‘ 1์ธ ๊ฒฝ์šฐ์™€ ์•„๋‹Œ ๊ฒฝ์šฐ๋กœ ๋‚˜๋ˆ„์–ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค. โ–ท n์˜ ์ด์ง„์ˆ˜๊ฐ€ ๋ชจ๋‘ 1์ธ..