목록algorithm/Python (36)
on my way

https://school.programmers.co.kr/learn/courses/30/lessons/70129# 1트def solution(s): cnt0, cntR = 0, 0 while s != "1": cntR += 1 zero = len([1 for d in s if d == "0"]) cnt0 += zero s = format(len(s) - zero, 'b') return cntR, cnt0# 리팩토링def solution(s): cnt0, cntR = 0, 0 while s != "1": cntR += 1 cnt0 += s.count("0") s = bin(len(s.rep..

[프로그래머스 코딩테스트 연습] 3진법 뒤집기 (Python3) def solution(n): answer = '' while n>0: n, mod = divmod(n, 3) answer += str(mod) return int(answer,3) 10진법 -> n진법 변화 방법을 알면 쉽게 풀 문젠데 (난 몰랐다 ㅎ)n이 0될때까지 divmod 함수를 사용해 3씩 나누어 mod를 구한다.각각 더해진 mod들을 원래 3진법 방식으로는 [::-1]로 뒤집어야하지만 이 문제에서는 어차피 뒤집으므로,바로 int( , 3) 로 3진법에서 10진법으로 변환하여 출력

https://school.programmers.co.kr/learn/courses/30/lessons/12930# def solution(s): answer = "" for word in s.split(' '): for i, w in enumerate(word): answer += w.lower() if i%2 else w.upper() answer += " " return answer[:-1] 반례input: "a a "output: "A A " 반례때문에 그냥 split() 했다가 좀 헤맸다. def solution(s): return ' '.join( ''.join(w.lowe..

https://www.acmicpc.net/problem/5622 # 리팩토링 전import sysfrom collections import defaultdictdial = ["ABC","DEF","GHI","JKL","MNO","PQRS","TUV","WXYZ"]tel = defaultdict(int)num = sys.stdin.readline().strip()count = 0for n in num: if not tel[n]: for i, d in enumerate(dial): if n in d: tel[n] = i+3 break count += tel[n]print(count)# 리팩토링 후import s..

def solution(n):# return [int(i) for i in list(str(n))[::-1]] return list(map(int,str(n)))[::-1]주석은 n년 전의 내가 푼 것속도, 메모리 측면에서 2번이 낫다.

https://www.acmicpc.net/problem/1253# 정답이긴 하나 리팩토링 전 코드import sysN = int(sys.stdin.readline())arr = sorted(map(int, sys.stdin.readline().split()))good = [False] * Nfor idx, a in enumerate(arr): start, end = 1 if idx == 0 else 0, N-2 if idx == N-1 else N-1 while start # 리팩토링import sysN = int(sys.stdin.readline())arr = sorted(map(int, sys.stdin.readline().split()))count = 0for idx, target i..