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

10 Answers 10

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
    
You probably wanted to write ne instead of eq (right now this doesn't work). – Dada 4 hours ago
1  
You can save three byte by doing /./ instead of $/ne$_ ;-) – Dada 4 hours ago

05AB1E, 12 11 10 bytes

Thanks @daHugLenny and @Adnan for 1 byte!
Thanks @Emigna for 1 byte!

[IDõQiX,ëU
share|improve this answer
1  
I can't test now, but I think you can remove the last character. – daHugLenny 12 hours ago
1  
Yes, you can remove the last bracket (it will be auto-completed). – Adnan 5 hours ago
2  
If you replace ® and © with X and U, you don't need the backslash. – Emigna 35 mins 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

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 10 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 10 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

Python3 - 49 bytes

s=""
while 1:
 b=input()
 if b:s=b
 else:print(s)
share|improve this answer
1  
NameError: name 'b' is not defined – shooqie 3 hours ago
    
@shooqie Ah, I typed this with my phone so I accidentally left out b=input(). Thanks! – TuukkaX 2 hours ago

Pyke, 6 bytes

z=z~zr

Try it here!

     r - while 1:
z      -   input() or z.contents
 =z    -  z.contents = ^
   ~z  -  z.contents
share|improve this answer

Python 3, 34 bytes

s=""
while[print(s)]:s=input()or s
share|improve this answer

Mathematica, 44 bytes

If[(b=InputString[])=="",Print@a,a=b]~Do~∞

Full program. The Unicode character is U+221E INFINITY for \[Infinity].

share|improve this answer

CMD, 37 bytes

set/p a=
if %a%.=. echo b
set a=%b%
c

in a file names c.cmd or

set/p a=
if %a%.=. echo b
set a=%b%
%0

in any file

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.