Coding Test
-
문자열 정렬 알고리즘 - 애너그램, 팰린드롬, 암호화 / 복호화Coding Test 2023. 12. 4. 21:59
sorted(s1) == sorted(s2) -> 정렬되므로 같은 문자열로 이루어진 문자(애너그램)인지 판별 가능하다. 팰린드롬(회문)Palindrome = 앞뒤가 똑같은 문자(말해말..) 파이썬에서의 슬라이스: a[3::2] ->3번째 인덱스부터 2씩 증가시키면서 마지막 요소까지 가져와 새 리스트를 만듦. s1[::-1] -> 마지막 요소부터 1씩 인덱스를 감소시키면서 요소를 가져와 뒤집은 문자열을 만들게된다. 파이썬 ord -> 아스키코드 반환 isdigit()-> 문자열에서 숫자만 남김 digit -> 0-9사이의 기호를 나타냄. s = "Buy 1 get 2 free" //[-1]부분이 마지막 글자만 뽑아냄 nl = [c for in s c.isdigit()[-1]] 암호 (Cipher) = 암호..
-
정렬 알고리즘Coding Test 2023. 12. 3. 20:40
정렬 알고리즘 : 데이터를 일정한 순서로 배치하는 것 1. 버블 정렬 for 문을 두번 사용해서 시간복잡도는 O(n^2) 배우기가 간단함 2. 삽입정렬 버블정렬보다는 효율적일지도? -> 어느정도 정렬이 되어있을 때의 시간복잡도가 O(n)으로 상당히 효율적임 3. 병합정렬 (Merge sort) 리스트를 계속해서 반으로 나눠 요소가 한 개뿐인 리스트로만 남았을 때, 이들을 올바른 순서대로 다시 합치는 재귀정렬 알고리즘. 🔶 O(logN) == O(log₂N)을 의미한다. O(n * log(n)) 의 시간복잡도 이진 검색이 정확히 O(logN) 알고리즘 방식으로 동작한다. 이진 검색을 빅 오 표기법의 관점에서 어떻게 설명할까? 배열의 크기가 3일 때 이진 검색은 2단계 배열의 크기가 7일 때 이진 검색은 3..
-
[Swift] 재귀 알고리즘Coding Test 2023. 11. 28. 00:51
Algorithm Recursion 재귀는 문제를 더 작은 부분으로 나누고, 각 부분의 문제를 해결한 후 결과를 조합해 전체 문제의 답을 찾는 해결 방법이다. 재귀 알고리즘의 법칙 반드시 종료 조건이 있어야 한다. 반드시 자기 자신의 상태를 변경하면서 종료 조건에 가까워져야 한다. 반드시 자기 자신을 재귀적으로 호출해야한다. 종료 조건 재귀 함수에서 재귀 함수가 자기 자신을 끝없이 호출하지 않도록 알고리즘을 빠져나가는 조건 재귀 알고리즘 특징 반복에 비해 코드가 간결함 스택에 데이터 저장 -> 메모리 소비가 많을 수 있음 import Foundation func recursionFunction(input:Int) -> String { if input != 0 { print("\(input)") retur..
-
[Swift] 내적Coding Test 2023. 11. 12. 23:24
import Foundation func solution(_ a:[Int], _ b:[Int]) -> Int { var resultArray: [Int] = [] var result: Int = 0 for i in 0...a.count - 1 { resultArray.append(a[i]*b[i]) } result = resultArray.reduce(0){$0+$1} return result } 1. 배열의 개수는 a.count - 1 이 필요하다. : 배열 원소 접근 시 주의할 것 2. reduce 사용법 reduce(초기값){실행할 작업} reduce(0, +) reduce(초기값,실행할 작업) 이렇게도 표기 가능 3. zip으로 묶고, map으로 (풀이 참고) zip(a, b).map(*).red..