Parrot User’s Delay

1

0

A simple code golf challenge similar to an already existing, but more complex one:

  1. Wait for user to press enter.
  2. Wait for user to press enter again.
  3. Print the elapsed time between the 1. and 2.
  4. Wait an equal amount of time
  5. Print the actual elapsed time of 4.

Order of 3. and 4. is not important. Notice that the second printout entails actual timekeeping during the machine's delay; just outputting the desired delay is not enough.

The time outputs should be in seconds, with at least 10 ms resolution.

Adám

Posted 2015-12-21T18:51:04.703

Reputation: 37 779

Question was closed 2015-12-21T20:53:32.547

@DigitalTrauma Fixed. Thanks. – Adám – 2015-12-21T19:19:11.310

1"Simpler" does not imply "not a duplicate." – Alex A. – 2015-12-21T20:56:01.877

Answers

2

MATL, 14 bytes

This answer uses the current version of the language (3.1.0), which is earlier than the challenge.

jY`jZ`tDY`Y.Z`

Example

I tried to wait for about 1 second:

>> matl jY`jZ`tDY`Y.Z`
> 
> 
1.011063770698121
1.013172086910349

Explanation

j       % input string (will be empty)
Y`      % start a stopwatch timer      
j       % input string (will be empty) 
Z`      % read the stopwatch timer and push the value onto the stack    
tD      % duplicate, convert to string and display
Y`      % start a stopwatch timer (this doesn't produce any output)      
Y.      % pause the specified amount of time (which is at the top of the stack)
Z`      % read the stopwatch timer (will be implicitly displayed)  

Luis Mendo

Posted 2015-12-21T18:51:04.703

Reputation: 87 464

1

R, 87 bytes

code

f=readline;s=Sys.time;f();q=s();f();e=(m=s())+(z=(m-q));while(s()<e)1;cat(z,"\n",s()-m)

example

> f=readline;s=Sys.time;f();q=s();f();e=(m=s())+(z=(m-q));while(s()<e)1;cat(z,"\n",s()-m)
> 1.343448     # 1 second wait plus 0.3s mental lag
> 1.358646

Mutador

Posted 2015-12-21T18:51:04.703

Reputation: 1 361

If I take the difference of two Sys.time R output's Time difference of x seconds, is this a valid printout instead of using cat()? – Mutador – 2015-12-21T19:34:47.050

1

Matlab: 44 bytes ##

input('');tic;input('');t=toc
pause(t);toc-t

Output

t =
    1.8318
ans =
    1.8338

Explanation: input('') wait for enter, tic starts timer, t=toc display

brainkz

Posted 2015-12-21T18:51:04.703

Reputation: 349

0

Mathematica, 71 bytes

(b=InputString)[];a=(c=Print@*First)@(d=AbsoluteTiming)@b[];c@d@Pause@a

Needs to be run in v10.3 or higher. Good thing that symbols are first-class objects...

LegionMammal978

Posted 2015-12-21T18:51:04.703

Reputation: 15 731

0

Bash + GNU utilities, 63

d=0
f(){ read;d=`date +%s.%N-$d|bc -l`;}
f
f
bc<<<$d/1
sleep $d

Digital Trauma

Posted 2015-12-21T18:51:04.703

Reputation: 64 644