10
\$\begingroup\$

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.

Clarification: The program does not need to store multiple questions. I originally wanted it to store multiple, but I changed it after many people misunderstood the question.

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

\$\endgroup\$
  • \$\begingroup\$ Did you mean "previously stored question"? I find "last stored question" confusing as it suggests you might store more than one question. \$\endgroup\$ – Neil Nov 6 '16 at 19:58
  • 1
    \$\begingroup\$ @Neil It's supposed to be storing more than one question, but since many answers already misunderstood the question, I'll just leave it be. \$\endgroup\$ – Oliver Ni Nov 6 '16 at 20:05

16 Answers 16

10
\$\begingroup\$

Perl, 13 17 14 +1 = 15 bytes

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

+1 for the -n flag (saved 4 bytes 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.

\$\endgroup\$
  • \$\begingroup\$ You probably wanted to write ne instead of eq (right now this doesn't work). \$\endgroup\$ – Dada Nov 6 '16 at 9:26
  • 1
    \$\begingroup\$ You can save three byte by doing /./ instead of $/ne$_ ;-) \$\endgroup\$ – Dada Nov 6 '16 at 9:28
8
\$\begingroup\$

05AB1E, 12 11 10 bytes

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

[IDõQiX,ëU
\$\endgroup\$
  • 1
    \$\begingroup\$ I can't test now, but I think you can remove the last character. \$\endgroup\$ – acrolith Nov 6 '16 at 2:02
  • 1
    \$\begingroup\$ Yes, you can remove the last bracket (it will be auto-completed). \$\endgroup\$ – Adnan Nov 6 '16 at 8:27
  • 2
    \$\begingroup\$ If you replace ® and © with X and U, you don't need the backslash. \$\endgroup\$ – Emigna Nov 6 '16 at 13:48
4
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ 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()) \$\endgroup\$ – ETHproductions Nov 6 '16 at 3:52
  • \$\begingroup\$ 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. \$\endgroup\$ – Oriol Nov 6 '16 at 4:08
  • \$\begingroup\$ also stops the browser for a few seconds with firefox-49.0.2 \$\endgroup\$ – bunyaCloven Nov 6 '16 at 18:21
  • 1
    \$\begingroup\$ The only requirement in your question is that empty input prints the last stored question. Why do you want me to hold references to previously stored questions? \$\endgroup\$ – Oriol Nov 6 '16 at 18:25
  • 1
    \$\begingroup\$ @bunyaCloven For me it only stops the browser when I tell it to block dialogs. That's expected due to the infinite loop. After some seconds the browser detects the problem and kills the JS. \$\endgroup\$ – Oriol Nov 6 '16 at 18:28
2
\$\begingroup\$

Mathematica, 44 bytes

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

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

\$\endgroup\$
2
\$\begingroup\$

Python 3, 31 bytes

while 1: I=input()or I;print(I)

Try it out!

-1 byte thanks to Flp.Tkc

\$\endgroup\$
  • 1
    \$\begingroup\$ you can write that while loop on 1 line \$\endgroup\$ – FlipTack Nov 7 '16 at 8:11
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

Pyke, 6 bytes

z=z~zr

Try it here!

     r - while 1:
z      -   input() or z.contents
 =z    -  z.contents = ^
   ~z  -  z.contents
\$\endgroup\$
1
\$\begingroup\$

Python3 - 49 bytes

s=""
while 1:
 b=input()
 if b:s=b
 else:print(s)
\$\endgroup\$
  • 1
    \$\begingroup\$ NameError: name 'b' is not defined \$\endgroup\$ – shooqie Nov 6 '16 at 10:54
  • \$\begingroup\$ @shooqie Ah, I typed this with my phone so I accidentally left out b=input(). Thanks! \$\endgroup\$ – Yytsi Nov 6 '16 at 11:29
1
\$\begingroup\$

Python 3, 34 bytes

s=""
while[print(s)]:s=input()or s
\$\endgroup\$
  • \$\begingroup\$ It doesn't? It works fine for me \$\endgroup\$ – Blue Nov 6 '16 at 18:09
  • 3
    \$\begingroup\$ The spec doesn't ask to store multiple questions? \$\endgroup\$ – Blue Nov 6 '16 at 18:12
1
\$\begingroup\$

Python 3, 60 bytes

q=[]
while 1:
 s=input()
 if''<s:q+=[s]
 else:print(q.pop())
\$\endgroup\$
0
\$\begingroup\$

CMD, 37 bytes

Sadly set does not take empty input:(

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

in a file names c.cmd or

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

in any file. Working(Use - as empty):

set/p a=
if %a%.=-. echo b
set b=%a%
c
\$\endgroup\$
  • \$\begingroup\$ set/p will never make a variable empty, so at the very least you need set a= on the first line; also I think you mean set b=%a%. \$\endgroup\$ – Neil Nov 6 '16 at 19:53
0
\$\begingroup\$

APLX, 19 bytes

a←⍞⋄→(0=⍴a)/1⋄a⋄→1

Explanation:

⍝ Note: the ⋄ symbols are statement separators, so I've replaced them here with newlines

a←⍞           ⍝ get an input string (⍞) and store it in variable a
→(0=⍴a)/1     ⍝ if the length of a (⍴a) is 0, goto (→) the start of this program (1)
a             ⍝ else, print a
→1            ⍝ goto the start of the program (1)
\$\endgroup\$
0
\$\begingroup\$

C, 73 63 bytes

g(){char a[99],x;for(;;){gets(a);if(*a)x=*a;else*a=x,puts(a);}}

Only uses 1 buffer and a single char for handling the logic. If user inputs nothing, only the first char in the buffer gets overwritten by '\0', in that case it gets restored from x and printed, otherwise copy the first letter into x for later use.

Previous code:

main(){char a[99],b[99];for(;;){gets(a);if(*a)strcpy(b,a);else puts(b);}}
\$\endgroup\$
0
\$\begingroup\$

Java 7, 189 187 178 bytes..

class M{public static void main(String[]a){String s="",x;do{x=new java.util.Scanner(System.in).nextLine();if(x.isEmpty())Syste‌​m.out.println(s);els‌​e s=x;}while(1>0);}}

-9 bytes by using new java.util.Scanner directly, thanks to @cliffroot.

Probably the first time I used a do-while in a codegolf challenge..

When functions are allowed instead of programs, it's 135 bytes (still many times more than most other answers..)

\$\endgroup\$
  • \$\begingroup\$ this is a a bit shorter and seems to be working – void c(){for(String s="",x;(x=new java.util.Scanner(System.in).nextLine())!=null;)if(x.isEmpty())System.out.println(s);else s=x;} \$\endgroup\$ – cliffroot Nov 7 '16 at 11:14
  • \$\begingroup\$ @cliffroot Ummm.. aren't you missing a for or while somewhere?.. What is (x=new java.util.Scanner(System.in).nextLine())!=null;)? \$\endgroup\$ – Kevin Cruijssen Nov 7 '16 at 12:32
  • 1
    \$\begingroup\$ Yeah, I managed to miss for before that somehow :) it was supposed to be void c(){for(String s="",x;for(x=new java.util.Scanner(System.in).nextLine())!=null;)if(x.isEmpty())System.out.println(s);else s=x;} and it's actually 1 byte longer then the version with do-while is – void c(){String s="",x;do{x=new java.util.Scanner(System.in).nextLine();if(x.isEmpty())System.out.println(s);else s=x;}while(1>0);} \$\endgroup\$ – cliffroot Nov 7 '16 at 12:35
  • \$\begingroup\$ @cliffroot Ah of course.. thanks! \$\endgroup\$ – Kevin Cruijssen Nov 7 '16 at 12:48
0
\$\begingroup\$

sed, 10 bytes

/./{x;d};g

Explanation:

      (implicit at start of program) read input, overwriting old input
/./{  If the input is nonempty
  x;  swap the input with the hold space
  d   then restart, without any implicit printing of input
};    end if statement
g     Append the hold space to the [empty] input
      (implicit at end of program) print the input, then restart

Nice to see an exoteric (i.e. "not esoteric") language tying with some of the golfing languages!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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