Advertisement
Guest User

Untitled

a guest
Jul 15th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 1.11
  2. // just keep track of 3 max and carefully compare them
  3. function thirdGreatest(xs) {
  4.   // we use this trick to avoid null comparison
  5.   // since we're looking for the max value, we can set
  6.   // our variables to the value that is less than any other value
  7.   // that way when we compare the first value in the array will
  8.   // be greater than our variables
  9.   // this avoids having to do an ugly null comparison
  10.   // you can use this for all max value problems
  11.   // if you are trying to find minimum of the array,
  12.   // this would look like var first = Number.MAX_VALUE
  13.   var first = Number.MIN_VALUE;
  14.   var second = Number.MIN_VALUE;
  15.   var third = Number.MIN_VALUE;
  16.   for (var i = 0; i < xs.length; i++) {
  17.     if (xs[i] > first) {
  18.       // case xs[i] >= first >= second >= third
  19.       // note the order here, we have to use "first"
  20.       // value before assigning it
  21.       second = first;
  22.       third = second;
  23.       first = xs[i];
  24.     } else if (xs[i] > second) {
  25.       // case first >= xs[i] >= second >= third
  26.       third = second;
  27.       second = xs[i];
  28.     } else if (xs[i] > third) {
  29.       // case first >= second >= xs[i] >= third
  30.       third = xs[i];
  31.     }
  32.     // case first >= second >= third >= xs[i]
  33.     // don't need to do anything do anything
  34.   }
  35. }
  36.  
  37. // 1.12
  38. // the idea here is similar to calculating max
  39. // of the array, but instead of comparing numbers
  40. // we can compare their frequencies
  41. function mostCommonLetter(s) {
  42.   // keep track of letter with max frequency (count)
  43.   var maxFreq = Number.MIN_VALUE;
  44.   // and another variable for the actual letter
  45.   var maxLetter = null;
  46.   for (var i = 0; i < s.length; i++) {
  47.     var count = freq(s[i], s);
  48.     // standard trick for comparison
  49.     // but note we're comparing count
  50.     // you can think of this as a standard pattern
  51.     // where you keep track of max / min but to calculate
  52.     // what you're keeping track of you're using a helper
  53.     // function
  54.     if (count > maxFreq) {
  55.       maxFreq = count;
  56.       maxLetter = s[i];
  57.     }
  58.   }
  59.   return [maxLetter, maxFreq];
  60. }
  61.  
  62. // helper function for calculating frequency of LETTER
  63. // in a string S
  64. // this function literally corresponds to the problem's
  65. // statement that says "and a count of how many times it
  66. // appears"
  67. function freq(letter, s) {
  68.   // this is a counting problem, similar to count vowels
  69.   var count = 0;
  70.   for (var i = 0; i < s.length; i++) {
  71.     // note the pattern here, check if condition is true
  72.     // and then increment COUNT
  73.     // you've seen this before, it's a general pattern
  74.     // for counting things that satisfy a certain condition
  75.     if (s[i] == letter) {
  76.       count++;
  77.     }
  78.   }
  79.   return count;
  80. }
  81.  
  82. // 1.13
  83. // similar pattern to "construct a string" type of problem
  84. // e.g. when removing vowels
  85. // but the condition here is a bit more complicated
  86. // again, literally from the description of the problem
  87. // "placing a single dash before and after each odd digit. There is one
  88. // exception: don't start or end the string with a dash."
  89. // this whole thing is going to go into our condition
  90. // tricky part is to not place double dashes
  91. // so we always need to place a dash before an odd number
  92. // but then do a check if the following number is even or end of string
  93. // to figure out if we need to place a dash in front of the number
  94. function dasherizeNumber(n) {
  95.   // note that we're given a number N so we need
  96.   // to convert it to string first
  97.   var s = n.toString();
  98.   var result = "";
  99.   for (var i = 0; i < s.length; i++) {
  100.     if (i === 0 || i === s.length-1 || // "don't start or end the string with a dash"
  101.         s[i]%2 == 0) { // don't place dashes around even digits (inverse logical statement of "placing a single dash before and after each odd digit")
  102.       result += s[i];
  103.     } else {
  104.       // tricky part here is that if we have "03570" we want "0-3-5-7-0"
  105.       // so we need to check if the following digit is even or end of number and only then place
  106.       // the final dash
  107.       result += "-" + s[i];
  108.       if (s[i+1]%2 == 0 || i+1 == s.length-1) {
  109.         result += "-";
  110.       }
  111.     }
  112.   }
  113.   return result;
  114. }
  115.  
  116. // 1.14
  117. // here's another pattern, it's called roughly
  118. // split - do work - join
  119. // and it allows you to do stuff with words
  120. // the idea is to take a string "hello world i am ken"
  121. // then split it on spaces like ["hello", "world", "i", "am", "ken"]
  122. // then do stuff with each word in the array
  123. // then put it back together with .join(" ")
  124. function capitalizeWords(s) {
  125.   var words = s.split(" "); // split
  126.   for (var i = 0; i < words.length; i++) {
  127.     // do work in the array
  128.     // here we just re-assign data in the array directly
  129.     words[i] = capWord(words[i]);
  130.   }
  131.   return words.join(" "); // join
  132. }
  133.  
  134. // capitalize word helper function
  135. // directly corresponds to problem statement
  136. // "that capitalizes the first letter of
  137. // each word"
  138. function capWord(w) {
  139.   return w[0].toUpperCase() + w.slice(1);
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement