├── README.md ├── codesignal ├── README.md ├── add.js ├── adjacentElementsProduct.js ├── centuryFromYear.js ├── checkPalindrome.js ├── makeArrayConsecutive2.js └── shapeArea.js ├── daily_coding_problem ├── README.md ├── daily_coding_01.js └── daily_coding_08.js ├── hackerrank └── fizzbuzz.js └── project_euler ├── fibonacci.js ├── largest_prime_factor.js └── multiples_3_5.js /README.md: -------------------------------------------------------------------------------- 1 | # Algorithms 2 | 3 | this repository aims to study and practice data structure and algorithms 4 | 5 | ## websites and levels 6 | 7 | | | level | status | 8 | | ------------- | ----- | ----------- | 9 | | codesignal | Basic | Paused | 10 | | Daily coding | Basic | Paused | 11 | | Hackerrank | Basic | In progress | 12 | | Project Euler | Basic | In progress | 13 | 14 | ## Books 15 | 16 | - [Data structures and algorithms in javascript](https://www.amazon.com.br/Estruturas-Dados-Algoritmos-Javascript-Habilidades/dp/8575225537) - Loiane Groner 17 | 18 | ## Courses 19 | 20 | - [techdevguide](https://techdevguide.withgoogle.com/paths/data-structures-and-algorithms/?no-filter=true) - Google 21 | -------------------------------------------------------------------------------- /codesignal/README.md: -------------------------------------------------------------------------------- 1 | # codesignal-algoritmos 2 | Estudo de algoritmos e estrutura de dados - utilizando os desafios do site https://app.codesignal.com/ - codesignal 3 | -------------------------------------------------------------------------------- /codesignal/add.js: -------------------------------------------------------------------------------- 1 | //Write a function that returns the sum of two numbers. 2 | 3 | function add(param1, param2) { 4 | return param1 + param2; 5 | } -------------------------------------------------------------------------------- /codesignal/adjacentElementsProduct.js: -------------------------------------------------------------------------------- 1 | // Given an array of integers, find the pair of adjacent elements that has the largest product and return that product. 2 | 3 | function adjacentElementsProduct(inputArray) { 4 | let biggestProduct = 0; 5 | inputArray.forEach((value, key) => { 6 | let product = value * inputArray[key + 1]; 7 | 8 | if(key === 0){ 9 | biggestProduct = product 10 | } 11 | 12 | if (product > biggestProduct){ 13 | biggestProduct = product 14 | } 15 | }); 16 | return biggestProduct; 17 | } -------------------------------------------------------------------------------- /codesignal/centuryFromYear.js: -------------------------------------------------------------------------------- 1 | // Given a year, return the century it is in. The first century spans from the year 1 up to and including the year 100, the second - from the year 101 up to and including the year 200, etc. 2 | 3 | function centuryFromYear(year) { 4 | 5 | // tutorial youtube calculo dos seculos - https://www.youtube.com/watch?v=D5Tjn_vhmWM 6 | 7 | var newyear = Math.ceil(year/100); 8 | 9 | return newyear; 10 | } -------------------------------------------------------------------------------- /codesignal/checkPalindrome.js: -------------------------------------------------------------------------------- 1 | // Given the string, check if it is a palindrome. 2 | 3 | function checkPalindrome(inputString) { 4 | if(inputString === inputString.split("").reverse().join("")){return true} else { return false } 5 | } 6 | 7 | 8 | // ------- old - 16 jan 19 9 | // function checkPalindrome(inputString) { 10 | // if(inputString.length > 1){ 11 | // let result = false; 12 | // for(let i = 0; i < inputString.length-1; i++){ 13 | // if(inputString.charAt(i) == inputString.charAt((inputString.length)-(i+1))){ 14 | // result = true; 15 | // }else{ 16 | // result = false; 17 | // break; 18 | // } 19 | // } 20 | // return result; 21 | // }else{ 22 | // return true; 23 | // } 24 | // } -------------------------------------------------------------------------------- /codesignal/makeArrayConsecutive2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Ratiorg got statues of different sizes as a present from CodeMaster for his birthday, each statue having an non-negative integer size. Since he likes to make things perfect, he wants to arrange them from smallest to largest so that each statue will be bigger than the previous one exactly by 1. He may need some additional statues to be able to accomplish that. Help him figure out the minimum number of additional statues needed. 4 | * 5 | */ 6 | 7 | function makeArrayConsecutive2(statues) { 8 | let nArr = []; 9 | let count = 0; 10 | for(let i = Math.min(...statues); i < Math.max(...statues); i++){ 11 | nArr.push(i); 12 | } 13 | for(let i in nArr){ 14 | if(nArr.indexOf(statues[i]) === -1) count++; 15 | } 16 | return count; 17 | } 18 | -------------------------------------------------------------------------------- /codesignal/shapeArea.js: -------------------------------------------------------------------------------- 1 | /** 2 | Below we will define an n-interesting polygon. Your task is to find the area of a polygon for a given n.A 1-interesting polygon is just a square with a side of length 1. An n-interesting polygon is obtained by taking the n - 1-interesting polygon and appending 1-interesting polygons to its rim, side by side. You can see the 1-, 2-, 3- and 4-interesting polygons in the picture below. 3 | */ 4 | 5 | function shapeArea(n) { 6 | return n * (n+n-1) - (n-1); 7 | } -------------------------------------------------------------------------------- /daily_coding_problem/README.md: -------------------------------------------------------------------------------- 1 | # Daily Coding Problem 2 | Sample Programming Interview Question - https://www.dailycodingproblem.com/ 3 | -------------------------------------------------------------------------------- /daily_coding_problem/daily_coding_01.js: -------------------------------------------------------------------------------- 1 | // This problem was recently asked by Google. 2 | 3 | // Given a list of numbers and a number k, return whether any two numbers from the list add up to k. 4 | 5 | // For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17. 6 | 7 | // Bonus: Can you do this in one pass? 8 | 9 | function sumFirstAndLast(array){ 10 | return array[0] + array[array.length - 1]; 11 | } 12 | 13 | sumManAndMin([10, 15, 3, 7]); -------------------------------------------------------------------------------- /daily_coding_problem/daily_coding_08.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Good morning! Here's your coding interview problem for today. 3 | 4 | This problem was asked by Google. 5 | 6 | A unival tree (which stands for "universal value") is a tree where all nodes under it have the same value. 7 | 8 | Given the root to a binary tree, count the number of unival subtrees. 9 | 10 | For example, the following tree has 5 unival subtrees: 11 | */ -------------------------------------------------------------------------------- /hackerrank/fizzbuzz.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Complete the 'fizzBuzz' function below. 3 | * 4 | * The function accepts INTEGER n as parameter. 5 | */ 6 | 7 | function fizzBuzz(n) { 8 | const messages = { 9 | fullMessage: 'FizzBuzz', 10 | firstMessage: 'Fizz', 11 | lastMessage: 'Buzz' 12 | } 13 | 14 | for (let i = 1; i <= n; i++) { 15 | if (i % 3 === 0 && i % 5 === 0) { 16 | console.log(messages.fullMessage) 17 | } else if (i % 3 === 0 && !(i % 5 === 0)) { 18 | console.log(messages.firstMessage) 19 | } else if (!(i % 3 === 0) && i % 5 === 0) { 20 | console.log(messages.lastMessage) 21 | } else { 22 | console.log(i) 23 | } 24 | } 25 | } 26 | 27 | function main() { 28 | const n = parseInt(readLine().trim(), 10); 29 | 30 | fizzBuzz(n); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /project_euler/fibonacci.js: -------------------------------------------------------------------------------- 1 | // Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 2 | // 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... 3 | // By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. 4 | 5 | function fibonacci(n) { 6 | let next, 7 | prev, 8 | i = 0, 9 | j = 0, 10 | accumulated = 0, 11 | sequence = [1]; 12 | 13 | for (i; i < n; i++) { 14 | if (i === 0) { 15 | sequence.push(sequence[i] + 1); 16 | } else if (sequence[i] < n) { 17 | prev = sequence[i - 1]; 18 | next = prev + sequence[i]; 19 | 20 | if (next > n) break; 21 | 22 | sequence.push(next); 23 | } 24 | } 25 | 26 | for (j; j < sequence.length; j++) { 27 | if (sequence[j] % 2 === 0) { 28 | accumulated = accumulated + sequence[j]; 29 | } 30 | } 31 | 32 | return accumulated; 33 | } 34 | 35 | fibonacci(4000000); 36 | -------------------------------------------------------------------------------- /project_euler/largest_prime_factor.js: -------------------------------------------------------------------------------- 1 | /** 2 | * The prime factors of 13195 are 5, 7, 13 and 29. 3 | * What is the largest prime factor of the number 600851475143 ? 4 | * 5 | * prime number conditionals: 6 | * divide by one and by itself 7 | */ 8 | 9 | function isPrime(number) { 10 | if (number / 1 && number / number === 1) { 11 | return true; 12 | } 13 | return false; 14 | } 15 | 16 | function findDivider(number) { 17 | counter = 2; 18 | 19 | while (True) { 20 | if (isPrime(number) && number % counter === 0) break; 21 | counter++; 22 | } 23 | 24 | return counter; 25 | } 26 | 27 | function findLargestPrimeFactor(number = 600851475143) { 28 | if (isPrime(number)) { 29 | findLargestPrimeFactor(number / findDivider()); 30 | } 31 | } 32 | 33 | findLargestPrimeFactor(); 34 | -------------------------------------------------------------------------------- /project_euler/multiples_3_5.js: -------------------------------------------------------------------------------- 1 | // If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. 2 | // Find the sum of all the multiples of 3 or 5 below 1000. 3 | 4 | function sumMultiples(n = 1000) { 5 | let multiples = 0; 6 | 7 | for (let i = 1; i < n; i++) { 8 | if (i % 3 === 0 || i % 5 === 0) { 9 | multiples = multiples + i; 10 | } 11 | } 12 | 13 | return multiples; 14 | } 15 | 16 | sumMultiples(); 17 | --------------------------------------------------------------------------------