CoDEVIANT #27 — Goldilocks, Kit-Kat Bars, & the Three Integers

Did you know that integers can swallow children whole? They also roam national parkgrounds and swipe peoples’ pic-nic baskets.

Just pulling the ol’ 1,2,3,4,5,6,7,8,9….y’know the rest

Well today we’re going to solve a neat little problem. I have recently discovered my developer super power. As an extremely visual person who suffers from not always having a largess of working memory, I find it helpful to draw pictures. Seriously, little hieroglyphics representing images of what I want to achieve in steps really makes a huge difference for me. I feel like my mind is freed up to focus on each step of a problem without having to actively focus on keeping the last thing in my head from getting mixed up.

Now I’m thinking of “Take on Me” by A-ha.

The problem we are solving today is to write a function that accepts an array of integers as an argument. The function must return an array containing the three largest integers in the array, in ascending order, without using the sort method on the given array.

So here is how I solved that sucker.

So there’s a little bit going on here, it requires two helper methods. Let’s break it down shall we? First we’ll take a look at the main method:

We first create a new array for the variable piece by getting the first three elements of the given array and cutting them away from the given array argument.

We’re literally doing this to the ‘array’ argument.

Next, we call the method doTheHustle on the piece array we just made. We’ll get into the implementation later, but what this method does is sort the three elements of the piece array by checking to see if the first element is larger than the second element and then re-setting the elements if so (it then does the same with the 2nd and 3rd elements).

Next we use a for…in statement on the array argument (which, do recall has lost its first three members). We say that “for every index in array, do the following”. The statement takes the integer at the array[index] position and the the piece array and sets them as arguments in the goldilocks method. Like the doTheHustle method, we’ll get into the implementation of that in a bit, but to be succint, the goldilocks method checks each of the existing elements in piece and if the new integer passed in is larger than any of the elements in piece temporary variables are created to facilitate the shifting of the elements in piece.

Finally, we return piece.

Okay so before we can call it a day and go home, it’s important that we understand what’s going on with doTheHustle and goldilocks.

I don’t know what’s up with me and why I’m invoking fucking disco. *shrugs*

Sigh…let’s doTheHustle shall we?

Basically we have a for loop that does the code-block as long as i is less than or equal to the length of piece minus 1. Since our piece will only ever be 3 elements long, this means that we will be managing the indices 0, 1, and 2. (Cuz y’know how arrays are zero-indexed).

So inside the for-loop we say that: “If the number at piece[i] is larger than the following number in piece (which lives at piece[i + 1]), then we:

  • Create a variable called tmp which holds the current value of piece[i]
  • Set piece[i] to equal piece[i + 1]
  • Set piece[i + 1] to equal whatever value is stored in tmp

It’s important to note that in doTheHustle, no elements are discarded from whatever the line-up of integers is inside of piece. That action takes place in goldilocks method.

Right off the top, I call doTheHustle because I want to make sure that everything is sorted so that the functionality of this method yields the appropriate result.

Then we go through a series of if statements that compare the value of the el integer from the for…in statement against the elements in piece.

If el is larger than the last element of piece → piece[2]:

  • We create a variable b to contain piece[2]
  • We create a variable a to contain piece[1]
  • We set piece[2] to equal el
  • We set piece[1] to equal b
  • We set piece[0] to equal a
  • Then we return

If el is larger than the second element of piece → piece[1]:

  • We create variable a to equal piece[1]
  • We set piece[1] to equal el
  • We set piece[0] to equal a
  • Then we return

If el is larger than the first element of piece → piece[0]:

  • We set piece[0] to equal el
  • Then we return

There’s no need to mess around with anything else!

And as they say in showbiz and the mummifying business…

to be clear, not a WAP…totally different content, go check out my non-existent OnlyFans for that XD



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store