Problem solution · Turing

CCC 1996 P4 - Roman Numerals

CCC 1996 P4 - Roman Numerals: a Turing solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to CCCSolutions.

Technique
Direct simulation
Source
CCCSolutions
Length
143 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For CCC 1996 P4 - Roman Numerals, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 143 lines of Turing from the credited upstream file ccc96s4.t.
  • The implementation keeps its working state in language-native values and containers.
  • No explicit loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from CCCSolutions by CCCSolutions contributors · Milliken Mills High School and is used under the MIT licence.

Full codeCCC 1996 P4 - Roman Numerals · TuringTuring
Use this to learn the idea, then write your own version.
% CCC 1996% problem 4 Calculate as the Romans Do % convert to and from Roman Numerals to do addition% max number is 1000 % file i/o used.% the first number is the number of numbers.% expression is in the form "a+b=" where a and b are Roman Numerals function toDecimal (s : string) : int    var t, v : int    var old : int := 100000    t := 0    for i : 1 .. length (s)        if s (i) = "I" then            v := 1        elsif s (i) = "V" then            v := 5        elsif s (i) = "X" then            v := 10        elsif s (i) = "L" then            v := 50        elsif s (i) = "C" then            v := 100        elsif s (i) = "D" then            v := 500        elsif s (i) = "M" then            v := 1000        end if        if v > old then            t := t + v - 2 * old        else            t := t + v        end if        old := v    end for    result tend toDecimal % very unFancy: brute force is easiestfunction toRoman (xx : int) : string    var x : int := xx    var d : int    var s : string    s := ""    d := x div 100    x := x mod 100    if d = 1 then        s := s + "C"    elsif d = 2 then        s := s + "CC"    elsif d = 3 then        s := s + "CCC"    elsif d = 4 then        s := s + "CD"    elsif d = 5 then        s := s + "D"    elsif d = 6 then        s := s + "DC"    elsif d = 7 then        s := s + "DCC"    elsif d = 8 then        s := s + "DCCC"    elsif d = 9 then        s := s + "CM"    end if    d := x div 10    x := x mod 10    if d = 1 then        s := s + "X"    elsif d = 2 then        s := s + "XX"    elsif d = 3 then        s := s + "XXX"    elsif d = 4 then        s := s + "XL"    elsif d = 5 then        s := s + "L"    elsif d = 6 then        s := s + "LX"    elsif d = 7 then        s := s + "LXX"    elsif d = 8 then        s := s + "LXXX"    elsif d = 9 then        s := s + "XC"    end if    d := x    if d = 1 then        s := s + "I"    elsif d = 2 then        s := s + "II"    elsif d = 3 then        s := s + "III"    elsif d = 4 then        s := s + "IV"    elsif d = 5 then        s := s + "V"    elsif d = 6 then        s := s + "VI"    elsif d = 7 then        s := s + "VII"    elsif d = 8 then        s := s + "VIII"    elsif d = 9 then        s := s + "IX"    end if    result send toRoman  var infile : string := "rom.in"var outfile : string := "rom.out"var fi, fo : intvar n : intvar line : stringvar plus, answer : int open : fi, infile, getopen : fo, outfile, put get : fi, nfor i : 1 .. n    get : fi, line    plus := index (line, "+")    answer := toDecimal (line (1 .. plus - 1)) + toDecimal (line (plus +        1 .. length (line) - 1))    if answer > 1000 then        put : fo, line, "CONCORDIA CUM VERITATE"        put line, "CONCORDIA CUM VERITATE"    else        put : fo, line, toRoman (answer)        put line, toRoman (answer)    end if    put : fo, ""    put ""end for close : ficlose : fo  

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗