Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 1.11
- // just keep track of 3 max and carefully compare them
- function thirdGreatest(xs) {
- // we use this trick to avoid null comparison
- // since we're looking for the max value, we can set
- // our variables to the value that is less than any other value
- // that way when we compare the first value in the array will
- // be greater than our variables
- // this avoids having to do an ugly null comparison
- // you can use this for all max value problems
- // if you are trying to find minimum of the array,
- // this would look like var first = Number.MAX_VALUE
- var first = Number.MIN_VALUE;
- var second = Number.MIN_VALUE;
- var third = Number.MIN_VALUE;
- for (var i = 0; i < xs.length; i++) {
- if (xs[i] > first) {
- // case xs[i] >= first >= second >= third
- // note the order here, we have to use "first"
- // value before assigning it
- second = first;
- third = second;
- first = xs[i];
- } else if (xs[i] > second) {
- // case first >= xs[i] >= second >= third
- third = second;
- second = xs[i];
- } else if (xs[i] > third) {
- // case first >= second >= xs[i] >= third
- third = xs[i];
- }
- // case first >= second >= third >= xs[i]
- // don't need to do anything do anything
- }
- }
- // 1.12
- // the idea here is similar to calculating max
- // of the array, but instead of comparing numbers
- // we can compare their frequencies
- function mostCommonLetter(s) {
- // keep track of letter with max frequency (count)
- var maxFreq = Number.MIN_VALUE;
- // and another variable for the actual letter
- var maxLetter = null;
- for (var i = 0; i < s.length; i++) {
- var count = freq(s[i], s);
- // standard trick for comparison
- // but note we're comparing count
- // you can think of this as a standard pattern
- // where you keep track of max / min but to calculate
- // what you're keeping track of you're using a helper
- // function
- if (count > maxFreq) {
- maxFreq = count;
- maxLetter = s[i];
- }
- }
- return [maxLetter, maxFreq];
- }
- // helper function for calculating frequency of LETTER
- // in a string S
- // this function literally corresponds to the problem's
- // statement that says "and a count of how many times it
- // appears"
- function freq(letter, s) {
- // this is a counting problem, similar to count vowels
- var count = 0;
- for (var i = 0; i < s.length; i++) {
- // note the pattern here, check if condition is true
- // and then increment COUNT
- // you've seen this before, it's a general pattern
- // for counting things that satisfy a certain condition
- if (s[i] == letter) {
- count++;
- }
- }
- return count;
- }
- // 1.13
- // similar pattern to "construct a string" type of problem
- // e.g. when removing vowels
- // but the condition here is a bit more complicated
- // again, literally from the description of the problem
- // "placing a single dash before and after each odd digit. There is one
- // exception: don't start or end the string with a dash."
- // this whole thing is going to go into our condition
- // tricky part is to not place double dashes
- // so we always need to place a dash before an odd number
- // but then do a check if the following number is even or end of string
- // to figure out if we need to place a dash in front of the number
- function dasherizeNumber(n) {
- // note that we're given a number N so we need
- // to convert it to string first
- var s = n.toString();
- var result = "";
- for (var i = 0; i < s.length; i++) {
- if (i === 0 || i === s.length-1 || // "don't start or end the string with a dash"
- 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")
- result += s[i];
- } else {
- // tricky part here is that if we have "03570" we want "0-3-5-7-0"
- // so we need to check if the following digit is even or end of number and only then place
- // the final dash
- result += "-" + s[i];
- if (s[i+1]%2 == 0 || i+1 == s.length-1) {
- result += "-";
- }
- }
- }
- return result;
- }
- // 1.14
- // here's another pattern, it's called roughly
- // split - do work - join
- // and it allows you to do stuff with words
- // the idea is to take a string "hello world i am ken"
- // then split it on spaces like ["hello", "world", "i", "am", "ken"]
- // then do stuff with each word in the array
- // then put it back together with .join(" ")
- function capitalizeWords(s) {
- var words = s.split(" "); // split
- for (var i = 0; i < words.length; i++) {
- // do work in the array
- // here we just re-assign data in the array directly
- words[i] = capWord(words[i]);
- }
- return words.join(" "); // join
- }
- // capitalize word helper function
- // directly corresponds to problem statement
- // "that capitalizes the first letter of
- // each word"
- function capWord(w) {
- return w[0].toUpperCase() + w.slice(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement