Skip to content

Instantly share code, notes, and snippets.

@acalpixca
acalpixca / cassidoo challenge 08-08-2019
Last active August 14, 2019 17:06
cassidoo challenge 08-08-2019
/*
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...
@acalpixca
acalpixca / cassidoo challenge 27-08-2018
Created August 31, 2018 12:43
cassidoo challenge 27-08-2018
/*
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);
@acalpixca
acalpixca / cassidoo challenge 2018-07-24
Created July 25, 2018 13:57
cassidoo challenge 2018-07-24
/*
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 ([]);
@acalpixca
acalpixca / cassidoo challenge 25-06-2018
Last active June 27, 2018 08:38
cassidoo challenge 25-06-2018
/*
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:
| |
|_|
@acalpixca
acalpixca / cassidoo challenge 29-05-2018
Created June 4, 2018 15:32
cassidoo challenge 29-05-2018
// 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));
}
}
@acalpixca
acalpixca / cassidoo challenge 15-05-2018
Created June 4, 2018 14:35
cassidoo challenge 15-05-2018
/*
Implement the charAt() and contains() functions using the best runtime possible.
Example:
> charAt(5)
> a
> "fish".contains("i")
> true
*/
@acalpixca
acalpixca / cassidoo challenge 03-06-2018
Created June 4, 2018 11:06
cassidoo challenge 03-06-2018
//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]));
@acalpixca
acalpixca / cassidoo challenge 12-03-2018
Created March 14, 2018 17:02
cassidoo challenge 12-03-2018
/* 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
@acalpixca
acalpixca / gist:6d26f064f0d0c695dc6cdc1ef965fba2
Created March 3, 2018 15:14
cassidoo challenge 26/02/2018
/*
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!
*/
@acalpixca
acalpixca / gist:6640853746c91920be384b913f48ffb4
Last active February 19, 2018 14:44
cassidoo challenge 18/02/2018
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]);
}