Skip to content

Instantly share code, notes, and snippets.

@armw4
Last active May 18, 2016 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save armw4/b9c90ac6139e9bcd36d4c07c66e5399a to your computer and use it in GitHub Desktop.
Save armw4/b9c90ac6139e9bcd36d4c07c66e5399a to your computer and use it in GitHub Desktop.
Question 5/6 of my Latest Programming Assessment

I only had 45 minutes in total to finish the entire assessment, but this was question 5/6. I ran out of time before submitting my final answer (which I'm disappointed in). Once the challenge was over, I thought more about the solution to the problem and was able to figure it out. What made the challenge more tricky is that the input was coming in from standard input and being buffered via the data event. Seeing this in node just kinda freaks you out when you're racing against time. All you can think is 😳. The authors of the assessment were kind enough to register the required event handlers for accumalating the input, and even provided the location of where to act on the final, fully buffered version of the input (I believe this was the 'end' event). Yes, stdin is a stream in node, and as a result, an event emitter. I'd still have run out of time even if I were able to fully derive my solution, as this took me at least 20 minutes of heuristics with a clear head, after the fact. That's pretty shocking. It feels like the only way you'd have gotten past this part is if you'd implemented something of similar nature recently. It can be hard flipping that switch from lodash, underscore, and native enumerable functionality provided to you out of the box, to "here....write this for loop from scratch and be those guys". So....practice and get back in that groove; else you'll be caught looking 😳 like I did.

Challenge

Find the number that's closest to the average of all the numbers.

Input

  • You've got a string coming in from stdin
  • first value is the number of numbers
  • remainder of input is a new line followed by the numbers
  • IIRC, when numbers and equally close to the average, the lastone wins

Since 3 and 5 are both 1 unit away from 4, 5 would win since it comes after 3 in our input.

The Solution

Right when my exam was automatically submitted due to the final buzzer sounding, I asked myself was I on the right track. I was, but I definitely would not have been able to derive the rest of the solution within a sufficient amount of time. I was too flustered and the waning seconds were choking me out mentally. Nonetheless, I was on my way, but I wasn't satisfied with the state of my half way done solution. So I paced around the living room deep in thought, anxiously awaiting my Eureka moment. Then it came to me. The solution was right there on the tip of my neural finger tips. I got it....absolute value for the win:

// CHALLENGE:
//
// find the number that's closest to the average of all the numbers
//
// INPUT:
//
// you've got a string coming in from stdin
// first value is the number of numbers
// remainder of input is a new line followed by the numbers
// IIRC, when numbers and equally close to the average, the *last*
// one wins. so 3 and 5 are both 1 away from 4, 5 would win since it comes
// after 3 in our input
var input = '6 \n12 2 99 4 5 6'
var parsedInput = input.split(/\n|\s/) // [ '6', '', '12', '2', '99', '4', '5', '6' ]
var min, closest
// it'd be nice if we could maintain a running average, but *no*.
// skip the count at index 0, as it's worthless, and the empty entry at index 1
var numbers = parsedInput.splice(2)
var sum = numbers.reduce(function(total, current) {
  return total + parseInt(current)
}, 0)
var average = Math.floor(sum / numbers.length)

for (var i = 0; i < numbers.length; i++) {
  var value = parseInt(numbers[i])
  var distanceFromAverage = Math.abs(value - average)

  if (!min || distanceFromAverage < min) {
    min = distanceFromAverage
    closest = value
  }
}

// value closest to average is 12, because it is only 9 units from the number 21
console.log('value closest to average is %d, because it is only %d units from the number %d',
  closest, min, average)

And there we have it. I was able to successfully answer 7/9 (C grade), but I'm still kicking myself for not getting past those last two questions on my first assessment. Good experience, though. Just have to be better next time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment