Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I often forget what I wanted to say when the teacher calls on me to speak. Can you make me a tool to solve this?

Requirements:

  • The program must loop as follows:
    • First, take in input
    • If the input is empty, print the last stored question.
    • Otherwise, store the input as a question.

Notes:

  • The input will never be empty if there are no questions stored.

Remember, this is , so the code with the smallest number of bytes wins.

share|improve this question

Perl, 13 17 +1 = 18 bytes

$/eq$_?$;=$_:say$

+1 for the -n flag (saved 1 byte thanks to @Dada)

As long as the input is not equal to the carriage return, it stores the input in $;. If it is equal to the carriage return, it prints what's already in $;.

This assumes that all input can be characterized as questions, even if grammatically, they are not such.

share|improve this answer
    
Actually I think you'll have to rollback to your first version, because with this one, 0 is treated like an empty input. – Dada 6 hours ago

Haskell, 49 bytes

g s=getLine>>=(#s)
""#s=putStr s>>g s
l#s=g l
g""

How it works: start with g "". The parameter s of function g is the question in store. g reads the next line from stdin and passes it (and also s) to #. If the line is empty, # prints the store and calls g again. If the line is not empty, g is called with the line as the new store.

share|improve this answer

05AB1E, 12 bytes

[IDõQi®,ë©\}

Body must be at least 30 characters; you entered 29.

share|improve this answer
    
I can't test now, but I think you can remove the last character. – daHugLenny 5 hours ago

JavaScript, 36 34 31 bytes

for(;;b?a=b:alert(a))b=prompt()

An infinite loop keeps asking for input and stores it in b. It the input is not empty it's then stored in a, otherwise the value of a is printed.

Saved 2 bytes thanks to @ETHproductions

share|improve this answer
    
Nice answer. You can save a byte by removing the 1, and another by moving b?a=b:alert(a) into the last section within the for-loop (i.e. for(a=0;;b?a=b:alert(a))b=prompt()) – ETHproductions 3 hours ago
    
In fact a=0 is not necessary neither. Since the input can only be empty if there are questions stored, a won't be referenced if it hasn't been assigned. – Oriol 3 hours ago

reticular, 12 bytes

id""E[$dp]~*

Try it online!

Explanation

id""E[$dp]~*
i             take a line of input     [input]
 d""E         push equality with ""    [input, input == ""]
     [$dp]    push that func           [input, input == "", [$p]]
         ~*   execute it iff equal
              on equal:
      $       drop empty input
       dp     duplicate then print the TOS
              this wraps around to the beginning, taking another line of input
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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