2 Ways to Distract Yourself While Doing the Hard Things

When doing something challenging, I've found that there are two ways that you can distract yourself that make the work that much easier.

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Roman Numeral Conversion Algorithm

One of the algorithms that I worked on earlier this week converts roman numerals into integers. Sometimes when I write a function for the sake of practice on sites like LeetCode.com or Hackerrank.com, I wonder if or when I would ever use it. However, as someone who is not from Ancient Rome, this function struck me as useful — in spite of my sense that I might have actually memorized the roman numeral system in less time than it took to write a function that would make that conversion for me. But I digress…

What was tricky about this problem was that there were a lot of conditions to articulate. It is undoubtedly the longest function I have written, which makes me suspect that there must be a much shorter solution to this problem, as is usually the case.

The first step I took was to split the string ‘s’ into an array and save it to another variable. If ‘s’ was “IV”, then the array would be [“I”, “V”]. Doing this allowed me to loop through each character and add up the numerals to convert them into an integer, saved to the variable ‘count.’ The first condition in the loop was to specify what to do if the item was not in the last index position. Within that condition, I went through each numeral to indicate how the count should change (or not change).

For ‘I’, if the numeral in the next position was ‘X’ then the count would go up by 9, or if it was ‘V’ then it would go up by 4. Otherwise, it would go up by one.

If the numeral was ‘X’, there would be a few possibilities. For each numeral, we basically have to teach the function to read the whole context, often including what comes before a character and what comes after it (if there is anything before or after — and specifying whether or not something comes before or after is another condition to spell out in some cases). For ‘X’, if the next character is ‘L’, then the count goes up by 40, and if it is C it goes up by 90. Then, if there is anything before ‘X’ and if what is before it is ‘I’, then that means the count will go up by 0. That is because if ‘I’ is before ‘X’ in the for loop, ‘IX’ has already been taken together and increased the count by 9 (as I spelled out in the last step). If I didn’t have this condition then when the loop made it to ‘X’ it would add another ten to the count. Finally, if none of these conditions were met, then the count would go up by ten.

If the character was ‘C’ and the next character was ‘D’, the count would go up by 400. If the next character was ‘M’, the count would increase by 900. But if the character before ‘C’ was ‘X’, then the count would not increase, because ‘XC’ is 90 and would have already been taken into account by a previous condition. If none of these conditions were met, then the count would go up by 100.

For the character ‘V’, if there was a numeral before it and if that numeral was ‘I’, then the count would not go up, because ‘IV’ would have already been calculated. Otherwise, it would increase by 5.

The logic for ‘L’ is the same as that for ‘V.’ If it is preceded by ‘X’ the count stays the same, because other than being a t-shirt size ‘XL’ also means 40. If nothing comes before it or what does come before it is not ‘X’ then 50 is added to the count.

The condition for ‘D’ also follows this pattern of reading only the numeral before it and not the one after it. If what is before it is ‘C’, then the count does not go up, because ‘CD’ is 400— and that would have already been added to the count by the time the loop made it to the D. Otherwise, the count would go up by 500.

Lastly, if the numeral was ‘M’ and there was either nothing before it or what was before it was not ‘C’, the count would go up by 1000. If what came before it was ‘M’, the count would not go up, because that would have been 900 and would have already been added.

If you are still awake, I will now go ahead and go through the next part of the function that accounts for the case in which the loop is at the last index position of the array.

Now that you are awake…

If ‘I’ is the last numeral in the array, then 1 is simply added to the count. Easy!

If ‘V’ is the last numeral, then the logic is actually the same as if it was in any other position in the array. Which means, this is an opportunity for refactoring!!! I will give you a couple of minutes to go work on that. Meet me back here when you are done.

Great, thank you for coming back. Now, if ‘L’ is the last numeral in the array, then the logic is ALSO the same as it would be for any other position in the array. WAIT A MINUTE! I am sensing a pattern here. There is a lot of repetitive code. That is not D.R.Y.!

Hmm…I am going to go refactor. Have a nice Thursday!

***UPDATE: I refactored the code above to get the following. There is undoubtedly still room for improvement, but this is what I have so far:

Add a comment

Related posts:

The Coronavirus Conspiracy Boom

COVID-19 has created a perfect storm for conspiracy theorists. Here we have a global pandemic, a crashing economy, social isolation, and restrictive government policies: All of these can cause…

Our Main Picks Are Hitting Bullseyes

If you google my name, you will see that I am known as a market wizard in stocks based on my KPMG verified track record as well as my numerous publications in books (Wiley & Sons is my main…

Significance of Jupiter in Vedic Astrology

Jupiter is the fifth planet in the solar system. The stripes and swirl that Jupiter displays are really iconic & beautiful. These stripes and swirls are nothing but the clouds made up of water and…