프로그래밍/알고리즘
-
행렬의 덧셈프로그래밍/알고리즘 2021. 3. 28. 14:06
나의 풀이 def solution(arr1, arr2): answer = [] for i in range(len(arr1)): a1 = arr1.pop(0) a2 = arr2.pop(0) num = [x + y for x, y in zip(a1, a2)] answer.append(num) return answer 다른 사람 풀이 def sumMatrix(A,B): answer = [[c + d for c, d in zip(a, b)] for a, b in zip(A,B)] return answer 이중 for문 컴프리헨션으로 끝냈다.
-
-
소수찾기프로그래밍/알고리즘 2021. 3. 27. 16:14
풀이 def solution(n): a = set([i for i in range(3, n+1, 2)]) for i in range(3, n+1, 2): if i in a: a -= set([i for i in range(i*2, n+1, i)]) return len(a)+1 참고 블로그 - Link [Python] 프로그래머스 - 소수찾기 - 문제 설명 1부터 입력받은 숫자 n 사이에 있는 소수의 개수를 반환하는 함수, solution을 만들어 보세요. 소수는 1과 자기 자신으로만 나누어지는 수를 의미합니다. (1은 소수가 아닙니다.) - 제한 rain-bow.tistory.com
-
3진법 뒤집기프로그래밍/알고리즘 2021. 3. 27. 15:27
풀이 def n_ary(n, base): result = [] while n > 0: n, r = divmod(n, base) # 몫이 없어질 때까지 나누기 result.append(r) # 나머지를 result에 추가 return ''.join(map(str, reversed(result))) # join 함수를 사용해 result를 뒤집고 하나의 문자열로 바꾼다. def solution(n): b3 = n_ary(n, 3) # n_ary에 10진수와 진법을 보냄 b3 = b3[::-1] # 리턴된 문자열을 뒤집는다. return int(b3, 3) # int의 base를 사용해 10진수로 바꾼다. divmod는 몫과 나머지를 리턴해준다. int의 두번째인자인 base를 사용하면 진법을 쉽게 바꿀 수 ..
-
신규 아이디 추천프로그래밍/알고리즘 2021. 3. 27. 15:25
내가 한 풀이 def solution(new_id): delStr = '' new_id = new_id.lower() new_id = list(new_id) for char in new_id: if char.isalnum() or char in '-_.': delStr += char print(delStr) while '..' in delStr: delStr = delStr.replace('..', '.') print(delStr) if delStr.startswith('.'): delStr = delStr[1:] if delStr.endswith('.'): delStr = delStr[:-1] print(delStr) if len(delStr) == 0: delStr += 'a' print(delStr..