Given a word (or any sequence of letters) as input, you must interpolate between each letter such that each adjacent pair of letters in the result is also adjacent on a QWERTY keyboard, as if you typed the input by walking on a giant keyboard. For example, 'yes' might become 'ytres', 'cat' might become 'cxzawert'.
Rules:
This is the keyboard format you should use:
qwertyuiop
asdfghjkl
zxcvbnmAny pair of keys which is touching in this layout is considered adjacent. For instance, 's' and 'e' are ajacent, but 's' and 'r' are not.
- The input "word" will consist of any sequence of letters. It will have only letters, so you don't have do deal with special characters.
- The input can be in any convenient form: stdin, a string, a list, etc. Letter case does not matter; you can take whatever is more convenient.
- The output can be in any convenient form: stdout, a string, a list, etc. Letter case does not matter, and it does not need to be consistent.
- Any path across the keyboard is valid, except that you cannot cross the previous letter again before getting to the next letter. For example, 'hi' could become 'hji' or 'hjnbgyui', but not 'hbhui'.
- A letter is not ajacent with itself, so 'poll' cannot become 'poll'. Instead it would need to become something like 'polkl'.
- No output letters are allowed before or after the word. For example, 'was' cannot become 'trewas' or 'wasdfg'.
This is code golf, the shortest answer in bytes wins.
9 Answers 9
Python 2, 83 bytes
lambda s:re.findall('.*?'.join(s),'qwertyuioplkmnjhbvgfcxdsza'*len(s))[0]
import re
Walks the entire keyboard until the word is written.
-
2\$\begingroup\$ How come the
import recomes after the code, not before? \$\endgroup\$BruceWayne– BruceWayne2018年12月07日 20:44:01 +00:00Commented Dec 7, 2018 at 20:44 -
\$\begingroup\$ @BruceWayne The
re.findallwould be evaluated when the lambda runs, so importing after the lambda's definition is ok. That being said, it is clearer to import before, there's just no need to \$\endgroup\$pushkin– pushkin2018年12月07日 23:17:58 +00:00Commented Dec 7, 2018 at 23:17 -
\$\begingroup\$ @pushkin ah, I didn't know that thanks for clarifying! Did you import after just as a personal preference/choice or does it help with byte count at all? \$\endgroup\$BruceWayne– BruceWayne2018年12月07日 23:25:11 +00:00Commented Dec 7, 2018 at 23:25
-
2\$\begingroup\$ @BruceWayne It's a bit of a convention for this forum. It's just so that it works with the way the TiO site organizes the code. Try clicking on the "Try it online!" link to see what I mean. \$\endgroup\$mypetlion– mypetlion2018年12月07日 23:27:44 +00:00Commented Dec 7, 2018 at 23:27
-
1\$\begingroup\$ @Makonede Doesn't work for the second case (
"cat") \$\endgroup\$TFeld– TFeld2022年01月27日 08:53:28 +00:00Commented Jan 27, 2022 at 8:53
Python 2 + NetworkX, 274 bytes (optimal solution)
(削除) 296 (削除ここまで) (削除) 300 (削除ここまで) (削除) 302 (削除ここまで) (削除) 308 (削除ここまで) (削除) 315 (削除ここまで) (削除) 319 (削除ここまで) (削除) 324 (削除ここまで) (削除) 327 (削除ここまで) (削除) 328 (削除ここまで) (削除) 430 (削除ここまで) (削除) 432 (削除ここまで) bytes
-4 bytes thanks to mypetlion
from networkx import*
i=input()
M,z='qwertyuiop asdfghjkl zxcvbnm'.center(55),i[:1]
G=from_edgelist([(M[e],M[e+h])for h in[-1,1,11,12,-11,-12]for e in range(44)if' '!=M[e]and' '!=M[e+h]])
for y,x in zip(i,i[1:]):z+=[shortest_path(G,y,x)[1:],list(G[y])[0]+y][x==y]
print z
This solution gives the shortest possible output. The keyboard is transformed into a graph used to find the shortest path to compute the output string:
puzzles --> poiuhbvcxzazxcvbhjklkiuytres
programming --> poiuytrtyuioijhgtresasdcvbnmkmkijnbg
code --> cvbhjioijhgfde
golf --> ghjiolkjhgf
yes --> ytres
hi --> hji
poll --> polpl
-
\$\begingroup\$ 274 bytes: Try it online! \$\endgroup\$mypetlion– mypetlion2018年12月11日 20:17:48 +00:00Commented Dec 11, 2018 at 20:17
-
1\$\begingroup\$ @mypetlion u made an important reduction, u can update the answer :) \$\endgroup\$mdahmoune– mdahmoune2018年12月11日 20:56:37 +00:00Commented Dec 11, 2018 at 20:56
Japt -g, 23 bytes
;D·ÎÔ+D·Årí)pUl)fUq".*?
Takes input as an array of capital letters. Very similar to the other answers otherwise.
Explanation:
; :Set D to the keyboard layout
D·Î :Get the first row of keys
Ô :Reversed
+ :Concat
D·Å :The other two rows
rí) :Interleaved
p :Repeat that string
Ul) : A number of times equal to the length of the input
f :Get the substrings that match
U : The input
q".*? : joined with ".*?"
:Implicitly output just one of the matches
JavaScript (ES6), 70 bytes
Same strategy as TFeld.
s=>'qazsxdcfvgbhnjmklpoiuytrew'.repeat(s.length).match(s.join`.*?`)[0]
-
\$\begingroup\$ -3 by outputting a singleton list (remove
[0]). \$\endgroup\$Makonede– Makonede2022年01月26日 21:56:41 +00:00Commented Jan 26, 2022 at 21:56
Charcoal, 48 bytes
≔"&⌈′′⌊5EWXVNa...-εW¶ζR"η≔⌕η§θ0ζFθF⊕%−⌕ηιζ26«§ηζ≦⊕ζ
Try it online! Link is to verbose version of code. Explanation:
≔"&⌈′′⌊5EWXVNa...-εW¶ζR"η
Get the string qwertyuioplkmjnhbgvfcdxsza.
≔⌕η§θ0ζ
Find the position of the first character of the word. This index is normally one past the character just reached, but this value fakes out the first iteration of the loop to print the first character of the word.
Fθ
Loop over each character.
F⊕%−⌕ηιζ26«
Compute how many characters to print to include the next character of the word and loop that many times.
§ηζ≦⊕ζ
Print the next character cyclically indexed and increment the index.
-
\$\begingroup\$ Have you tried rotating the string "qwertyuioplkmjnhbgvfcdxsza" and seeing if any of the rotations happen to be more compressible? I'm not familiar with charcoal's compression; maybe this isn't possible. \$\endgroup\$Vaelus– Vaelus2018年12月08日 00:06:21 +00:00Commented Dec 8, 2018 at 0:06
-
\$\begingroup\$ @Vaelus I don't know either so I tried all 26 rotations but they all compress to 20 bytes. Of course, those aren't all of the possible walks... \$\endgroup\$Neil– Neil2018年12月08日 01:04:00 +00:00Commented Dec 8, 2018 at 1:04
05AB1E, (削除) 43 (削除ここまで) (削除) 33 (削除ここまで) 31 bytes
ü2εUžV`.ιJsRìD«Œ.ΔнÅ?XyθÅ¿} ̈}JIθ«
Not the right language for this challenge, since it cannot use regex like the other answers did.. EDIT: proven wrong by @Makonede's 05AB1E answer using the Elixir eval builtin .E.
Try it online or verify all test cases.
Explanation:
ü2 # Split the input into overlapping pairs
# i.e. "poll" → ["po","ol","ll"]
ε # Map each to:
U # Pop and store the current pair in variable `X`
žV # Push ["qwertyuiop","asdfghjkl","zxcvbnm"]
` # Pop and push all three separated to the stack
.ι # Interweave the top two with each other
J # Join the list of characters to a single string
# → "azsxdcfvgbhnjmkl"
s # Swap so "qwertyuiop" is at the top
R # Reverse it to "poiuytrewq"
ì # Prepend it in front of the earlier string
# → "poiuytrewqazsxdcfvgbhnjmkl"
D« # Duplicate it, and append it to itself
# → "poiuytrewqazsxdcfvgbhnjmklpoiuytrewqazsxdcfvgbhnjmkl"
Œ # Get all substrings of this strings
.Δ # Find the first substring which is truthy for:
Á # Rotate the current string once towards the right
2£ # Only leave the first 2 letters
R # Reverse it
XQ # Check if it's equal to pair `X`
} ̈ # After the find_first, remove the last character
} # Close the map
J # Join everything together
Iθ« # Append the last character of the input
# (after which the result is output implicitly)
05AB1E, (削除) 51 (削除ここまで) (削除) 39 (削除ここまで) 38 bytes
-1 thanks to Kevin Cruijssen.
gžV`.ιJs×ばつs....*?ý’RŽ1⁄2TM«.‡Ð~r/ÿ/,"ÿ"’.E
Try it online! Takes input as a list of lowercase letters and outputs as a singleton list.
This answer exists to prove Kevin Cruijssen wrong; you can use regex in 05AB1E.
-
\$\begingroup\$ Nice answer! I didn't knew you could use the Elixir eval to use Regex like that. I need to remember that for sure. :) (PS: My answer was posted before the new 05AB1E version was live. But I assume using the Python eval
.Ein the legacy version should be possible as well.) I've also just golfed my answer a bit, and one golf can also be applied to your answer:žVćRs`.ιJ«tožV`.ιJsRìfor -1. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2022年01月27日 08:46:04 +00:00Commented Jan 27, 2022 at 8:46
Vyxal, 16 bytes
Lk•÷Y$Ṙp*?‛€?jẎh
Lk•÷Y$Ṙp*?‛€?jẎh
k• # List of rows on the keyboard: ["qwertyuiop", "asdfghjkl", "zxcvbnm"]
÷ # Push each item onto the stack
Y # Interleave the top two: "azsxdcfvgbhnjmkl"
$ # Swap so "qwertyuiop" is on top
Ṙ # Reverse it
p # Prepend it to the other string
L * # Repeat it input-length number of times
?‛€?j # Join each character in the input by ".*?"
Ẏh # Get the first regex match
J-uby, 68 bytes
Again lost a few bytes by having to call Regexp.new with a string instead of using a Regexp literal with interpolation. Same technique as TField et al.
-[:+@|:*&"qazsxdcfvgbhnjmklpoiuytrew",~:join&".*?"|:new&Regexp]|+:[]
-[
:+@ | :* & "qazsxdcfvgbhnjmklpoiuytrew", # Repeat string times size of input
~:join & ".*?" | :new&Regexp # Also make a Regexp by joining input with ".*?"
] | # then
+:[] # call :[] with the two values, i.e. repeated_string[regexp]
Explore related questions
See similar questions with these tags.
dewqwertyis a valid path fordy. Could you confirm that? \$\endgroup\$