The next algorithm I work on was Roman to Integer. The basic premise is “Given a roman numeral, convert it to an integer”

The first step I needed to take was to be able to use the roman numeral as their integer counter parts. To make this happen I setup a map or a key setting each integer to the correct corresponding reman numeral.

we also need to be able to track a total number so I set a variable of total to 0

The next step was a bit tricky to figure out and took a bit of research. When combining roman numerals to make an integer the order we read the roman numerals matter. When we read left to right if the first numeral we read is bigger than the second we only add the first value to our total and move down the line to our next integer.

Input: s = “MCMXCIV”

Here is what I mean,

M is 1000 so 1000 is added to the total. Next numeral is a C. C is only 100. Since C is in front of an M, which is a higher value of 1000, we must subtract C from M getting 900 in this case. New total is 1900. Here is the weird part, since we just used C and M we include both in our movement and move to X. This is the movement pattern. This pattern was achieved using this.

This final step I had to add was to somehow know when to stop moving down the length of s. To do this I just added an if statement that just says if there is a next integer then continue the code else add currentInt to total and return the total.

Houston Flatiron student 2020