Problem solution · Turing

CCC 2003 S2 - Poetry

CCC 2003 S2 - Poetry: 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
75 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 2003 S2 - Poetry, 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

  • 75 lines of Turing from the credited upstream file ccc03s2.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 2003 S2 - Poetry · TuringTuring
Use this to learn the idea, then write your own version.
% CCC 2003% Problem J4/S2: Poetry%% get the last syllable of 4 lines and determine type of rhyme.% relatively simple string processing, including converting % to lower so that case is not an issue%% file I/O% number of 4 line poems given, followed by those 4 sets of lines% ouptut is a single word description of each group of 4 lines. var fi, fo : intvar n : intvar a, b, c, d : string %returns the last syllable: sequence from the last vowel to end% or the entire last word if not vowel in that last word. function lastSyllable (s : string) : string    var i : int    var vowels : string := "aeiou "    i := length (s)    loop        exit when i = 0 or index (vowels, s (i)) > 0        i := i - 1    end loop    if i = 0 or s (i) = " " then        result s (i + 1 .. *)    else        result s (i .. *)    end ifend lastSyllable % converts the string to lower casefunction toLower (s : string) : string    var t : string := ""    for i : 1 .. length (s)        if s (i) < "A" or s (i) > "Z" then            t := t + s (i)        else            t := t + chr (ord (s (i)) - ord ('A') + ord ('a'))        end if    end for    result tend toLower open : fi, "poetry1.in", getopen : fo, "poetry1.out", put get : fi, nfor i : 1 .. n    get : fi, a : *    get : fi, b : *    get : fi, c : *    get : fi, d : *    a := lastSyllable (toLower (a))    b := lastSyllable (toLower (b))    c := lastSyllable (toLower (c))    d := lastSyllable (toLower (d))    if a = b and b = c and c = d then        put : fo, "perfect"    elsif a = b and c = d then        put : fo, "even"    elsif a = c and b = d then        put : fo, "cross"    elsif a = d and b = c then        put : fo, "shell"    else        put : fo, "free"    end ifend forclose : 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 ↗