[Challenge] Integer to Roman Numerals

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 7586 Views - Last Post: 15 April 2015 - 10:58 PM Rate Topic: -----

#1 atraub  Icon User is offline

  • Pythoneer
  • member icon

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

[Challenge] Integer to Roman Numerals

Post icon  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 :D


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  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13400
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#3 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 828
  • View blog
  • 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 :P

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

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13400
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

#5 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 828
  • View blog
  • 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 :online2long:
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Screw Trump (before he screws you)
  • member icon


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

Re: [Challenge] Integer to Roman Numerals

Posted 08 April 2015 - 01:13 PM

View Postatraub, on 08 April 2015 - 02:12 PM, said:

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


Remember, kids - don't try this at work!
Was This Post Helpful? 0
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

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

Re: [Challenge] Integer to Roman Numerals

Posted 08 April 2015 - 01:16 PM

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

View Postatraub, on 08 April 2015 - 02:12 PM, said:

It's ugly, but it's 1 line :D

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

No argument here!
Was This Post Helpful? 0
  • +
  • -

#8 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1469
  • View blog
  • 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).
Was This Post Helpful? 1
  • +
  • -

#9 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 828
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6966
  • View blog
  • 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. :P 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...
Was This Post Helpful? 1
  • +
  • -

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


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

Re: [Challenge] Integer to Roman Numerals

Posted 08 April 2015 - 03:53 PM

View PostBetaWar, 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.
Was This Post Helpful? 0
  • +
  • -

#12 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 828
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

#13 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • 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

Was This Post Helpful? 2
  • +
  • -

#14 atraub  Icon User is offline

  • Pythoneer
  • member icon

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

Re: [Challenge] Integer to Roman Numerals

Posted 08 April 2015 - 05:46 PM

View PostDogstopper, 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
Was This Post Helpful? 0
  • +
  • -

#15 jon.kiparsky  Icon User is offline

  • Screw Trump (before he screws you)
  • member icon


Reputation: 10625
  • View blog
  • 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? :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2