CoDEVIANT #4 (3/24/19)

Ok…back at this shit again. Of all the habits I have, this seems to at least be healthier than most.

Me, this past weekend.

Today I’m starting off with a math based one. It’s not something I chose on purpose…I just hit random when I go to CodeWars. Typically, I’d run away from math based ones…but I’m going to try thugging it out. Plus at least this one doesn’t seem to deal with any concepts I’ve never heard of before, so that’s a plus.

Buying a Car


A man has a rather old car being worth $2000. He saw a secondhand car being worth $8000. He wants to keep his old car until he can buy the secondhand one.

He thinks he can save $1000 each month but the prices of his old car and of the new one decrease of 1.5 percent per month. Furthermore this percent of loss increases by 0.5 percent at the end of every two months. Our man finds it difficult to make all these calculations.

Can you help him?

How many months will it take him to save up enough money to buy the car he wants, and how much money will he have left over?

Parameters and return of function:


Selling, buying and saving are normally done at end of month. Calculations are processed at the end of each considered month but if, by chance from the start, the value of the old car is bigger than the value of the new one or equal there is no saving to be made, no need to wait so he can at the beginning of the month buy the new car:

We don’t take care of a deposit of savings in a bank:-)

How Adrian will attempt/solve this problem:

Real talk…I don’t always solve it first and then give a presentation. I almost always laboriously detail my thought process as I do it while jumping on and off stage for opera rehearsals. I may not, and likely won’t, be able to figure this out. But I’m going to try my level best.

What we are given to work with:

Our function is supposed to be able to pass tests like this:

What this means is that our guy in the problem would be able to buy the new car in 6 months with 766 dollars left over.

Or that in another reality, he could buy it immediately with 4000 dollars left over.

Adrian’s Observations:

And you know what? This almost works.

So close…yet so far…

Turns out that the writing of the test was a little misleading. We get directions that the man thinks he can save $1000 a month. However, the function we are tasked with filling out includes an argument that dictates how much the man will save per month:


So if I change this line:


then we win!


Adrian’s Solution:

Let’s break it down:

  • dough represents the money the man has on hand PLUS the value of his old car.
  • month represents the amount of months that have passed since homeboy began saving up for his new whip.
  • While dough is less than the price of the new car:
  • month increases by one
  • dough increases by the value represented by the savingperMonth argument that is passed into the function.
  • We use a modulus operator to figure out if we are dealing with every second month or just another regular month, because each second month has different effects on the money flow of our prospective car buyer.
*adjusts glasses*


Let me explain. When you divide 7/3, you’re going to get a remainder with that. If you just wanted to get the remainder of a given set of numbers being divided, you would express it like this:

In our solution, we see if we get a remainder of any kind when we divide by 2…if we do not (if it equals zero), then we know we are dealing with a second month, which impacts the man’s money situation a little bit differently.

Back to the solution:

If we are in a second month:

  • We increase the amount of percentage lost per month by 0.5
  • The new car’s price [startPriceNew] equals itself minus (itself * the percentage loss each month)
  • dough equals itself minus the (old car’s value * the percentage loss each month)
  • The old car’s value also becomes itself minus its value multiplied by the percentage loss each month.
  • If we are in a regular month:
  • The exact same stuff happens EXCEPT that the percentage lost per month value remains the same.
  • Once dough is finally more than or equal to whatever the price of the new car happens to be:
  • we return an array with the month value at the first index location, and the rounded difference of the dough subtracted from the price of the new car at the second index location.

How do n̵e̵r̵d̵s̵ really good programmers do it?

Hm…okay, so I see a lot of similarities (not to toot my own horn…too much 😉 )

  • Our dough variable and the others’ moneySaved variable serve the exact same purpose.
  • Great minds do think alike.
  • We use a while loop, although the direction of the compared values was reversed in mine.
  • The others’ also used similar lines to mine to change the value of dough/moneySaved, startPriceOld, & startPriceNew
  • months get augmented LATER in the process, which is why the others’ solution seeks for a remainder of 1 when using the modulus operator between the months value and 2.
  • And like ours it is at that point that the percentLossByMonth value augments by 0.5.
  • Then similarly to ours, we return an array with months as the first value, and the various values of the man’s wealth with the new car price subtracted; the product of which is then rounded.

I think what I could have done was employ what a̵n̵a̵l̵ ̵r̵e̵t̵e̵n̵t̵i̵v̵e̵ ̵c̵a̵l̵c̵u̵l̵a̵t̵o̵r̵s̵ ̵w̵i̵t̵h̵ ̵h̵a̵i̵r̵ ̵c̵u̵t̵s̵ ̵&̵ ̵c̵i̵r̵c̵u̵l̵a̵t̵o̵r̵y̵ ̵s̵y̵s̵t̵e̵m̵s̵ good programmers refer to as the D.R.Y. Principle. I think of dirty things & loneliness immediately, but what it means is Don’t Repeat Yourself; as in, don’t rewrite code needlessly.

If we take a look at my solution, I repeat like three lines for the two cases in my switch statement:

I could easily reformat my solution to look like this and still work.

Adrian’s Slightly Revised Solution:

We maintain the placement of actions happening after a judgement has been evaluated regarding our switch statement, so we put all the sensitive operations to help with finding the money amount after the switch statement. 🙂

And that solution also works.


Buy me this take out…please?

You should strive to avoid repeating yourself. So often in coding, the difference between shippable code or at least functional code is a small typo or a stray character that aint supposed to be there. When you have one pristine instance of a particular block of code that is vital to the operation of what you are trying to do it is preferable to refer to it somehow either with a separate function or the importing of a module you’ve created elsewhere in your file structure, whatever…

  • Other than that…scary math problems can be okay. 🙂
  • Sometimes directions are wrong, we were told to expect a $1000 increase each month but our actual function gave us something different. Be sure to be very skeptical and thorough about what you’re handed to work with. Sometimes people will cut corners and leave just enough out of a description to put you on a wild goose chase. It’s like being given a video game without a controller…

That’s kind of it for today. I may be able to come back and edit this post and add another problem, but I’ve got plans and rehearsals as well as a few projects to tinker with. Maybe I’ll make a special post about something related to a project I’m working on.



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