- 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
- 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.
Use this to learn the idea, then write your own version.
1234567 89101112function full (s : string, w : int) : string13 var t : string14 var i : int15 i := 116 t := s17 if index (t, ".") > 0 then18 loop19 exit when length (t) = w20 21 22 loop23 exit when t (i) = "."24 i := i + 125 if i > length (t) then26 i := 127 end if28 end loop29 30 31 t := t (1 .. i) + "." + t (i + 1 .. *)32 33 34 loop35 exit when t (i) not = "."36 i := i + 137 if i > length (t) then38 i := 139 end if40 end loop41 end loop42 else43 44 loop45 exit when length (t) = w46 t = t + "."47 end loop48 end if49 result t50end full51 52 53var words : array 1 .. 6 of string54var w, i, space : int55var s : string56 57words (1) := "WELCOME"58words (2) := "TO"59words (3) := "CCC"60words (4) := "GOOD"61words (5) := "LUCK"62words (6) := "TODAY"63 64put "enter w: " ..65get w66 67s := words (1)68i := 269 70loop71 exit when i > 672 73 74 loop75 exit when i > 6 or length (s) + length (words (i)) + 1 > w76 s := s + "." + words (i)77 i := i + 178 end loop79 80 81 put full (s, w)82 83 84 if i <= 6 then85 s := words (i)86 else87 s := ""88 end if89 90 i := i + 191end loop92 9394if length (s) > 0 then95 put full (s, w)96end if97 98