top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

Roman numbers to decimal converter tool with Python

This article explains how we can build a tool than helps converting a Roman number to its equivalent decimal representation. Lets first see what the function look like and then chunk it down into pieces and look at them in depth.


    def roman_to_int(s):
        romans_int = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
        int_val = 0
        for i in range(len(s)):
            if i > 0 and romans_int[s[i]] > romans_int[s[i - 1]]:
                int_val += romans_int[s[i]] - 2 * romans_int[s[i - 1]]
            else:
                int_val += romans_int[s[i]]
        return int_val

The function accepts as an argument the roman number representation and returns the decimal equivalent by computing it.


First, the roman numbers are defined in a python dictionary as a {key: value} pair. The value of a particular dictionary can be accessed as dict[key], where key represents the position of the element in the dictionary. The python range(n) function creates a collection of numbers on the fly, from 0 to n-1. Using the python dictionary data structure and the built-in python functions like range() and len() - a function that returns the length of a value, the above code implements the logic behind the calculation of an integer representation for a given roman number.


 
 
 

1 Comment


Data Insight
Data Insight
Sep 21, 2021

Absolutely no context and no explanation given!


Like

COURSES, PROGRAMS & CERTIFICATIONS

 

Advanced Business Analytics Specialization

Applied Data Science with Python (University of Michigan)

Data Analyst Professional Certificate (IBM)

Data Science Professional Certificate (IBM)

Data Science Specialization (John Hopkins University)

Data Science with Python Certification Training 

Data Scientist Career Path

Data Scientist Nano Degree Program

Data Scientist Program

Deep Learning Specialization

Machine Learning Course (Andrew Ng @ Stanford)

Machine Learning, Data Science and Deep Learning

Machine Learning Specialization (University of Washington)

Master Python for Data Science

Mathematics for Machine Learning (Imperial College London)

Programming with Python

Python for Everybody Specialization (University of Michigan)

Python Machine Learning Certification Training

Reinforcement Learning Specialization (University of Alberta)

Join our mailing list

Data Insight participates in affiliate programs and may sometimes get a commission through purchases made through our links without any additional cost to our visitors.

bottom of page