-
[알고리즘/자바스크립트] Pig Latin 게임 (Simple Pig Latin)Algorithm 2019. 8. 4. 22:32
-해당 문제는 codewars사이트의 level5 문제입니다. (1~8단계 중 8단계가 가장 쉬운 레벨)- Simple Pig Latin Move the first letter of each word to the end of it, then add "ay" to the end of the word. Leave punctuation marks untouched. pigIt('Pig latin is cool'); // igPay atinlay siay oolcay pigIt('Hello world !'); // elloHay orldway ! 매우 간단한 문제이다. 어떤 문장을 받으면 각 단어의 첫 글자를 그 단어 맨 뒤에 붙이고 'ay'를 덧붙여야한다. ?나 !와 같은 punctuation marks는 제..
-
[알고리즘/자바스크립트] 아나그램 모두 찾기 (Where my anagrams at?)Algorithm 2019. 8. 4. 22:22
-해당 문제는 codewars사이트의 level5 문제입니다. (1~8단계 중 8단계가 가장 쉬운 레벨)- Where my anagrams at? What is an anagram? Well, two words are anagrams of each other if they both contain the same letters. For example: 'abba' & 'baab' == true 'abba' & 'bbaa' == true 'abba' & 'abbba' == false 'abba' & 'abca' == false Write a function that will find all the anagrams of a word from a list. You will be given two inputs a..
-
[알고리즘/자바스크립트] 반복되지 않는 첫 알파벳 찾기 (First non-repeating character)Algorithm 2019. 8. 4. 22:09
-해당 문제는 codewars사이트의 level5 문제입니다. (1~8단계 중 8단계가 가장 쉬운 레벨)- First non-repeating character Write a function named first_non_repeating_letter that takes a string input, and returns the first character that is not repeated anywhere in the string. For example, if given the input 'stress', the function should return 't', since the letter t only occurs once in the string, and occurs first in the strin..
-
[알고리즘/자바스크립트] 알파벳 스크램블 (Scramblies)Algorithm 2019. 8. 3. 22:09
-해당 문제는 codewars사이트의 level5 문제입니다. (1~8단계 중 8단계가 가장 쉬운 레벨)- Scramblies Complete the function scramble(str1, str2) that returns true if a portion of str1 characters can be rearranged to match str2, otherwise returns false. Only lower case letters will be used (a-z). No punctuation or digits will be included. Performance needs to be considered. scramble('rkqodlw', 'world'); // ==> True scramble('c..
-
[알고리즘/자바스크립트] 한 자리수가 될 때까지 곱하기 (Persistent Bugger)Algorithm 2019. 8. 3. 21:40
-해당 문제는 codewars사이트의 level6 문제입니다. (1~8단계 중 8단계가 가장 쉬운 레벨)- Persistent Bugger Write a function, persistence, that takes in a positive parameter num and returns its multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a single digit. For example: persistence(39) === 3 // because 3*9 = 27, 2*7 = 14, 1*4=4 // and 4 has only one digit persisten..
-
[알고리즘/자바스크립트] 트리에서 잎노드 개수 구하기 (Tree Count Leaves)Algorithm 2019. 7. 23. 11:11
Tree Count Leaves Implement a `countLeaves` in this basic binary Tree class. A leaf node is any node in the tree that has no children. `countLeaves` should traverse the tree, and return the number of leaf nodes the tree contains. Illustration of a tree with three leaves: Example usage: var root = new Tree(); root.countLeaves(); // 1 root.addChild(new Tree()); root.countLeaves(); // still 1 root...
-
[JS/Sorting] 퀵 정렬, 자바스크립트로 구현하기 (Quick Sort in JavaScript)Frontend 2019. 7. 21. 13:24
Quick Sort 퀵 정렬은 1960년에 찰스 앤터니 리처드 호어(C.A.R hoare)가 처음 제안한 방법으로 이후 많은 사람들이 수정 보완하여 완성된 정렬 알고리즘이다. 이 알고리즘은 처음 소개된 이후로 반세기가 넘었지만 현존하는 가장 빠른 정렬 알고리즘 중에 하나이다. 퀵 정렬은 in place 방법과 in place가 아닌 방법 2가지가 있는데 실제로 많이 쓰이는 방법은 메모리 사용량이 적은 in place 방법이다. 그러나 in place가 아닌 방법이 더 직관적으로 이해하기 쉬우므로 이 방법을 먼저 정리하고 그 다음 in place 방법을 정리하겠다. Basic Quick Sort - Not In Place 퀵 정렬은 지난번 병합 정렬 포스팅에서 소개한 Divide and Conquer 전략..
-
[JS/Sorting] 병합 정렬(Merge Sort)과 분할 정복 전략(Divide and Conquer) 개념에 대하여Frontend 2019. 7. 21. 11:49
Merge Sort 병합 정렬 혹은 합병 정렬이라고 불리는 Merge Sort는 데이터들을 잘게 쪼갠 다음에 하나로 합치는 과정에서 정렬하는 방법이다. 요즘은 데이터를 USB같은 장치로 저장하는 것이 일반적이었으나 아주 옛날에는 테이프를 이용해서 저장했다. 테이프 드라이브에 저장된 데이터를 정렬하는 일은 매우 어려운 일이었다. 왜냐면 테이프 드라이브는 항상 데이터를 처음부터 순차적으로 읽어야 한다는 특징이 있기 때문이다. 이러한 테이프 드라이브의 문제점 때문에 병합 정렬 알고리즘이 탄생하였다. 병합 정렬은 위의 그림처럼 데이터를 절반씩 쪼개는 divide 작업을 먼저 하는데 이 작업은 데이터가 하나만 남을 때까지 반복한다. 그 후에 하나씩 쪼개진 데이터를 정렬을 하면서 다시 그룹화한다. 이런 과정은 아래..
-
[JS/Sorting] 버블 정렬, 삽입 정렬, 선택 정렬 자바스크립트로 구현하기 (Bubble Sort, Insertion Sort, Selection Sort in JavaScript)Frontend 2019. 7. 20. 23:47
Sorting Algorithm 무작위로 섞여있는 데이터를 어떤 기준에 맞춰 정렬하는 알고리즘은 여러 가지가 있다. 정렬 알고리즘은 다양한 경우에 매우 유용하게 사용된다. 각종 데이터 목록을 정리하고 싶을 때 분포도의 중위값을 알아내고 싶을 때 데이터에서 중복값을 잡아내고 싶을 때 이진 탐색을 하고 싶을 때 사실 내가 공부하는 자바스크립트에 sort()라는 메소드가 이미 존재하듯이, 모든 프로그래밍 언어에는 자체적인 정렬 메소드가 있다. 그럼에도 불구하고 우리가 정렬 알고리즘을 배워야하는 이유는 시스템 정렬이 항상 좋은 퍼포먼스를 보장하지 않기 때문이다. 또한 내가 가진 데이터베이스의 양이나 상황에 따라 어떤 정렬을 사용하는 것이 좋을지 달라지기 때문에 우리는 기본적으로 유명한 정렬 알고리즘들은 반드시 ..
-
[알고리즘/자바스크립트] 뒤집어진 연결 리스트 (Reverse Linked List)Algorithm 2019. 7. 18. 11:08
Reverse Linked List Write a function that reverses the order of a singly-linked list in place. So a list like this: A -> B -> C -> null Should be transformed into a list like this: C -> B -> A -> null Example) var root = Node('A'); var nodeB = root.next = Node('B'); var nodeC = nodeB.next = Node('C'); // The list looks like this: A -> B -> C -> null var newRoot = reverseLinkedList(root); // The ..