본문 바로가기

> 알고리즘 문제 풀이/프로그래머스27

1v1- 최대공약수와 최소공배수 (python, 파이썬) - 유클리드 알고리즘 체크! """ 1번: 나의 풀이 """ def gcd(a, b): gcm = 1 for k in range(2, min(a, b) + 1): while a % k == 0 and b % k == 0: a //= k b //= k gcm *= k return gcm def lcm(a, b): return a*b//gcd(a, b) def solution(n, m): g = gcd(n, m) return [g, lcm(n, m, g)] """ 2번: 다른 사람의 풀이 """ def gcdlcm(a, b): c, d = max(a, b), min(a, b) t = 1 while t > 0: t = c % d c, d = d, t return [c, a*b//c] def solution(n.. 2020. 10. 24.
lv1-[1차] 비밀지도 (python, 파이썬) """ 1번: 나의 풀이 """ def solution(n, arr1, arr2): secret_map = list(map(lambda x: format(x[0]|x[1], 'b'), zip(arr1, arr2))) answer = [] for s in secret_map: tmp = '' while len(s) < n: s = '0'+s for b in s: if b == '1': tmp += '#' else: tmp += ' ' answer.append(tmp) return answer """ 2번: 다른 사람의 풀이(rjust, replace 활용) """ def solution2(n, arr1, arr2): answer = [] for i in range(n): a = str(bin(arr1[i].. 2020. 10. 24.
lv1-콜라츠 추측 (python, 파이썬) """ 나의 풀이 """ def solution(num): i = 0 while i < 500: if num == 1: return i if num % 2: num = num*3 + 1 else: num //= 2 i += 1 return -1 2020. 10. 22.
lv1-시저 암호 (python, 파이썬) """ 나의 풀이 """ def solution(s, n): uppers = [ s for s in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'] lowers = [ s for s in 'abcdefghijklmnopqrstuvwxyz'] ans = '' for x in s: if x == ' ': ans += ' ' else: if x.isupper(): ans += uppers[(uppers.index(x)+n)%26] else: ans += lowers[(lowers.index(x)+n)%26] return ans ​ 2020. 10. 22.