13
\$\begingroup\$

I have an issue with my keyboard (more likely an issue with my browser). Some times when I click on a new text field and begin typing all my text comes out backwards. After a very short amount of time (for the purpose of the challenge we will say it is 10 milliseconds) it resumes typing forwards again. It seems that at first my cursor does not move when I begin to type so all the letters get inserted at the beginning of the textfield.

For example if I were typing python and I typed pyt in the first 10 milliseconds the result would be

hontyp

Task

Your job is to simulate this behavior in as few bytes as possible.

Input

You may take input in any reasonable method. You may not however take the input as two separate fields. The 10 millisecond delay should start when the program begins to allow input. That is you may have preprocessing to do before input is allowed, which should not be counted towards the 10 milliseconds.

Some leniency is permitted in the timing as long as it averages 10 milliseconds and is off by more than 1 milliseconds no more than 1% of the time on a reasonably speced computer.

Output

You may output the string in whatever method you see fit. You may either update live as the user is typing or output the result after you are finished taking input (either by a newline or end of file ^D).

Test cases

Some of these were generated by this CMC in The Nineteenth Byte

First 10  | after   | result
bra         in-flak   in-flakarb
ap          ple       plepa
code-       golf      golf-edoc
no          ne        neon
we          st        stew
ai          med       media
draw        back      backward
\$\endgroup\$
8
  • 3
    \$\begingroup\$ @Luke 1) Yes. 2) If that is the only way they can take input then they cannot compete. \$\endgroup\$
    – Wheat Wizard
    Apr 16, 2017 at 16:03
  • 3
    \$\begingroup\$ ^ That probably rules out a lot of languages \$\endgroup\$
    – Luis Mendo
    Apr 16, 2017 at 16:12
  • 2
    \$\begingroup\$ It is unfortunate. I don't want this challenge to rule our languages, however I really don't want it to become a trivial string manipulation challenge. \$\endgroup\$
    – Wheat Wizard
    Apr 16, 2017 at 16:23
  • 2
    \$\begingroup\$ 10 milliseconds is a very short time. Did you really mean that? \$\endgroup\$
    – Neil
    Apr 16, 2017 at 21:02
  • 1
    \$\begingroup\$ @Neil You are probably right, but it is what it is, I'm not in a position to change it at this point. \$\endgroup\$
    – Wheat Wizard
    Apr 16, 2017 at 21:19

6 Answers 6

7
\$\begingroup\$

VimScript, 48 47 bytes

Vim has some nice builtins for this. Requires Vim 8.

se ri|cal timer_start(10,{->execute("se ri&")})

Expects text entered in insert mode, a very reasonable input for VimScript. Explanation:

se ri " short for 'set revins', or 'reverse insert'
cal timer_start(10, " Start a timer that fires after 10ms
                 {->execute("se ri&")}) " Give it a lambda that unsets ri

My initial approach, se ri|sl10m|se ri&, doesn't work because the text is buffered until after the sleep command, and I couldn't find a way around this.

\$\endgroup\$
5
\$\begingroup\$

HTML (JS), 96 bytes

<input id=i oninput=i.d=i.d||Date.now()+1e3;Date.now()<i.d&&(i.selectionEnd=i.selectionStart=0)>

Starts timing for 1s from the first keypress to give you a chance to see it in action.

\$\endgroup\$
4
\$\begingroup\$

JavaScript (ES6), 88 bytes

p=s='',t=+new Date,document.onkeyup=k=>console.log(+new Date-t>9?(s+=k.key)+p:p=k.key+p)

Demo

The way the snippet is working, you'll need to click inside its frame so that it gets focus after clicking on the Run button and before you can start typing.

There's no way to do that in less that 10ms. So, I've set the delay to 1 second in this demo -- and even so, you'd better hurry.

p=s='',t=+new Date,document.onkeyup=k=>console.log(+new Date-t>999?(s+=k.key)+p:p=k.key+p)

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

Processing, 167 165 146 bytes

String a="",b="";long m=0;void setup(){size(9,9);}void draw(){m=m<1?millis():m;println(b+a);}void keyTyped(){if(millis()-m>9)b+=key;else a=key+a;}

Takes input as keystrokes and outputs to the console. It updates every frame.

Explanation

String a="",b="";                // a contains the reversed (in the 10 milliseconds) string, b contains the text that is typed after the 10 milliseconds.
long m=0;                        // Store the start milliseconds in variable m
void setup() {
  size(9,9);                     // set the dimensions of the window
}

void draw() {                    // loop forever
  m=m<1?millis():m;              // update m
  println(b+a);                  // print the string b+a
}

void keyTyped() {                // this function gets called whenever a key is typed
  if(millis() - m) >9)           // if over 9 milliseconds have elapsed then
    b+=key;                      //   add the key to the end of b
  else                           // else (if less than 10 milliseconds have elapsed)
    a=key+a;                     //   prepend the key to a
}
\$\endgroup\$
4
  • 1
    \$\begingroup\$ @WheatWizard Say the input was asdf + ghjk. If everything was in one variable, then after the 10 milliseconds, we would have fdsa in that one string. After that, when g is typed, the string will prepend the g and become gfdsa. Then when the h is typed, I cannot prepend h to the variable (because then the input is still reversed), I will need to use substrings and have to calculate the length of that String variable. This will cost a lot of bytes. \$\endgroup\$
    – user41805
    Apr 16, 2017 at 17:11
  • \$\begingroup\$ use ternary to reduce bytes \$\endgroup\$
    – Mr. Alien
    Apr 16, 2017 at 17:13
  • \$\begingroup\$ @Mr.Alien In order to use a ternary, I would need to deal with the same variable, but here, I am use two different variables (namely a and b). Even if I assign the result to a temp variable, the resulting bytecount is still larger. \$\endgroup\$
    – user41805
    Apr 16, 2017 at 17:18
  • \$\begingroup\$ My bad, I thought it's a JavaScript answer x) .. realized when I saw type casting in your answer... \$\endgroup\$
    – Mr. Alien
    Apr 16, 2017 at 19:53
1
\$\begingroup\$

C - 169 bytes

Well C is not the best for this as it has no platform-independent way of doing this. Works under MSVC.

#include<time.h>
#include<conio.h>
l,k,b[9];main(a){for(a=clock();clock()-a<100;(k=_getch())?b[l++]=k:0);for(;(k=_getch())-13;k?putchar(k):0);for(;l;putchar(b[--l]));}

Bit of a hacky approach, if you can type more than 9 characters in 10 ms, this will crash. Updates after a newline character.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I'm not sure if I can mash 9 random characters in 10ms... \$\endgroup\$
    – bendl
    Jul 5, 2017 at 14:47
1
\$\begingroup\$

Python 2, 112 106

import msvcrt as m,time
a=b=''
T=time.time
t=T()
while 1:c=m.getch();exec'ba=+c=+cb'[T()-t>1::2];print a+b

If you want the output to be flush, without flooding with lines, use print'\r'+a+b, instead

You'll need to kill the process to stop it

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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