Coding Test/정리

Coding Test/정리

[Swift] Swift를 이용한 문제 풀이

프로그래머스 Lv1 남은 문제들을 Swift로 풀어보면서 얻었던 IDEA들을 정리했다. map, filter, reduce func solution(_ arr:[Int], _ divisor:Int) -> [Int] { let array = arr.sorted().filter{ $0 % divisor == 0 } return array == [] ? [-1] : array } map: 클로저로 각 항목들을 반영한 결과물을 가진 새로운 배열을 반환합니다. filter: 클로저로 각 항목들을 비교하여 일치하는 결과물을 가진 새로운 배열을 반환합니다. reduce: 배열의 각 항목들을 재귀적으로 클로저를 적용시켜 하나의 값을 만듭니다. array.map { $0 * 2 } array.filter { $0 % 2..

Coding Test/정리

[python] list pop VS pop first

5. 자료 구조 — Python 3.10.2 문서 5. 자료 구조 이 장에서는 여러분이 이미 배운 것들을 좀 더 자세히 설명하고, 몇 가지 새로운 것들을 덧붙입니다. 5.1. 리스트 더 보기 리스트 자료 형은 몇 가지 메서드들을 더 갖고 있습니다. 이 docs.python.org 강의를 듣다가 잊고 있던 부분 다시 한 번 상기. 강의에서는 pop(0) 보다 pop()이 더 효율적이기 때문에, 리스트를 역순으로 정렬한 뒤 사용하는 풀이를 보여줬다. 리스트를 큐로 사용하는 것도 가능한데, 처음으로 넣은 요소가 처음으로 꺼내지는 요소입니다 (《first-in, first-out》); 하지만, 리스트는 이 목적에는 효율적이지 않습니다. 리스트의 끝에 덧붙이거나, 끝에서 꺼내는 것은 빠르지만, 리스트의 머리에 덧..

Coding Test/정리

[python] functools.cmp_to_key(func)

functools — Higher-order functions and operations on callable objects — Python 3.10.2 documentation docs.python.org functools.cmp_to_key(func) Transform an old-style comparison function to a key function. Used with tools that accept key functions (such as sorted(), min(), max(), heapq.nlargest(), heapq.nsmallest(), itertools.groupby()). This function is primarily used as a transition tool for pr..

Coding Test/정리

[python] Dictionary, Counter

Dictionary는 Hash Table로 구성되어 있는데, 이는 상수 시간으로 접근이 가능하다. Counter의 연산. Counter는 Dictionary의 subclass이다. >>> c = Counter(a=3, b=1) >>> d = Counter(a=1, b=2) >>> c + d # add two counters together: c[x] + d[x] Counter({'a': 4, 'b': 3}) >>> c - d # subtract (keeping only positive counts) Counter({'a': 2}) >>> c & d # intersection: min(c[x], d[x]) Counter({'a': 1, 'b': 1}) >>> c | d # union: max(c[x], d..

EUNJI HA
'Coding Test/정리' 카테고리의 글 목록