Skip to content

Instantly share code, notes, and snippets.

@mlhaufe
Created December 10, 2018 05:23
Show Gist options
  • Save mlhaufe/ba15a8c337062222eb3e723cdf7a5f12 to your computer and use it in GitHub Desktop.
Save mlhaufe/ba15a8c337062222eb3e723cdf7a5f12 to your computer and use it in GitHub Desktop.
function main() {
console.log("question1(0) == 0");
console.assert(question1(0) == 0, `${question1(0)}`);
console.log("question1(1) == 1");
console.assert(question1(1) == 1, `${question1(1)}`);
console.log("question1(7) == 13");
console.assert(question1(7) == 13, `${question1(7)}`);
console.log("question1(12) == 144");
console.assert(question1(12) == 144, `${question1(12)}`);
console.log("question2(987) == 6");
console.assert(question2(987) == 6, `${question2(987)}`);
console.log("question2(1024) == 7");
console.assert(question2(1024) == 7, `${question2(1024)}`);
console.log("question2(777) == 3");
console.assert(question2(777) == 3, `${question2(777)}`);
console.log("question3(5,13,6,104,13,6) == 99");
console.assert(question3(5,13,6,104,13,6) == 99, `${question3(5,13,6,104,13,6)}`);
console.log("question3(0,0,0,0,0,1) == 1");
console.assert(question3(0,0,0,0,0,1) == 1, `${question3(0,0,0,0,0,1)}`);
console.log("question4(10) == 23");
console.assert(question4(10) == 23, `${question4(10)}`);
console.log("question4(100) == 2318");
console.assert(question4(100) == 2318, `${question4(100)}`);
console.log("question4(1000) == 233168");
console.assert(question4(1000) == 233168, `${question4(1000)}`);
console.log("question5(476) == \"Four Hundred and Seventy Six\"");
console.assert(question5(476) == "Four Hundred and Seventy Six", question5(476));
console.log("question5(1024) == \"One Thousand Twenty Four\"");
console.assert(question5(1024) == "One Thousand Twenty Four", question5(1024));
console.log("question5(8675309) == \"Eight Million Six Hundred and Seventy Five Thousand Three Hundred and Nine\"");
console.assert(question5(8675309) == "Eight Million Six Hundred and Seventy Five Thousand Three Hundred and Nine", question5(8675309));
console.log("question6(\"The quick brown fox jumps over lazy dogs\") == true");
console.assert(question6("The quick brown fox jumps over lazy dogs") == true, `${question6("The quick brown fox jumps over lazy dogs")}`);
console.log("question6(\"The five boxing wizards dump quickly\") == false");
console.assert(question6("The five boxing wizards dump quickly") == false, `${question6("The five boxing wizards dump quickly")}`);
console.log("question6(\"HEAVY BOXES PERFORM WALTZES AND JIGS\")");
console.assert(question6("HEAVY BOXES PERFORM WALTZES AND JIGS") == false, `${question6("HEAVY BOXES PERFORM WALTZES AND JIGS")}`);
console.log("question7(1) == false");
console.assert(question7(1) == false, `${question7(1)}`);
console.log("question7(5) == true");
console.assert(question7(5) == true, `${question7(5)}`);
console.log("question7(13) == true");
console.assert(question7(13) == true, `${question7(13)}`);
console.log("question7(837) == false");
console.assert(question7(837) == false, `${question7(837)}`);
console.log("question8(35) == 44");
console.assert(question8(35) == 44, `${question8(35)}`);
console.log("question8(4000000) == 4613732");
console.assert(question8(4000000) == 4613732, `${question8(4000000)}`);
console.log("question8(2048) == 798");
console.assert(question8(2048) == 798, `${question8(2048)}`);
}
/*
* Easy - You are on the ground step(0th step) of a staircase
* consisting of N steps. You can step up 1 step at a time, or land above
* the next step (This does not mean skipping 2 steps a.k.a skipping one step).
* Climbing the stairs with each step whether being 1 step up or up the next step,
* how many different ways can you climb the N steps?
*/
// 0
let question1 = (steps: number): number => {
// if I'm currently on step N,
// I either got here from the previous step: N - 1
// or I got here by from the step below that: N - 2
// so the result would be the sum of both possibilities:
// (N - 1) + (N - 2)
//If you squint, you'll realize that this is the fibonacci sequence
//in disquise...
return steps <= 1 ? steps :
question1(steps - 1) + question1(steps - 2);
}
/*
* Easy - Find the sum of all digits in an int such that you get a single digit number.
* Example:
* 987 => 9+8+7 = 24 => 2+4 = 6
*/
// 1
let question2 = (x: number): number => {
var s = x.toString();
return s.length == 1 ? x : question2(
Array.from(s).map(c => Number(c)).reduce((sum, next) => sum + next)
);
}
/*
* Easy - What is the distance between two 3D points in cartesian space?
* Example:
* P1 = (5,13,6)
* P2 = (104,13,6)
* distance(P1,P2) == 99
*
*/
let question3 = (x1: number, y1: number, z1: number, x2: number, y2: number, z2: number): number => {
return Math.sqrt(Math.pow(x2 - x1,2) + Math.pow(y2 - y1, 2) + Math.pow(z2 - z1,2));
}
/*
* Medium
*
* 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.
* Find the sum of all the multiples of 3 or 5 below the given number.
*/
let question4 = (n: number): number => {
let result = 0;
for (let i = 1; i < n; i++) {
if (((i % 3) == 0) || ((i % 5) == 0)) {
result += i;
}
}
return result;
}
/*
* Medium
* Given a random integer between 0 and 9,999,999 inclusive, produce its exact English equivalent as a string.
* Example:
* 476 -> Four Hundred and Seventy Six
*/
let question5 = (i: number): string => {
var units = ["Zero","One","Two","Three","Four",
"Five","Six","Seven","Eight","Nine","Ten",
"Eleven","Twelve","Thirteen","Fourteen","Fifteen",
"Sixteen","Seventeen","Eighteen","Nineteen"];
var tens = ["","","Twenty","Thirty","Forty","Fifty",
"Sixty","Seventy","Eighty","Ninety"];
return i < 20 ? units[i] :
i < 100 ? tens[i/10] + ((i % 10 > 0)? " " + question5(i % 10):"") :
i < 1000 ? units[i/100] + " Hundred" + ((i % 100 > 0)?" and " + question5(i % 100):"") :
i < 1000000 ? question5(i / 1000) + " Thousand " + ((i % 1000 > 0)? "" + question5(i % 1000):"") :
question5(i / 1000000) + " Million " + ((i % 1000000 > 0)? "" + question5(i % 1000000):"");
}
/*
* Easy
* A pangram is a sentence that contains all the letters of the English alphabet at least once.
* The most popular example is: "The quick brown fox jumps over the lazy dog"
* Detect if a given string is a pangram
*/
let question6 = (sentence: string): boolean => {
var alphabet = "abcdefghijklmnopqrstuvwxyz";
return Array.from(alphabet).every(sentence.toLowerCase().includes)
}
/*
* Medium
* the first six prime numbers: 2, 3, 5, 7, 11, and 13
* given an arbitrary integer, determine if it is prime
*/
let question7 = (n: number): boolean => {
if (n <= 1) {
return false;
}
if(n == 2){
return true;
}
if (n % 2 == 0) {
return false;
}
let counter = 3;
while ((counter * counter) <= n) {
if (n % counter == 0) {
return false;
} else {
counter +=2;
}
}
return true;
}
/*
Hard
The fibonacci sequence is: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
The next number is found by adding up the two numbers before it.
Find the sum of all the even-valued terms in the sequence which do not exceed a given limit.
Example
question8(35) = 2 + 8 + 34 = 44
*/
let question8 = (limit: number): number => {
let fib = [2,0];
let i = 0;
let result = 0;
while (fib[i] < limit) {
result += fib[i];
i = (i + 1) % 2;
fib[i] = 4 * fib[(i + 1) % 2] + fib[i];
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment