본문 바로가기

Programmers20

[프로그래머스] 리스트 생성 방식 def solution(answers): p = [[1, 2, 3, 4, 5], [2, 1, 2, 3, 2, 4, 2, 5], [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]] s = [0] * len(p) for q, a in enumerate(answers): for i, v in enumerate(p): if a == v[q % len(v)]: s[i] += 1 return [i + 1 for i, v in enumerate(s) if v == max(s)]​ def solution(answers): answer = [[0,1],[0,2],[0,3]] list_1 = [1, 2, 3, 4, 5] * (len(answers)//5 +1) list_2 = [2, 1, 2, 3, 2, 4, 2, .. 2024. 1. 29.
[프로그래머스] 덧칠하기 .기준의 중요성. 무엇을 기준으로 잡을 것인가. https://school.programmers.co.kr/learn/courses/30/lessons/161989 def solution(n, m, section): answer = 1 standard = section[0] for i in range(1,len(section)): if section[i] - standard < m: continue else: answer += 1 standard = section[i] return answer 기준을 무엇으로 잡을 것인가. 처음에, section[i] - section[i-1] 로 풀어나가면서 기준을 제대로 잡지 못하니 계속 헤맸다. 기준점을 잡고 현재 숫자에서 기준이 되는 숫자를 빼고, 그것이 롤러의 길이보다 크거나 같을 경우에만 answer에 + .. 2024. 1. 29.
10진수를 2진수로 바꾸기 bin() 사용. rjust 문자열 정렬 사용하기. [1차] 비밀지도 https://school.programmers.co.kr/learn/courses/30/lessons/17681/solution_groups?language=python3 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr def solution(n, arr1, arr2): answer = [] for i,j in zip(arr1, arr2): a, b = bin(i), bin(j) if len(a[2:]) < n or len(b[2:]) < n: a =(n - len(a[2:])) * '0' + a[2:] b =(n - len(b[2:]).. 2024. 1. 24.
딕셔너리로 알파벳 생성하기, key와 value 위치 바꾸기. 모든 key, value값 가져오기 프로그래머스 lv1 숫자 문자열과 영단어 파이썬 알파벳 딕셔너리로 생성하기. from string import ascii_lowercase alphabet_dict = {} for idx, i in enumerate(ascii_lowercase): alphabet_dict[i] = idx + 1 print(alphabet_dict) -> {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10, 'k': 11, 'l': 12, 'm': 13, 'n': 14, 'o': 15, 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, .. 2024. 1. 23.
[프로그래머스]유클리드 알고리즘으로 최대공약수 구하기. feat 재귀 gcd 함수는 유클리드 알고리즘을 사용하여 최대공약수를 계산합니다. 람다 함수를 이용하여 정의되어 있으며, a와 b의 최대공약수를 재귀적으로 계산합니다. lcm 함수는 최소공배수를 계산하는 함수로, 입력된 두 수 a와 b의 최소공배수를 계산합니다. 여기서 lcm(a, b) * gcd(a, b) = a * b라는 관계를 이용합니다. def solution(n, m): # answer = [i for i in range(1,n+1) if n%i == 0 and m%i == 0] # return [max(answer), (n*m)//max(answer)] gcd = lambda n, m : m if not n%m else gcd(m, n%m) lcm = lambda n, m : n*m//gcd(n,m) ret.. 2024. 1. 22.
[프로그래머스]정수를 나선형으로 배치하기 feat 2차원 배열 선언하여 풀기 def solution(n): arr = [[0] * n for i in range(n)] #2차원 배열 선언하기 많이 나옴. #이거는 종이에 써서 암기. a = 0 b = 0 x, y = 0,0 moving = [[0,1],[1,0],[0,-1],[-1,0]] for i in range(1, n**2+1): arr[x][y] = i if (x+moving[b][0]) >= n or (y+moving[b][1]) >= n or (y+moving[b][1]) < 0 or (x+moving[b][0]) < 0 or arr[x+moving[b][0]][y+moving[b][1]] != 0: b = (b + 1)%(len(moving)) x += moving[b][0] y += moving[b][1] retur.. 2024. 1. 22.