본문 바로가기

분류 전체보기

Hash and Map 1. Map The Map object holds key-value pairs and remembers the original insertion order of the keys. Any value (both objects and primitive values) may be used as either a key or a value. for . . . of loop returns an array of [key, value] for each iteration 2. 관련 함수들 python의 dictionary와 다른 점은 접근, 수정시에 항상 set, get 매서드를 사용하여야 한다는 점이다. const contacts = new Map() contacts.set('Jessie', {phone: "213-55.. 더보기
array 중복 제거 및 중복 값 찾기 1. 사용할 함수 및 클래스 Array.indexOf() Array.find() Array.filter() Set 2. 중복 제거 - Set 객체를 이용하여 중복 제거(본 예제는 두개의 배열을 합친 후 중복을 제거하는 예제이다.) let a = [1, 3, 5, 2, 9] let b = [3, 2, 5, 6, 9] let answer = [...a, ...b] let set = new Set(c) answer = [...set] - indexOf(), filter() : indexOf()는 가장 앞의 index를 반환하고 filter()는 조건을 만족하는 값만 걸려서 새로운 배열을 만들어준다. const dupArr = [1, 2, 3, 1, 2]; const uniqueArr = dupArr.filte.. 더보기
array 합치기 python과 달리 JS에서는 배열을 합칠 때 "+" 연산자를 사용할 수 없다. 1. concat() 함수 let a = [1, 3, 5]; let b = [2, 3, 6, 7, 9]; let c = a.concat(b) 2. ... spread operator (전개 연산자) let a = [1, 3, 5]; let b = [2, 3, 6, 7, 9]; let c = [...a, ...b] 더보기
Map and Hash 1. Map(Hash Table) A map models a searchable collection of (key, value) entries value 중복은 허용되지만 key의 중복은 허용되지 않는다. 예시로 python의 dictionary가 있다. 2. Map ADT(abstract data type) M[k] : return the value of v associated with key k, or(if the key is not exist) raise a KeyError M[k] = v : add or replace del M[k] len(M) iter(M) k in M : return Ture if the map contains an item with key k M.get(k, d=None) .. 더보기
Amortized Analysis(분할 상환 분석) 1. 분할 상환 분석 알고리즘 분석시 각각의 연산마다 최악의 경우를 가정하는 것은 불합리하다. 어떠한 임의의 알고리즘에서 어떤 연산은 자원적 측면에서 상당한 비용을 소모하는 반면 다른 연산은 그렇게 고비용을 지불하지 않을 수도 있 상환 분석은 알고리즘의 전반적인 연산 집합에 대해 비용이 높은 연산, 그리고 비용이 덜한 연산 모두를 함께 고려한다. 2. Array Doubling : Array에 새로운 원소를 append 할 때는 O(1)의 시간이 들지만 Array가 full이 되는 순간 Array Doubling에 O(n)의 시간을 소요한 뒤에 append를 수행하게 된다. 따라서 Amortized Analysis를 통해 분석을 하고 O(1*)(amortized O(1) time) 3. Amortized.. 더보기
asymptotic notation(점근 표기법) 1. asymptotic notation : 알고리즘의 효율성을 표기위한 방법으로 상수 계수와 중요하지 않은 항목들을 제거한 것이다. 2. growth rate : n 값이 일정하게 증가함에 따라 늘어나는 시간을 확인한다. : c < logn < n < nlogn < n^2 더보기
String(), toString() 문자열로 형변환시에 String(), toString() 두가지 매서드를 자주 사용하게 되는데 차이가 뭘까? String() 은 null과 undefined에 대헤서도 잘 동작하는 반면, toString() 사용시 에러가 발생한다. 1. String - String 전역 객체는 문자열의 생성자이다. - thing : 문자열로 변환할 아무 값. String(thing) 2. toString() - 반면 toString()은 Number Class의 매서드이다. - Number.prototype.toString() 단순 형변환을 위해서라면 두개에 큰 차이는 없다. 더보기
문자열에서 특정 값인 index 전부 탐색 하기 1. indexOf str.indexOf(searchValue[, fromIndex]) - 맨 처음 나온 Index 또는 -1을 반환 - fromIndex를 통해 찾기 시작할 위치 지정 가능 2. 문자열 내 index 전부 찾기 - 카운팅 하기 var str = 'To be, or not to be, that is the question.'; var count = 0; var pos = str.indexOf('e'); //pos는 4의 값을 가집니다. while (pos !== -1) { count++; pos = str.indexOf('e', pos + 1); // 첫 번째 e 이후의 인덱스부터 e를 찾습니다. } console.log(count); // 로그에 4를 출력합니다. - Index 순서대로.. 더보기