SubSequencing Arrays: “The Awards Ceremony”, and… a Few Words About Learning

Awesome coding challenge yesterday with fellow dev Ranika Williams from Flatiron School. Though I’ve been attempting to substitute values in code such as “index, idx, val” with more descriptive values and console logs, working through the problem via Zoom with Ranika gave me a new appreciation for the approach.

If you’re attempting to tackle algorithms, you may benefit from attempting to “personify” or “characterize” what’s happening. The reason is, it will be easier for you to keep track of the multiple variables, what IS happening, and what is supposed to happen.

You will see in lots of learning materials around the internet, that analogies are employed to help make code more tangible. NOTE: if the analogy is lousy, it will add another layer of difficulty as opposed to clarity. The analogy will almost always be easier than the code, so if the analogy is wrong, that’s why the code will be more obscure.

That said, don’t worry about finding the perfect analogy… but do not be so “attached” to an analogy that you don’t want to change it once you’ve set upon it. For the following code, we originally started off with a classroom analogy about students, cool kids, and friends. Once we worked through to the end, we realized that as the order of the subsequence and original array mattered… we swapped out the student analogy for one of contestants and medalistsBronzeSilverGold.

Some TIPS for making your own analogy:
– be familiar, concrete, AND specific when choosing the PARTS of your analogy. Opt to choose something that HAS ONE AND ONLY ONE clear definition, especially if it’s familiar (but in actuality vague)… To be specific: you will find lots of analogies that employ things like music players. While music players are familiar, this can be a terrible mistake unless you have a specific service or device in mind: the reason? Just think of your own experience using iTunes, amazon music, spotify, Pandora, LiveFM, or otherwise. Every other music service catalogs and categorizes differently meaning your analogy has a good chance of getting fuzzy. REMEMBER: The whole point of coming up with an analogy is to reduce complexity, not to increase it, that’s why “the gold standard” of the analogy is something that has one and only one meaning.
– be descriptive (instead of something like “for student in students” aim for something more like “lonelyNerd in students“)
– use console logs to “tell a story” and blank console logs to delineate space so you can tell when one loop begins and another ends.
– after adapting code to an analogy… see if you can retell the analogy in plain English without looking at the code, and possibly to someone that doesn’t even know how to code. This will reinforce the analogy.
– treat the analogies almost as “code smells” so when encountering an unfamiliar problem, you can recall your analogy and determine quickly how it might be modified.

DESCRIPTIVE EXAMPLE, slightly embellished from the coding session for the purpose of this article. Make sure to spin up a repl, or playcode.io to test. Note: the example is in Javascript.

function approveAwardCeremonyLineUp(racersFinishedInThisOrder, proposedChampionLineUp) {
  
  if (arguments.length === 2) {
  
    let featuredChampion = 0;
    
    for (let runnerUp = 0; runnerUp < racersFinishedInThisOrder.length; runnerUp++) {
      console.log(" ")
      console.log("We look at all the racers...")
      console.log("The first argument: ", racersFinishedInThisOrder)
      console.log("The second argument: ", proposedChampionLineUp)
        if (featuredChampion === proposedChampionLineUp.length) {
          console.log("...we've come to the end of our proposedChampionLineUp");
          break;
        } 
        if (proposedChampionLineUp[featuredChampion] === racersFinishedInThisOrder[runnerUp] ) {
          console.log("checking to make sure that the runner up", racersFinishedInThisOrder[runnerUp], "is also the featured champion", proposedChampionLineUp[featuredChampion])
          featuredChampion++;
        }
        console.log("")
        console.log(`---------end of loop ${runnerUp} ------------`)
    }
      
    return (
      console.log("the list is approved?", (featuredChampion === proposedChampionLineUp.length)),
      featuredChampion === proposedChampionLineUp.length
    );
  } else {
    return console.log("wrong number of arguments")
  }
}

The console returns is as follows:

approveAwardCeremonyLineUp(["Ellen", "Mike", "Jay", "Kara", "Sydney", "Holly", "Alvin"],  ["Jay", "Sydney", "Holly"])

// the above will return what's below...

We look at all the racers...
The first argument: 
(7) [
"Ellen",
"Mike",
"Jay",
"Kara",
"Sydney",
"Holly",
"Alvin"
]
The second argument: 
(3) [
"Jay",
"Sydney",
"Holly"
]
---------end of loop 0 ------------
 
We look at all the racers...
The first argument: 
(7) [
"Ellen",
"Mike",
"Jay",
"Kara",
"Sydney",
"Holly",
"Alvin"
]
The second argument: 
(3) [
"Jay",
"Sydney",
"Holly"
]
---------end of loop 1 ------------
 
We look at all the racers...
The first argument: 
(7) [
"Ellen",
"Mike",
"Jay",
"Kara",
"Sydney",
"Holly",
"Alvin"
]
The second argument: 
(3) [
"Jay",
"Sydney",
"Holly"
]
checking to make sure that the runner upJayis also the featured championJay
---------end of loop 2 ------------
 
We look at all the racers...
The first argument: 
(7) [
"Ellen",
"Mike",
"Jay",
"Kara",
"Sydney",
"Holly",
"Alvin"
]
The second argument: 
(3) [
"Jay",
"Sydney",
"Holly"
]
---------end of loop 3 ------------
 
We look at all the racers...
The first argument: 
(7) [
"Ellen",
"Mike",
"Jay",
"Kara",
"Sydney",
"Holly",
"Alvin"
]
The second argument: 
(3) [
"Jay",
"Sydney",
"Holly"
]
checking to make sure that the runner upSydneyis also the featured championSydney
---------end of loop 4 ------------
 
We look at all the racers...
The first argument: 
(7) [
"Ellen",
"Mike",
"Jay",
"Kara",
"Sydney",
"Holly",
"Alvin"
]
The second argument: 
(3) [
"Jay",
"Sydney",
"Holly"
]
checking to make sure that the runner upHollyis also the featured championHolly
---------end of loop 5 ------------
 
We look at all the racers...
The first argument: 
(7) [
"Ellen",
"Mike",
"Jay",
"Kara",
"Sydney",
"Holly",
"Alvin"
]
The second argument: 
(3) [
"Jay",
"Sydney",
"Holly"
]
...we've come to the end of our proposedChampionLineUp
the list is approved?true
 

Discover more from Comedy Tragedy Epic

Subscribe now to keep reading and get access to the full archive.

Continue reading