- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- 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.
Use this to learn the idea, then write your own version.
1234567891011 12var fi, fo : int13var n : int14var a, b, c, d : string15 161718 19function lastSyllable (s : string) : string20 var i : int21 var vowels : string := "aeiou "22 i := length (s)23 loop24 exit when i = 0 or index (vowels, s (i)) > 025 i := i - 126 end loop27 if i = 0 or s (i) = " " then28 result s (i + 1 .. *)29 else30 result s (i .. *)31 end if32end lastSyllable33 3435function toLower (s : string) : string36 var t : string := ""37 for i : 1 .. length (s)38 if s (i) < "A" or s (i) > "Z" then39 t := t + s (i)40 else41 t := t + chr (ord (s (i)) - ord ('A') + ord ('a'))42 end if43 end for44 result t45end toLower46 47open : fi, "poetry1.in", get48open : fo, "poetry1.out", put49 50get : fi, n51for i : 1 .. n52 get : fi, a : *53 get : fi, b : *54 get : fi, c : *55 get : fi, d : *56 a := lastSyllable (toLower (a))57 b := lastSyllable (toLower (b))58 c := lastSyllable (toLower (c))59 d := lastSyllable (toLower (d))60 if a = b and b = c and c = d then61 put : fo, "perfect"62 elsif a = b and c = d then63 put : fo, "even"64 elsif a = c and b = d then65 put : fo, "cross"66 elsif a = d and b = c then67 put : fo, "shell"68 else69 put : fo, "free"70 end if71end for72close : fi73close : fo74 75