Mathematica (notebook expression), 248 bytes
DynamicModule[{x={},s=0,t=0},EventHandler[Framed@Dynamic[If[x=={"a"}&&s<1,s=SessionTime[]];Which[x==a,If[t==0,t=SessionTime[]-s];1000t,x==a~Take~Length@x,""<>x,1>0,"Fail"]],Table[{"KeyDown",c}:>x~AppendTo~CurrentValue@"EventKey",{c,a=Alphabet[]}]]]
How it works
DynamicModule[{x={},s=0,t=0},
EventHandler[
Framed@Dynamic[
If[x=={"a"} && s<1,s=SessionTime[]];
Which[
x==a,If[t==0,t=SessionTime[]-s];1000t,
x==a~Take~Length@x,""<>x,
1>0,"Fail"]],
Table[{"KeyDown",c}:>x~AppendTo~CurrentValue@"EventKey",
{c,a=Alphabet[]}]]]
A DynamicModule
with an EventHandler
that responds to lowercase letter keypresses. The variables x
, s
, and t
hold the letters pressed so far, the start time, and the end time, respectively. As soon as we notice x
being equal to {"a"}
, we start the time; we display either the total time spent, or the string built so far, or "Fail"
depending on which condition is met.
We could save another byte with t<1
rather than t==0
if we can assume that nobody is fast enough to type the alphabet in less than one second :)
If you're trying this out in a Mathematica notebook, keep in mind that you have to click inside the frame before your keypresses will be registered. (This is the reason that we need the frame to begin with; if Framed
is not there, then the entire object selected changes when a key is pressed, so it stops being selected and you'd have to click again.)
4Relevant project I made a while back. (the 15th level is basically this) – ETHproductions – 2017-10-29T19:42:40.883
4Can we output
Fail
without a heading newline? (e.g.abdFail\n
orabd Fail\n
)) – scottinet – 2017-10-30T08:44:43.7731@scottinet, no, the result (
Fail
or milliseconds) must be on a new line, like in the example. Most answers already assume this. – Danko Durbić – 2017-10-31T06:29:47.4532-1 as this "new" rule was not in the original specification and invalidates my suggestions on one of the Python answers which was within the original rules. – ElPedro – 2017-10-31T09:35:34.880
I was expecting this to be a [tag:fastest-code] challenge of printing the alphabet. – ATaco – 2017-11-02T01:19:41.983
Q about precision: if I used the ~60Hz system IRQ on a C64 in machine code to calculate milliseconds, would this be acceptable? Or do I have to program a timer chip myself just because it can be done in machine code? – Felix Palmen – 2017-11-02T06:58:13.117