Problem solution · Turing

CCC 2008 J4 - From Prefix to Postfix

CCC 2008 J4 - From Prefix to Postfix: a Turing solution using depth-first search. Learn the idea, check the complexity, and read the full code, with credit to CCCSolutions.

Technique
Depth-first search
Source
CCCSolutions
Length
68 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

Depth-first search

For CCC 2008 J4 - From Prefix to Postfix, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.

  1. Define the state carried into one recursive or stack frame.
  2. Mark or choose the current state before exploring children.
  3. Combine child results or undo the choice when the branch finishes.

Code notes

  • 68 lines of Turing from the credited upstream file ccc08j4.t.
  • The implementation keeps its working state in language-native values and containers.
  • No explicit loop blocks detected.

Complexity

Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.

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 2008 J4 - From Prefix to Postfix · TuringTuring
Use this to learn the idea, then write your own version.
% CCC 2008% J4: Prefix to postfix%% It occurs to me that the best way to do this% is with recursion.%% this method takes an input prefix string s, % and creates the postfix, stored in post1% with a temporary string called rest%% generally the method is, if the postfix starts with + or -% the first operand starts in the third letter% the second operand is found from the rest of the string%     (after the first operand is taken out)% then put the postfix together: "first second +"%% If the postfix starts with a number, that is the postfix. %% an example might help, starting with "+ - 1 2 3"% 1. it would enter with s = "+ - 1 2 3"% 2. it would enter again with s = "- 1 2 3"% 3. it would enter again with "1 2 3" and leave with post1="1" and rest="2 3"%          (the "1" is the first part of "- 1 2 3")% 4. it would enter again with "2 3" and leave with post1="2" and rest="3"%          (the "2" is the second part of "- 1 2 3")%    now 2 finishes with post1 = "1 2 -" and rest = "3"%    now the first half of 1 is done and second call is done:% 5. it would enter with s = "3" and leave with post1 = "3" and rest = ""% now 1 is done with post1 = "1 2 - 3 +"%% think about it enough and you'll get it :-)%  var prefix : stringvar post1, temp : string procedure postfix (s : string, var post1 : string, var rest : string)    var first, second, temp1, temp2 : string    put "in:" + s    if s (1) = '+' then        postfix (s (3 .. *), first, temp1)        postfix (temp1, second, temp2)        post1 := first + " " + second + " +"        rest := temp2    elsif s (1) = '-' then        postfix (s (3 .. *), first, temp1)        postfix (temp1, second, temp2)        post1 := first + " " + second + " -"        rest := temp2    else        post1 := s (1)        if length (s) > 1 then            rest := s (3 .. *)        else            rest := ""        end if    end if    put "out:" + post1 + ":" + restend postfix loop    get prefix : *    exit when prefix = "0"    postfix (prefix, post1, temp)    put post1end loop 

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 ↗