Skip to main content
2 of 3
added 361 characters in body
Greg Martin
  • 16.7k
  • 4
  • 23
  • 73

Mathematica, 113 90 bytes

Thanks to Martin Ender for two suggestions that reduced the length by over 20%!

Showing off the high-level commands in Mathematica.

Nest[Flatten@(Characters/@{RomanNumeral@#,#2}&@@@(Reverse@#&@@@Tally/@Split@#))&,{"I"},#]& 

A pure function, taking an argument N and outputting the Nth element of this (0-indexed) sequence, as a list of characters. Spread out a bit:

Nest[ Flatten@ (Characters/@ {RomanNumeral@#,#2}& @@@ (Reverse@# & @@@ Tally /@ Split@#) ) & , {"I"}, #] & 

The outer Nest iterates the middle four-line function, starting on {"I"}, N times. Line 4 splits the character list of the input Roman numeral into runs of like characters, counts each run with Tally, and puts the counts before the characters they're counting. Line 3 renders the counts as Roman numerals, then splits those Roman numerals up into lists of characters. The Flatten command reduces the whole list-of-lists to a one-dimensional list.

Here's the initial version:

Nest[ "" <> Flatten[{RomanNumeral@#[[1]], #[[2]]} & /@ (Reverse@#[[1]] & /@ Tally /@ Split@Characters@#)] &, "I", #] & 
Greg Martin
  • 16.7k
  • 4
  • 23
  • 73