목록프로그래머스 (18)
IT 세계의 후아

participant: 참가자 목록completion: 완주자 목록return: 완주하지 못한 참가자 한 명의 이름*중복된 이름이 있을 수 있음 # 내 첫 풀이def solution(participant, completion): names_dic = dict(zip(participant, [0]*len(participant))) for i in participant: names_dic[i] += 1 for j in completion: names_dic[j] -= 1 return [k for k,v in names_dic.items() if v!=0][0] # 다른 사람 풀이 참고-Hash 활용def solution(participant..

명함을 모두 담을 수 있는 w(가로)*h(세로)가 최소인 카드 크기 구하기명함 사이즈가 이중 리스트로 주어짐구하는 건 w*h 값 ※ 이중 리스트에서 최대/최소 구하기단순 max(리스트) 하면 리스트가 나옴a = [[10, 7], [12, 3], [8, 15], [14, 7], [5, 15]]print(max(a)) # [14, 7]print(min(a)) # [5, 15]print(max(map(max, a))) # 15print(min(map(min, a))) # 3 ≫ max(max(x) for x in 리스트) ≫ max(map(max, 리스트)) / min(map(min, 리스트))# 다른 사람 풀이def solution(sizes): return max(ma..
※ n진수 → 10진수int(n진수값, n)int('101', 2) # 5int('202', 3) # 20 ※ 10진수 → n진수 #프로그래머스 lv1 3진법 뒤집기#블로그 풀이 참고함def solution(n): n_3 = '' while n: n_3 += str(n%3) n //= 3 return int(n_3, 3) # 10진수 -> n진수answer = ''while num: answer += str(num % n) num //= nprint(num) # n진수 거꾸로 된 값print(num[::-1]) # n진수 변환값 cf) https://velog.io/@code_angler/%ED%8C%8C%EC%9D%B4%EC%8D%AC-%..
※ List Comprehension리스트 a, b[x for x in a if x in b] # a의 인자 중 b에도 있는 인자만 추출 #프로그래머스 lv1 최대공약수, 최소공배수#n, m 두 숫자를 받으면 [gcd, lcm] returndef divide(num): # num의 약수 리스트 return div_li = [] for i in range(1, int(num**(1/2))+1): if num%i == 0: div_li.append(i) if i != num//i: div_li.append(num//i) return div_lidef solution(n, m): answer = [] i..
※ 2차원 리스트(배열)접근할 때 배열 틀 제대로 갖추지 않고 무턱대고 값을 저장하면 index error 남!!#프로그래머스 lv1 행렬의 덧셈def solution(arr1, arr2): answer = [] for i in range(len(arr1)): answer.append([]) # arr1, arr2와 같이 2차원 배열로 틀 갖추기 for j in range(len(arr1[i])): answer[i].append(arr1[i][j] + arr2[i][j]) return answer # 다른 사람 풀이 return [list(map(sum, zip(*x))) for x in zip(A, B)] return ..
※ 문자열.isdigit()문자열이 '숫자'로만 구성돼있는지 확인하는 함수따로 chr, ord 함수를 활용하지 않아도 간단하게 풀 수 있음!!# 프로그래머스 lv1 문자열 다루기 기본def solution(s): a = [i for i in s if ord(i)>=48 and ord(i)
round(값): 정수로 반올림round(값, 자리수n): 소수점 n자리수에서 반올림 # 프로그래머스 lv0 두 수의 나눗셈# 테스트 케이스 실패...def solution(num1, num2): return round((num1/num2)*1000) # 수정 후 return int((num1/num2)*1000) ※ int와 roundround: 반올림int: 정수로의 변환 - 소수점 이하 버림 ※ format, f-string"문자열 {1:.2f} ".format(실수1, 실수2) => 실수2를 소수점 둘째자리까지 출력f"문자열 {변수:.3f}" => 변수의 값을 소수점 셋째자리까지 출력 cf) https://blockdmask.tistory.com/534..
set 활용하기!!! a_li = [a, b, c, d]b_li = [b, c, e, f] ※ 합집합list(set(a_li) | set(b_li))== list(set().union(a_li, b_li)) ※ 교집합 ※ 차집합list(set(a_li) - set(b_li)== list(set(a_li).difference(b_li))# 프로그래머스 lv0 글자 지우기# str에서 indices[0,2,6,...] 인덱스 글자들만 빼서 출력하기# 초반 풀이 - 테스트 케이스 에러..def solution(my_string, indices): li = list(set([i for i in range(len(my_string))]).difference(indices))..