Problem solution · Turing

CCC 2009 J4 - Signage

CCC 2009 J4 - Signage: 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
98 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 2009 J4 - Signage, 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

  • 98 lines of Turing from the credited upstream file ccc09j4.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 2009 J4 - Signage · TuringTuring
Use this to learn the idea, then write your own version.
% CCC 2009% J4: Signage%% arrays and string processing:% a classic full justification exercise% % given a string and length w, fully justify% the string, to a length of w. % Justify by adding periods (spaces) between% words or at the end of a single word. function full (s : string, w : int) : string    var t : string    var i : int    i := 1    t := s    if index (t, ".") > 0 then        loop            exit when length (t) = w             % find a gap            loop                exit when t (i) = "."                i := i + 1                if i > length (t) then                    i := 1                end if            end loop             % add a period (space)            t  := t (1 .. i) + "." + t (i + 1 .. *)             % skip over periods            loop                exit when t (i) not = "."                i := i + 1                if i > length (t) then                    i := 1                end if            end loop        end loop    else        % add periods (spaces) to end of single word        loop            exit when length (t) = w            t = t + "."        end loop    end if    result tend full  var words : array 1 .. 6 of stringvar w, i, space : intvar s : string words (1) := "WELCOME"words (2) := "TO"words (3) := "CCC"words (4) := "GOOD"words (5) := "LUCK"words (6) := "TODAY" put "enter w: " ..get w s := words (1)i := 2 loop    exit when i > 6     % build the line    loop        exit when i > 6 or length (s) + length (words (i)) + 1 > w        s := s + "." + words (i)        i := i + 1    end loop     % justify the line and print    put full (s, w)     % start the next line    if i <= 6 then        s := words (i)    else        s := ""    end if     i := i + 1end loop % deal with last lineif length (s) > 0 then    put full (s, w)end if  

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 ↗