Git Product home page Git Product logo

romanconverter's Introduction

RomanConverter

A simple project to convert Roman numerals to Arabic numerals and vice versa

Roman-to-int Approach:

I started by writing tests to check if the sickliest input such as "I" or "V" would give me the correct output. Therefore, based on the this theory I wrote a simple switch statement based on the seven symbols which are used to create complex numerals. I then unit tested few examples in order to make sure that the approach which I had taken would provide me with the correct result.

Once I had finished working on the switch statement I worked on creating an algorithm to convert roman numerals into integers. I decided to loop the every letter in the String which is being inputted by the user expect the last character since its not dividable by 10.

For each letter I checked if the current character's equivalent numeric value is less than the next character's numeric value. Based on these assumptions I took either of the following decisions:

  • If its less than the next character's value than it is subtracted from the final result.
  • If its greater than the next character's value then it is added on to the final result.

Once I had loop through every character I went back to the last character to find the equivalent numeric value and added it to the result.

Int-to-roman Approach:

I started by writing a simple test to see if the number being passed by the user is between 1 and 3999. If the number is less than or greater than the numbers specified in the requirements document it will throw and IllegalArgumentException.

I then initialised two arrays (int and String). The int array with numbers ranging from 1000-1 and its corresponding roman value in a String array. I then created a nested loop to go through the integer array and check if the user's number was greater than 1000 (the highest possible roman numeral.) Inside the nested the loop, I appended the equivalent numeral to the final result. I then subtracted the value first the int array eg. 1000, from the input.

romanconverter's People

Contributors

saurabhkjain avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.