103
\$\begingroup\$

Your task is to reverse the order in which some prints get executed.


Specs:
Your code will be in this form:

//some lines of code
/*code*/ print "Line1" /*code*/
/*code*/ print "Line2" /*code*/
/*code*/ print "Line3" /*code*/
/*code*/ print "Line4" /*code*/
//some lines of code

You will have to print (or echo, or write, or equivalent) those strings from the fourth to the first.

  • You decide which lines of your program must print the strings, but they must be adjacent;

  • Every line can contain only one print, and cannot exceed 60 bytes in length;

  • Since this is , be creative and avoid to write just a goto or a simple for(i){if(i=4)print"Line1";if(i=3)...}

  • The most upvoted answer in 2 weeks wins this.

  • Your output MUST be Line4 Line3 Line2 Line1 OR Line4Line3Line2Line1 OR Line4\nLine3\nLine2\nLine1(where \n is a newline), and it must be generated only by executing those prints backwards.

Happy coding!

UPDATE: Contest is over! Thank you all :)

\$\endgroup\$
6
  • 17
    \$\begingroup\$ Does Arabic count? : ) \$\endgroup\$
    – user11739
    Commented Feb 13, 2014 at 8:17
  • \$\begingroup\$ If you are able to meet the specs, of course :P \$\endgroup\$
    – Vereos
    Commented Feb 13, 2014 at 10:34
  • \$\begingroup\$ Wanted to quickly clarify one rule... When you say "Every like can contain only one print", do you mean one text line in the code file or one LOC/statement? \$\endgroup\$
    – Ruslan
    Commented Feb 15, 2014 at 8:54
  • \$\begingroup\$ Every line of code can contain only one print \$\endgroup\$
    – Vereos
    Commented Feb 15, 2014 at 15:07
  • \$\begingroup\$ does it have to pass a code review - suitable for production code? \$\endgroup\$
    – Lance
    Commented Feb 17, 2014 at 19:37

156 Answers 156

1 2 3 4 5
6
0
\$\begingroup\$

SHELL

_rp() { 
    eval "printf '%s\\n' \
        \"\${$(seq -s'}" "${' $# -1 1)}\""
}

Hand that a list of shell arguments and it will print them back at you in reverse. It works by evaluating the output of GNU's sequence into a shell quoted list of positional parameters.

DEMO

set -vx
_rp one two three

###OUTPUT

+ _rp one two three
seq -s'}" "${' $# -1 1
++ seq '-s}" "${' 3 -1 1
+ eval 'printf '\''%s\n'\'' "${3}" "${2}" "${1}"'
printf '%s\n' "${3}" "${2}" "${1}"
++ printf '%s\n' three two one
three
two
one

From that point on you can just define IFS as necessary when invoking it in order to split among the arguments as you please.

\$\endgroup\$
0
\$\begingroup\$

Javascript

Sort of a translation of Gareth's Perl answer

setTimeout(function(){alert("Line1")},200);
setTimeout(function(){alert("Line2")},140);
setTimeout(function(){alert("Line3")},80);
setTimeout(function(){alert("Line4")},20);
\$\endgroup\$
0
\$\begingroup\$

DC

File rev3.dc:

[[line 1]PAP]
[[line 2]PAP]
[[line 3]PAP]
[[line 4]PAP]
xxxx

This pushes the print actions on the stack and executes them by x (== pop and execute).

Run:

$ dc -f rev3.dc 
line 4
line 3
line 2
line 1
\$\endgroup\$
0
\$\begingroup\$

C#

Simply using a Stack of type Action to store lambdas that execute Console.WriteLine. As the nature of a stack dictates the last "pushed" Action will be "popped" first.

static void Main(string[] args)
    {
        var s = new Stack<Action>();
        s.Push(() => { Console.WriteLine("Line1"); });
        s.Push(() => { Console.WriteLine("Line2"); });
        s.Push(() => { Console.WriteLine("Line3"); });
        s.Push(() => { Console.WriteLine("Line4"); });
        while (s.Count > 0)
        {
            s.Pop().Invoke();
        }
        Console.Read();
    }
\$\endgroup\$
0
0
\$\begingroup\$

Tcl/Tk

pack [label .l1 -text "Line 1"] -side bottom
pack [label .l2 -text "Line 2"] -side bottom
pack [label .l3 -text "Line 3"] -side bottom
pack [label .l4 -text "Line 4"] -side bottom

enter image description here

\$\endgroup\$
0
\$\begingroup\$

C++

Based on my own C++ answer, but next level: no functors and no lambdas! Just function pointers!

#include <iostream>
#include <stack>

using namespace std;

void f1() {cout<<"Line 1"<<endl;}
void f2() {cout<<"Line 2"<<endl;}
void f3() {cout<<"Line 3"<<endl;}
void f4() {cout<<"Line 4"<<endl;}

int main()
{
    stack<void(*)()> instructions;
     
    instructions.push(f1);
    instructions.push(f2);
    instructions.push(f3);
    instructions.push(f4);
   
    while(!instructions.empty())
    {
        instructions.top()();
        instructions.pop();
    }
}

demo

\$\endgroup\$
1 2 3 4 5
6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.