Implement Stopwatch for typing Alphabets

Alphabets = "a b c d e f g h i j k l m n o p q r s t u v w x y z".


  • The program should run infinitely and should stop only when Esc button is pressed.
  • The stopwatch starts with keystroke "a" and ends with keystroke "z"

  • Output is the time taken to type the Alphabets correctly (ie. time take for 51 keystrokes)

  • If an illegal character occurs then stopwatch should stop and print "Failed"

  • If a new record is set then print "!!New record!!," TimeTaken

    where, TimeTaken = time taken to print alphabets correctly.

Winner: One with the shortest code.

  • \$\begingroup\$ What do you mean by "new record"? Is the alphabet typed in over and over again? \$\endgroup\$
    – Howard
    Oct 19 '13 at 8:37
  • \$\begingroup\$ yes, the program will run till we press "Esc" \$\endgroup\$
    – Coding man
    Oct 19 '13 at 8:43
  • \$\begingroup\$ @Codingman How exactly is the record supposed to be saved? \$\endgroup\$ Feb 7 '14 at 10:53
  • \$\begingroup\$ I created something like this a while back: ethproductions.github.io/type Level 15 is the alphabet. \$\endgroup\$ Oct 11 '16 at 16:03

Mathematica, 303

Here with white-space and expanded symbols. Everything happens in the Switch statement of the EventHandler where I pattern match {pressed key, next letter}.

Might want to replace the Quit with something less sinister before hitting escape:

z := AbsoluteTime[];
f = "a"; y = Dynamic;
  a = f~CharacterRange~"z", c = 1, d = f, r = \[Infinity]},
  "KeyDown" :> Switch[
    {CurrentValue@"EventKey", a[[c]]},
    {f ..},
      t = z; c++; d = y@a[[c]],
    {"z" ..}, 
      t = z - t;
      d = If[t < r,
         r = t; "!!New record!!,",
         ""] ~~ ToString@t;
      c = 1,
    {a_, a_},
    {"\[RawEscape]", _}, Quit[],
    _, d = "Failed"; c = 1]]]
  • \$\begingroup\$ I apologize. "!!New record!!, TimeTaken" is not string completely. Instead, it is "!!New record!!," TimeTaken. TimeTaken is variable storing the time taken. I am not aware of Mathematica. I hope, you will handle it correctly \$\endgroup\$
    – Coding man
    Oct 19 '13 at 18:15
  • \$\begingroup\$ @Codingman Oh, I should have figured, just mindless copy+paste :) \$\endgroup\$
    – ssch
    Oct 19 '13 at 18:40

C, 181

Note that in order for this to work, your terminal must be setup to not buffer characters. One possible linux solution is here. Should this be considered part of my code?

double t,c,r=2<<29;main(l){for(;l<27&&96+l++==(c=getchar());)l==2&&time(&t);if(c==27)return;t=difftime(time(),t);l-27?puts("Failed"):t<r&&printf("!!New record!! %f\n",r=t);main(1);}

Perl, 182

use Term::ReadKey;ReadMode 3;@a=a..z;while(1){$c=getc;$s=time if!$i;last if$c eq"\e";if($c ne$a[$i++]){print"Failed";last}if($i==@a){print"!!New record!!,",(time-$s);last}}ReadMode 0

There's probably room for improvement. I could also remove the ReadMode 0 from the end, saving 10 chars. In addition, it's not clear to me if the use of Term::ReadKey is OK or not for the Code Golf. If it's not, then I don't think that this particular golf can be solved with Perl.

Same program written properly and in a readable fashion:

use warnings;
use strict;
use Term::ReadKey;
use Time::HiRes qw(time);
ReadMode 3;
my @a=('a'..'z');
my $i=0;
my $s;
while (1) {
    my $c=ReadKey(0);
    $s=time if !$i;
    last if $c eq "\e";
    if ($c ne $a[$i++]) {
        print "Failed";
    if ($i==@a) {
        print "!!New record!!,",(time-$s);
ReadMode 0;

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.