# [Challenge] Integer to Roman Numerals

• (2 Pages)
• 1
• 2

## 29 Replies - 7586 Views - Last Post: 15 April 2015 - 10:58 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=373827&amp;s=c9d6f26e44deb1bc7d7d207199a2b9ae&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 atraub

• Pythoneer

Reputation: 828
• Posts: 2,235
• Joined: 23-December 08

# [Challenge] Integer to Roman Numerals

Posted 08 April 2015 - 12:12 PM

Today's challenge is simple, yet can provide some interesting results. I'm sure the vast majority of you already know what Roman Numerals are. Whether you've seen them on the face of clocks or in the title of your favorite video game, they're still a part of the modern world despite their age. (For those needing a refresher: wikipedia)

To complete this challenge, you'll need to make a function that takes an integer as input and returns a string with the roman numeral representation of that value. This chart may prove useful for checking your work.

When creating your function, there are different approaches you may want to consider:

Will your function have a simplistic elegance so that even a non-programmer would completely understand it?

Will it be computationally efficient?

Will you use some outside-the-box thinking in your algorithm?

Do you know some obscure Python library that you can utilize to do this simply and effotlessly?

Will you turn this into code golf and try to do it in as few lines as possible? I did it in 1 line

I can't wait to see your results!! As always, please remember to use spoiler tags.

P.S. After we get a few submissions, I'll share my 1-liner with you. It's ugly, but it's 1 line

ADVANCED CHALLENGE! (Can you handle it?)
Inspired by BetaWar A more advanced variation of this challenge would be to create a function that can compress a value into it's smallest possible Roman Numeral representation. For example, the number 49 evaluates to XLIX, but it could logically be written as IL (-1+50). This is a far more advanced topic, so hopefully this could tempt some of the more seasoned Programmers into giving this one a shot. THE GAUNTLET IS THROWN!

This post has been edited by atraub: 08 April 2015 - 07:08 PM

Is This A Good Question/Topic? 2

## Replies To: [Challenge] Integer to Roman Numerals

### #2 modi123_1

• Suitor #2

Reputation: 13400
• Posts: 53,484
• Joined: 12-June 08

## Re: [Challenge] Integer to Roman Numerals

Posted 08 April 2015 - 12:15 PM

Heh.. I remember doing a Roman Numeral Calculator back in the day for an AP class.

### #3 atraub

• Pythoneer

Reputation: 828
• Posts: 2,235
• Joined: 23-December 08

## Re: [Challenge] Integer to Roman Numerals

Posted 08 April 2015 - 12:20 PM

Surely you can do it again! Unless you've gotten rusty since then

This post has been edited by atraub: 08 April 2015 - 12:23 PM

### #4 modi123_1

• Suitor #2

Reputation: 13400
• Posts: 53,484
• Joined: 12-June 08

## Re: [Challenge] Integer to Roman Numerals

Posted 08 April 2015 - 12:32 PM

Sure.. let me go fire up my Mac PowerPC, and dust off my copy of CodeWarrior!

### #5 atraub

• Pythoneer

Reputation: 828
• Posts: 2,235
• Joined: 23-December 08

## Re: [Challenge] Integer to Roman Numerals

Posted 08 April 2015 - 12:38 PM

I still have a Powerbook somewhere around here

### #6 jon.kiparsky

• Screw Trump (before he screws you)

Reputation: 10625
• Posts: 18,186
• Joined: 19-March 11

## Re: [Challenge] Integer to Roman Numerals

Posted 08 April 2015 - 01:13 PM

atraub, on 08 April 2015 - 02:12 PM, said:

It's ugly, but it's 1 line />/>/>

Remember, kids - don't try this at work!

### #7 atraub

• Pythoneer

Reputation: 828
• Posts: 2,235
• Joined: 23-December 08

## Re: [Challenge] Integer to Roman Numerals

Posted 08 April 2015 - 01:16 PM

jon.kiparsky, on 08 April 2015 - 04:13 PM, said:

atraub, on 08 April 2015 - 02:12 PM, said:

It's ugly, but it's 1 line

Remember, kids - don't try this at work!

No argument here!

### #8 BetaWar

• #include "soul.h"

Reputation: 1469
• Posts: 8,177
• Joined: 07-September 06

## Re: [Challenge] Integer to Roman Numerals

Posted 08 April 2015 - 02:22 PM

This is an interesting challenge given that difficulty levels are basically built in. You have the easier side of just doing a strictly-additive solution, the intermediate of doing additive and subtractive, and the advanced of doing an additive and subtractive solution that maintains the center as the largest possible values:

For instance 1990:
Simple: MDCCCCLXXXX
Intermediate: MCMXC
Advanced: CMMXC (there may be a better solution for this, but that's what I came up with off the top of my head).

### #9 atraub

• Pythoneer

Reputation: 828
• Posts: 2,235
• Joined: 23-December 08

## Re: [Challenge] Integer to Roman Numerals

Posted 08 April 2015 - 02:30 PM

I believe the most proper solution would be the intermediate one, that being said, I love the more advanced variant for it

This post has been edited by atraub: 08 April 2015 - 02:34 PM

### #10 baavgai

• Dreaming Coder

Reputation: 6966
• Posts: 14,572
• Joined: 16-October 07

## Re: [Challenge] Integer to Roman Numerals

Posted 08 April 2015 - 03:45 PM

Damn you, you made me write a python one liner! That is almost certainly the most un-pythony thing I've ever done in python. I'll post it after this topic has had a chance to get played with.

Kids, many lines is honestly fine for this. Fundamental rule: don't repeat yourself. If you keep to that, you win.

I actually use roman numerals as one of my playing with new language projects. However, once you understand the trick of it, it's really just rearranging stuff. I wrote a half dozen different versions in haskell...

### #11 baavgai

• Dreaming Coder

Reputation: 6966
• Posts: 14,572
• Joined: 16-October 07

## Re: [Challenge] Integer to Roman Numerals

Posted 08 April 2015 - 03:53 PM

BetaWar, on 08 April 2015 - 05:22 PM, said:

For instance 1990:
Intermediate: MCMXC
Advanced: CMMXC (there may be a better solution for this, but that's what I came up with off the top of my head).

Intermediate would be correct here. You basically just talk the digits out. It should scanner higher to lower, so M+CM+XC is 1000+900+90. The "advanced" reads CM-M-XC, 900+1000+90, which is the wrong order.

### #12 atraub

• Pythoneer

Reputation: 828
• Posts: 2,235
• Joined: 23-December 08

## Re: [Challenge] Integer to Roman Numerals

Posted 08 April 2015 - 03:56 PM

I can't wait to compare our 1 liners. I always learn something when you and I work on the same thing!

### #13 Dogstopper

Reputation: 2965
• Posts: 11,222
• Joined: 15-July 08

## Re: [Challenge] Integer to Roman Numerals

Posted 08 April 2015 - 04:32 PM

I did it in 2 lines:

Spoiler

### #14 atraub

• Pythoneer

Reputation: 828
• Posts: 2,235
• Joined: 23-December 08

## Re: [Challenge] Integer to Roman Numerals

Posted 08 April 2015 - 05:46 PM

Dogstopper, on 08 April 2015 - 07:32 PM, said:

I did it in 2 lines:

Spoiler

I seriously opened up my interpreter to see if that would work

### #15 jon.kiparsky

• Screw Trump (before he screws you)

Reputation: 10625
• Posts: 18,186
• Joined: 19-March 11

## Re: [Challenge] Integer to Roman Numerals

Posted 08 April 2015 - 05:53 PM

Just out of curiosity, does your one-liner pass pep8 validation?