This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given three numbers that represent lengths of sides of a triangle, determine if the triangle is valid or not. | |
*/ | |
function validTriangle(a,b,c) { | |
// Usando la ley de los cosenos obtenemos los ángulos del triángulo... | |
let anguloA = Math.acos((b*b+c*c-(a*a))/(2*b*c)); | |
let anguloB = Math.acos((c*c+a*a-(b*b))/(2*c*a)); | |
let anguloC = Math.acos((a*a+b*b-(c*c))/(2*a*b)); | |
// La suma de los 3 ángulos de un triángulo son 180 grados, o en radianes, Pi :-) | |
// Usamos una precisión de 5 decimales, los números reales es lo que tienen... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given a a 2D matrix that represents a maze (with 1s as walls and 0s as path). | |
Find a path from one corner to another, backtracking should be allowed. | |
Return an array of steps taken in order. | |
*/ | |
function included(lista, elem) { | |
var res=false; | |
var i=0; | |
var stringElem=JSON.stringify(elem); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given an int array, remove all leading zeros from the array. | |
removeLeading({0, 0, 0, 1, 0, 2, 3}) | |
> 1 0 2 3 | |
*/ | |
function removeZeros(s) { | |
if (s.length<=0) { | |
return ([]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. | |
Example: | |
waterTrap([2,0,2]) | |
> 2 | |
Structure is like this: | |
| | | |
|_| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Given n, write a function that returns the sum of 1*1! + 2*2! + ... + n*n! | |
function factorial(n) { | |
if (n===0) { | |
return(1); | |
} | |
else { | |
return(n * factorial(n-1)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Implement the charAt() and contains() functions using the best runtime possible. | |
Example: | |
> charAt(5) | |
> a | |
> "fish".contains("i") | |
> true | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Given a queue, write a recursive function to reverse it. | |
function reverse(q) { | |
if (q.length === 1) { | |
return(q); | |
} | |
else { | |
var head=q.shift(); | |
return(reverse(q).concat([head])); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Given two sets of rectangles (where each is represented by an array of four elements, where the first two elements are the coordinates of | |
the upper left corner, and the second pair is of the bottom right), determine the area of the space in which they overlap. | |
Example: | |
> overlapArea([0,0,4,4], [2,2,4,4]) | |
> 4 | |
*/ | |
function area(r){ | |
// base x altura |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Now, onto this week! | |
Given two strings, return if they have a common substring. | |
Bonus: Return the common substring(s). | |
Bonus x2: Do the original question plus the first bonus in less than O(n^2) time! | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function minimumDiff(p) { | |
// old school solution | |
diff=0; | |
if (p.length>1) { | |
diff=Math.abs(p[1]-p[0]); | |
for (i=0;i<p.length;i++){ | |
for (j=i+1;j<p.length;j++){ | |
if (Math.abs(p[j]-p[i])<diff){ | |
diff=Math.abs(p[j]-p[i]); | |
} |
NewerOlder