본문 바로가기

자료구조48

[알고리즘] 프로그래머스 | 둘만의 암호 둘만의 암호 답: 더보기 from string import ascii_lowercase alpha = list(ascii_lowercase) #아스키코드로 alphabet 배열 만들기 def solution(s, skip, index): answer = '' for sk in skip: alpha.remove(sk) for str_s in s: idx_s = alpha.index(str_s) answer += alpha[(idx_s + index) % len(alpha)] return answer 문제 분석 및 해석 주어지는 문자열을 규칙에 따라 변환하기 1. s에서 index만큼 뒤에 있는 알파벳을 찾아 길이 만큼 answer에 더해주기 2. 26개 넘으면 a로 돌아가서 더 세기 3. skip의 알파벳.. 2023. 8. 18.
[알고리즘] 프로그래머스 | 소수 만들기 소수만들기 답: 더보기 식 하나로 만들기 from itertools import combinations def solution(nums): result = 0 n_list = list((combinations(nums, 3))) for number in n_list: num = sum(number) new_num = int(num ** 0.5) + 1 for i in range(2, new_num): if num % i == 0: break else: result += 1 return result 함수로 나누기 from itertools import combinations def is_prime(n): new_num = int(n**0.5+1) for i in range(2, new_num): if n .. 2023. 8. 15.
[알고리즘] 프로그래머스 | 소수 찾기 Lv.1 소수 찾기 #수학 #에라토스테네스의 체 답: 더보기 이중 for 문을 피하기 위해 검증함수를 따로 빼내었다. def is_prime(n): new_n = int(n**0.5) + 1 for i in range(2, new_n): if n % i == 0: return False return True def solution(n): answer = 0 for i in range(2, n + 1): if is_prime(i) == True: answer += 1 return answer 다른 사람의 풀이 1) def solution(n): answer = 0 for i in range(2, n+1): if i == 2: answer += 1 continue for j in range(2, int(i ** (1.. 2023. 8. 12.
[알고리즘] 프로그래머스 | 삼총사, 키패드 누르기 삼총사 답: 더보기 from itertools import combinations def solution(number): answer = 0 combi = list(combinations(number, 3)) for i in combi: if sum(i) == 0: answer +=1 return answer 문제 분석 및 해석 요소별로 중복이 없는 조합을 구하고 ** 서로 다른 학생의 정수 번호가 같을 수 있습니다. 주의!! 학생 3명의 정수 번호를 더했을 때 0 더해서 0이되는지 확인 >>풀이생각 조합이구나! combinations써야지! 리뷰. 모듈 활용이외의 풀이도 도전해봐야겠다 counter 끼리 계산이 되는 것도 새로웠다 키패드 누르기 답: 더보기 1) 좌표를 지정해서 풀이 key = {1:(.. 2023. 8. 11.