Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.90 KB | None | 0 0
  1. /* ===================================================================
  2. Even or Odd
  3. ====================================================================*/
  4. Answer: Constant Time - O(1)
  5. Reason: Function only takes on value and makes a simple if then comparison
  6. There are no extra function calls, loops, or anything to increase the complexity
  7.  
  8. function isEven(value){
  9. if (value % 2 == 0){
  10. return true;
  11. }
  12. else
  13. return false;
  14. }
  15.  
  16. /* ===================================================================
  17. Are You Here?
  18. ====================================================================*/
  19.  
  20. Answer: Polynomial Time - O(n^k)
  21. Reason: Function takes two arrays with n length, It initiates a for loop
  22. that looks at each item in the array. For each item in the first array,
  23. it initiates a second for loop that looks through each item in the second array
  24. If an item in the second array equals the item in the first array, it returns true
  25. Because this is a for loop nested inside a for loop, the run time increases polynomially
  26. with the lengths of each array.
  27.  
  28. function areYouHere(arr1, arr2) {
  29. for (let i=0; i<arr1.length; i++) {
  30. const el1 = arr1[i];
  31. for (let j=0; j<arr2.length; j++) {
  32. const el2 = arr2[j];
  33. if (el1 === el2) return true;
  34. }
  35. }
  36. return false;
  37. }
  38.  
  39.  
  40. /* ===================================================================
  41. Doubler
  42. ====================================================================*/
  43. Answer: Linear Time - O(n)
  44. Reason: Function takes an array and doubles each item in the array.
  45. The run time increases in a linear relation to the amount of items in the array
  46.  
  47. function doubleArrayValues(array) {
  48. for (let i=0; i<array.length; i++) {
  49. array[i] *= 2;
  50. }
  51. return array;
  52. }
  53.  
  54. /* ===================================================================
  55. Naive Search
  56. ====================================================================*/
  57. Answer: Linear - O(n)
  58. Reason: Function takes an array and looks for the item in the array,
  59. and returns the index of the item - essentially indexOf. This is linear because
  60. it goes through the array one time comparing the item with the current index. Best case
  61. scenario it only takes one try, worst case it takes the entire length of the array. Therefore,
  62. it is linear.
  63.  
  64. function naiveSearch(array, item) {
  65. for (let i=0; i<array.length; i++) {
  66. if (array[i] === item) {
  67. return i;
  68. }
  69. }
  70. }
  71.  
  72. /* ===================================================================
  73. Creating Pairs
  74. ====================================================================*/
  75. Answer: Polynomial - O(n^k)
  76. Reason: This function creates pairs for numbers by pairing each item in the array
  77. with every other item in the array at a larger index than it. Once it makes pair
  78. (array[0], array[1]), it does not make pair (array[1], array[0]) on it's pass through for index 1.
  79. Therefore the length of time in the second for loop is decreased with every pass through. That said,
  80. it loops through the array at least once for every index in the array. Therefore, it has a polynomial run time complexity.
  81.  
  82. function createPairs(arr) {
  83. for (let i = 0; i < arr.length; i++) {
  84. for(let j = i+1; j < arr.length; j++) {
  85. console.log(arr[i] + ", " + arr[j] );
  86. }
  87. }
  88. }
  89.  
  90.  
  91. /* ===================================================================
  92. Computing fibonaccis
  93. ====================================================================*/
  94. Answer: Linear - O(n)
  95. Reason: Function initiates one for loop that loops through every number
  96. up to and including the argument. Within the for loop are simple conditional statements,
  97. which have a constant run time complexity. The length of time it takes to complete this function
  98. is directly related to the size of the argument passed into the function e.g. it'll take 3 loops for
  99. num = 3, and 100 loops for num = 100
  100.  
  101. function generateFib(num) {
  102. let result = [];
  103. for (let i = 1; i <= num; i++) {
  104.  
  105. // we're adding the first item
  106. // to the result list, append the
  107. // number 0 to results
  108. if (i === 1) {
  109. result.push(0);
  110. }
  111. // ...and if it's the second item
  112. // append 1
  113. else if (i == 2) {
  114. result.push(1);
  115. }
  116.  
  117. // otherwise, sum the two previous result items, and append that value to results.
  118. else {
  119. result.push(result[i - 2] + result[i - 3]);
  120. }
  121. }
  122. // once the for loop finishes
  123. // we return `result`.
  124. return result;
  125. }
  126.  
  127.  
  128. /* ===================================================================
  129. An Efficient Search
  130. ====================================================================*/
  131. Answer: Logarithmic - O(log(n))
  132. Reason: This function calculates the index of the item argument
  133. in the array argument. It does this by comparing the midpoint of the array
  134. to the item, if it is greater it sets the min index to the midpoint plus one - if lesser, the max index
  135. to the mid point minus one. In this way, it cuts the problem size by half each time until it either returns
  136. the index of the item argument or returns -1.
  137.  
  138. function efficientSearch(array, item) {
  139. let minIndex = 0;
  140. let maxIndex = array.length - 1;
  141. let currentIndex;
  142. let currentElement;
  143.  
  144. while (minIndex <= maxIndex) {
  145. currentIndex = Math.floor((minIndex + maxIndex) / 2);
  146. currentElement = array[currentIndex];
  147.  
  148. if (currentElement < item) {
  149. minIndex = currentIndex + 1;
  150. }
  151. else if (currentElement > item) {
  152. maxIndex = currentIndex - 1;
  153. }
  154. else {
  155. return currentIndex;
  156. }
  157. }
  158. return -1;
  159. }
  160.  
  161. /* ===================================================================
  162. Random element
  163. ====================================================================*/
  164. Answer: Constant O(1)
  165. Reason: This function finds a random element in an array calculating a random
  166. number within the array length and returning the item at that index. Regardless
  167. of the array's length, it will return one number and require it to be called only once.
  168. Therfore its run time complexity is constant.
  169.  
  170. function findRandomElement(arr) {
  171. return arr[Math.floor(Math.random() * arr.length)];
  172. }
  173.  
  174.  
  175. /* ===================================================================
  176. Is it prime?
  177. ====================================================================*/
  178. Answer: Linear - O(n)
  179. Reason: This function divides the passed in number by every number between it and
  180. 2. Because it passes through every number once and only once, it is a linear relationship.
  181. I believe the time could be cut in half by comparing the numbers between 2 and the num/2 because
  182. if it isn't divisble by any number between two and it's half, then it won't be divisible by any number
  183. greater than it's half.
  184.  
  185. function isPrime(n) {
  186. // if n is less than 2 or a decimal, it's not prime
  187. if (n < 2 || n % 1 != 0) {
  188. return false;
  189. }
  190. // otherwise, check if `n` is divisible by any integer
  191. // between 2 and n.
  192. for (let i = 2; i < n; ++i) {
  193. if (n % i == 0) return false;
  194. }
  195. return true;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement