www.fatihkabakci.com

Personal Website and Computer Science TUR EN

Interview Question Roman to Integer

Last update: 12/27/2016 4:23:00 AM

By Fatih KABAKCI

The problem is to convert a roman numeral to an integer value.

Problem #13 - Roman to Integer

Description: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.

Difficulty: Easy

Solution in Java

Reference Roman Table

http://www.mathatube.com/tables-charts-roman-numeral-chart-html.html

It is useful to remember the following simple rule before starting to solve this problem. A roman numeral can be calculated by adding or subtracting the numeric values of the characters each other by comparing their values. The rule is basically if the value of the character is greater than the value of the next character, the total sum variable is added with the value of the character. Otherwise the subtraction operation is performed.

For example,

So, we can implement this rule as below.

package com.fatihkabakci.Easy.RomanToInteger;

/**
 * 
 * @author fkabakci
 * 
 * Problem Description: Given a roman numeral, convert it to an integer.
 * Input is guaranteed to be within the range from 1 to 3999.
 * 
 * Solution:
 * 
 * Postcondition: num is within the range from 1 to 3999
 * 
 * 1. Compare adjacent two characters in regarding to the following rule.
 * 2. If the roman value of a character is greater than the value of another character which is right of that,
 *    	Total sum is added with the value of the first character.
 *    otherwise,
 *    	Total sum is subtracted by the value of the first character.
 * 3. Go back to 2 until all characters are visited.
 * 
 * Example: 
 * 
 * Roman characters: MCMXCVI
 * 
 * Their values: 1000 100 1000 10 100 5 1
 * 
 * 1. Start comparing with 1000 and 100, because of the comparison 1000 > 100 is true, add total sum with 1000, total = 1000
 * 2. Then 100 and 1000. 100 < 1000, subtract total sum by 100, total = 900
 * 3. Now is 1000 and 10. 1000 > 10, adding total sum with 1000, total = 1900
 * 4. 10 and 100. 10 < 100, again subtracting total sum by 10, total = 1890
 * 5. 100 and 5. 100 > 5, adding total sum with 100, total = 1990
 * 6. 5 and 1. 5 > 1, adding total sum with 5, total = 1995,
 * 7. Lastly, 1 and 0 because there is no more character left. 1 > 0, adding sum with 1 more, total = 1996.
 *    
 */
public class RomanToInteger {	
	private int getValue(char c) {
		switch (c) {
		case "M":
			return 1000;
		case "D":
			return 500;
		case "C":
			return 100;
		case "L":
			return 50;
		case "X":
			return 10;
		case "V":
			return 5;
		case "I":
			return 1;
		default:
			return 0;
		}
	}

	public int romanToInt(String s) {
		int acc = 0, i = 0, l = s.length();
		while (i < l) {
			char c = s.charAt(i);
			char csub = (i + 1 < s.length()) ? s.charAt(i + 1) : "\0";
			int v1 = getValue(c);
			int v2 = getValue(csub);
			if (v1 < v2)
				acc = acc - v1;
			else
				acc = acc + v1;
			i = i + 1;
		}
		return acc;
	}

	public static void main(String[] args) {
		RomanToInteger rti = new RomanToInteger();
		System.out.println(rti.romanToInt("MCMXCVI"));
	}
}

Similar Problems

#12 Integer To Roman

There has been no comment yet

Name:


Question/Comment
   Please verify the image




The Topics in Computer Science

Search this site for





 

Software & Algorithms

icon

In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.

Programming Languages

icon

A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. It can be used to create programs to control the behavior of a machine. Java,C, C++,C#

Database

icon

A database is an organized collection of data. The data are typically organized to model aspects of reality in a way that supports processes requiring information.

Hardware

icon

Computer hardware is the collection of physical elements that constitutes a computer system. Computer hardware refers to the physical parts or components of a computer such as the monitor, memory, cpu.

Web Technologies

icon

Web development is a broad term for the work involved in developing a web site for the Internet or an intranet. Html,Css,JavaScript,ASP.Net,PHP are one of the most popular technologies. J2EE,Spring Boot, Servlet, JSP,JSF, ASP

Mobile Technologies

icon

Mobile application development is the process by which application software is developed for low-power handheld devices, such as personal digital assistants, enterprise digital assistants or mobile phones. J2ME

Network

icon

A computer network or data network is a telecommunications network that allows computers to exchange data. In computer networks, networked computing devices pass data to each other along data connections.

Operating Systems

icon

An operating system is software that manages computer hardware and software resources and provides common services for computer programs. The OS is an essential component of the system software in a computer system. Linux,Windows

Computer Science

icon

Computer science is the scientific and practical approach to computation and its applications.A computer scientist specializes in the theory of computation and the design of computational systems.