Guest User

Untitled

a guest
Dec 9th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.31 KB | None | 0 0
  1. function main() {
  2. console.log("question1(0) == 0");
  3. console.assert(question1(0) == 0, `${question1(0)}`);
  4. console.log("question1(1) == 1");
  5. console.assert(question1(1) == 1, `${question1(1)}`);
  6. console.log("question1(7) == 13");
  7. console.assert(question1(7) == 13, `${question1(7)}`);
  8. console.log("question1(12) == 144");
  9. console.assert(question1(12) == 144, `${question1(12)}`);
  10.  
  11. console.log("question2(987) == 6");
  12. console.assert(question2(987) == 6, `${question2(987)}`);
  13. console.log("question2(1024) == 7");
  14. console.assert(question2(1024) == 7, `${question2(1024)}`);
  15. console.log("question2(777) == 3");
  16. console.assert(question2(777) == 3, `${question2(777)}`);
  17.  
  18. console.log("question3(5,13,6,104,13,6) == 99");
  19. console.assert(question3(5,13,6,104,13,6) == 99, `${question3(5,13,6,104,13,6)}`);
  20. console.log("question3(0,0,0,0,0,1) == 1");
  21. console.assert(question3(0,0,0,0,0,1) == 1, `${question3(0,0,0,0,0,1)}`);
  22.  
  23. console.log("question4(10) == 23");
  24. console.assert(question4(10) == 23, `${question4(10)}`);
  25. console.log("question4(100) == 2318");
  26. console.assert(question4(100) == 2318, `${question4(100)}`);
  27. console.log("question4(1000) == 233168");
  28. console.assert(question4(1000) == 233168, `${question4(1000)}`);
  29.  
  30. console.log("question5(476) == \"Four Hundred and Seventy Six\"");
  31. console.assert(question5(476) == "Four Hundred and Seventy Six", question5(476));
  32. console.log("question5(1024) == \"One Thousand Twenty Four\"");
  33. console.assert(question5(1024) == "One Thousand Twenty Four", question5(1024));
  34. console.log("question5(8675309) == \"Eight Million Six Hundred and Seventy Five Thousand Three Hundred and Nine\"");
  35. console.assert(question5(8675309) == "Eight Million Six Hundred and Seventy Five Thousand Three Hundred and Nine", question5(8675309));
  36.  
  37. console.log("question6(\"The quick brown fox jumps over lazy dogs\") == true");
  38. console.assert(question6("The quick brown fox jumps over lazy dogs") == true, `${question6("The quick brown fox jumps over lazy dogs")}`);
  39. console.log("question6(\"The five boxing wizards dump quickly\") == false");
  40. console.assert(question6("The five boxing wizards dump quickly") == false, `${question6("The five boxing wizards dump quickly")}`);
  41. console.log("question6(\"HEAVY BOXES PERFORM WALTZES AND JIGS\")");
  42. console.assert(question6("HEAVY BOXES PERFORM WALTZES AND JIGS") == false, `${question6("HEAVY BOXES PERFORM WALTZES AND JIGS")}`);
  43.  
  44. console.log("question7(1) == false");
  45. console.assert(question7(1) == false, `${question7(1)}`);
  46. console.log("question7(5) == true");
  47. console.assert(question7(5) == true, `${question7(5)}`);
  48. console.log("question7(13) == true");
  49. console.assert(question7(13) == true, `${question7(13)}`);
  50. console.log("question7(837) == false");
  51. console.assert(question7(837) == false, `${question7(837)}`);
  52.  
  53. console.log("question8(35) == 44");
  54. console.assert(question8(35) == 44, `${question8(35)}`);
  55. console.log("question8(4000000) == 4613732");
  56. console.assert(question8(4000000) == 4613732, `${question8(4000000)}`);
  57. console.log("question8(2048) == 798");
  58. console.assert(question8(2048) == 798, `${question8(2048)}`);
  59. }
  60.  
  61. /*
  62. * Easy - You are on the ground step(0th step) of a staircase
  63. * consisting of N steps. You can step up 1 step at a time, or land above
  64. * the next step (This does not mean skipping 2 steps a.k.a skipping one step).
  65. * Climbing the stairs with each step whether being 1 step up or up the next step,
  66. * how many different ways can you climb the N steps?
  67. */
  68. // 0
  69. let question1 = (steps: number): number => {
  70. // if I'm currently on step N,
  71. // I either got here from the previous step: N - 1
  72. // or I got here by from the step below that: N - 2
  73. // so the result would be the sum of both possibilities:
  74. // (N - 1) + (N - 2)
  75. //If you squint, you'll realize that this is the fibonacci sequence
  76. //in disquise...
  77. return steps <= 1 ? steps :
  78. question1(steps - 1) + question1(steps - 2);
  79. }
  80.  
  81. /*
  82. * Easy - Find the sum of all digits in an int such that you get a single digit number.
  83. * Example:
  84. * 987 => 9+8+7 = 24 => 2+4 = 6
  85. */
  86. // 1
  87. let question2 = (x: number): number => {
  88. var s = x.toString();
  89. return s.length == 1 ? x : question2(
  90. Array.from(s).map(c => Number(c)).reduce((sum, next) => sum + next)
  91. );
  92. }
  93. /*
  94. * Easy - What is the distance between two 3D points in cartesian space?
  95. * Example:
  96. * P1 = (5,13,6)
  97. * P2 = (104,13,6)
  98. * distance(P1,P2) == 99
  99. *
  100. */
  101. let question3 = (x1: number, y1: number, z1: number, x2: number, y2: number, z2: number): number => {
  102. return Math.sqrt(Math.pow(x2 - x1,2) + Math.pow(y2 - y1, 2) + Math.pow(z2 - z1,2));
  103. }
  104.  
  105. /*
  106. * Medium
  107. *
  108. * If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9.
  109. * The sum of these multiples is 23.
  110. * Find the sum of all the multiples of 3 or 5 below the given number.
  111. */
  112. let question4 = (n: number): number => {
  113. let result = 0;
  114. for (let i = 1; i < n; i++) {
  115. if (((i % 3) == 0) || ((i % 5) == 0)) {
  116. result += i;
  117. }
  118. }
  119. return result;
  120. }
  121.  
  122. /*
  123. * Medium
  124. * Given a random integer between 0 and 9,999,999 inclusive, produce its exact English equivalent as a string.
  125. * Example:
  126. * 476 -> Four Hundred and Seventy Six
  127. */
  128. let question5 = (i: number): string => {
  129. var units = ["Zero","One","Two","Three","Four",
  130. "Five","Six","Seven","Eight","Nine","Ten",
  131. "Eleven","Twelve","Thirteen","Fourteen","Fifteen",
  132. "Sixteen","Seventeen","Eighteen","Nineteen"];
  133. var tens = ["","","Twenty","Thirty","Forty","Fifty",
  134. "Sixty","Seventy","Eighty","Ninety"];
  135.  
  136. return i < 20 ? units[i] :
  137. i < 100 ? tens[i/10] + ((i % 10 > 0)? " " + question5(i % 10):"") :
  138. i < 1000 ? units[i/100] + " Hundred" + ((i % 100 > 0)?" and " + question5(i % 100):"") :
  139. i < 1000000 ? question5(i / 1000) + " Thousand " + ((i % 1000 > 0)? "" + question5(i % 1000):"") :
  140. question5(i / 1000000) + " Million " + ((i % 1000000 > 0)? "" + question5(i % 1000000):"");
  141. }
  142.  
  143. /*
  144. * Easy
  145. * A pangram is a sentence that contains all the letters of the English alphabet at least once.
  146. * The most popular example is: "The quick brown fox jumps over the lazy dog"
  147. * Detect if a given string is a pangram
  148. */
  149. let question6 = (sentence: string): boolean => {
  150. var alphabet = "abcdefghijklmnopqrstuvwxyz";
  151. return Array.from(alphabet).every(sentence.toLowerCase().includes)
  152. }
  153.  
  154. /*
  155. * Medium
  156. * the first six prime numbers: 2, 3, 5, 7, 11, and 13
  157. * given an arbitrary integer, determine if it is prime
  158. */
  159. let question7 = (n: number): boolean => {
  160. if (n <= 1) {
  161. return false;
  162. }
  163.  
  164. if(n == 2){
  165. return true;
  166. }
  167.  
  168. if (n % 2 == 0) {
  169. return false;
  170. }
  171.  
  172. let counter = 3;
  173.  
  174. while ((counter * counter) <= n) {
  175. if (n % counter == 0) {
  176. return false;
  177. } else {
  178. counter +=2;
  179. }
  180. }
  181.  
  182. return true;
  183. }
  184.  
  185. /*
  186. Hard
  187. The fibonacci sequence is: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
  188. The next number is found by adding up the two numbers before it.
  189.  
  190. Find the sum of all the even-valued terms in the sequence which do not exceed a given limit.
  191.  
  192. Example
  193. question8(35) = 2 + 8 + 34 = 44
  194. */
  195.  
  196. let question8 = (limit: number): number => {
  197. let fib = [2,0];
  198. let i = 0;
  199. let result = 0;
  200.  
  201. while (fib[i] < limit) {
  202. result += fib[i];
  203. i = (i + 1) % 2;
  204. fib[i] = 4 * fib[(i + 1) % 2] + fib[i];
  205. }
  206. return result;
  207. }
Add Comment
Please, Sign In to add comment