Problem solution · Turing

CCC 1997 P1 - Sentences

CCC 1997 P1 - Sentences: 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
53 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 1997 P1 - Sentences, 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

  • 53 lines of Turing from the credited upstream file ccc97s1.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 1997 P1 - Sentences · TuringTuring
Use this to learn the idea, then write your own version.
% CCC 1997% problem A: Sentences%% given arrays of nouns, verb and object% print ALL combinations of nouns, vers and objects% a simple simple triple nested loop does that. % file handling is used, the number of sets is given% then the number of subjects, verbs and objects (max 20)% finally the subjects, verbs and objects, each on a separate line var infile : string := "sent.in"var outfile : string := "sent.out"var fi, fo : intvar n : intvar ns, nv, no : intvar s, v, o : array 1 .. 20 of string open : fi, infile, getopen : fo, outfile, put get : fi, nfor i : 1 .. n    get : fi, ns    get : fi, nv    get : fi, no    for j : 1 .. ns        get : fi, s (j) : *    end for    for j : 1 .. nv        get : fi, v (j) : *    end for    for j : 1 .. no        get : fi, o (j) : *    end for    for j : 1 .. ns        for k : 1 .. nv            for m : 1 .. no                put : fo, s (j), " ", v (k), " ", o (m), "."            end for        end for    end for    put :fo, ""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 ↗