As três linhas do teclado qwerty são qwertyuiop
, asdfghjkl
e zxcvbnm
. Sua tarefa é encontrar a palavra mais longa que pode ser digitada usando apenas uma linha do teclado, de uma determinada lista de palavras.
Entrada de amostra 1
artist
home
gas
writer
geology
marine
twerp
Saída
writer
(Das palavras dadas, única gas
, writer
etwerp
pode ser escrito usando uma única linha, e writer
é a mais longa)
As palavras podem não ser reais (não assuma a terceira linha como inválida). No entanto, você pode assumir que sempre haverá exatamente uma resposta (nem mais nem menos).
Entrada de amostra 2
wrhuji
bxnzmmx
gllwssjjd
vnccbb
lrkjhgfdsa
tttttt
Saída
bxnzmmx
Pontuação e espaços em branco adicionais podem ser fornecidos na entrada (conforme os requisitos de idioma). No entanto, nenhuma saída extra deve ser fornecida. Entrada e saída estão em minúsculas. O menor código vence.
Respostas:
Python 2, 84 bytes
Finds the
max
of the input, comparing by fewer keyboard rows spanned, then in increasing length. The keyboard row value is extracted by"asdfghjklzxcvbnm".find(c)/9
, which takes the middle row to0
, the bottom row to1
, and the top row, which is excluded, to-1
, sincefind
gives-1
for missing values.Other attempts:
fonte
/
character more for Python 3 ;)9
andfor
...E
ore
, you can pretty much always remove whitespace between a number and a variable name/keyword4if 0else 2
is valid in 2.7.11 and 3.5.1 (and probably has been valid for a few versions prior)Japt,
3230 bytesTest it online! Input is an array of strings.
How it works
fonte
"QWERTYUIOP\nASDFGHJKL\nZXCVBNM"
a a predefined literal? Well played :-)D
is set toQWERTYUIOP\nASDFGHJKL\nZXCVBNM
, even the page you refer to seems to stateVariables <...> D 13
;
at the beginning of the program resets variablesA-L
to various values.D
is set to the keyboard string. You can find more info here.Python 2.5+ and 3, 93 bytes
Had to test how many strokes for this approach; this uses the fact that
a.strip(b)
results in empty string ifa
solely consists of characters that occur in b.The function takes list of strings and returns a string.
fonte
key
variable there? I think you can remove it.key
argument ofmax
function is keyword-only.Retina, 73 bytes
Try it online!
Conclusion: Retina needs a sorting stage.
Explanation
This is a grep stage: it only keeps lines which are matched by the regex. I.e. those which are formed exclusively from one of those character classes.
Now we just need to find the largest of the remaining strings. We do this by matching all words which are at least as long than all the words after them. The
1
is a new addition to Retina (released two days ago), which limits this match stage to considering only the first such match. And!
instructs Retina to print the match (instead of counting it).fonte
(?<a-b>...)
syntax which is rarely needed in code golf.o-r
, awesome.Java,
154142 or142130 bytesBecause, ya know, Java.
C#, for comparison.
146 bytes if input has to be a single string with values separated by
\n
:134 bytes if I can assume input as String[] instead:
Slightly ungolfed:
Second lambda is a
Function<String[],String>
.fonte
l->l.stream().filter(g->g.matches("[wertyuio-r]*|[asdfghjkl]*|[zxcvbnm]*")).max((a,b)->a.length()-b.length()).get()
(116 chars)import java.util.*;
in the byte count, meaning taking a list is -16 bytes by taking a List but +19 to import the List. HOWEVER, you did catch there usingmax
instead ofreduce
for a gain of -7 bytes.Jelly,
4034 bytesTry it online!
How it works
fonte
Python 3, 98
Saved 5 bytes thanks to Kevin.
Saved 3 bytes thanks to PM 2Ring.
Saved 3 bytes thanks to Antti Haapala.
Brute forcing it at the moment. I filter the words down to only those contained by a single row, and then sort for max string length.
Test cases:
fonte
PowerShell v2+, 72 bytes
Takes input via command-line arguments as
$args
, then uses the-match
operator with a regex to select only the words that are exclusively made up of one keyboard row. We pipe those results intoSort-Object
that sorts by the propertyLength
. We can do this since strings in PowerShell are all of theSystem.String
type, which includes.Length
as a sortable property. This sorts the strings into ascending order by length, so we take the last one with[-1]
, leave it on the pipeline, and output is implicit.Example
fonte
Pyth,
4535 bytesThanks to @FryAmThe Eggman for saving me some bytes!
Try it here!
Takes input as a list of words.
Explanation
fonte
Ruby,
888269If I'm not allowed to take a list of strings and must take a multiline string, add +12 to the score and add
.split('\n')
right before the.grep
call.Thanks CatsAreFluffy for teaching me about stabby lambdas in Ruby, and further optimizations from manatwork
fonte
.split('\n')
before the.select
, right? And why no stabby lambdas?.select
's code block is to match it against a regular expression,.grep
is more suitable; no need to put parenthesis around the last method's parameters in a call chain;.length
has a shorter alias,.size
:->x{x.grep(/^([o-rwetyui]+|[asdfghjkl]+|[zxcvbnm]+)$/).max_by &:size}
C#, 141 / 112 / (120 bytes)
Contender for worst golfing language, for obvious reasons. Uses "my" locale with qwertz instead of qwerty but works fine otherwise.
Full program without where:
Only output without Where:
Only output (original):
fonte
bash
, 105 bytesAnd various other utilities, of course.
fonte
awk
code can be written shorter as$0=length"\t"$0
.awk ,
928481 bytessaved 3 bytes thanks to @Wolfgang suggestion
fonte
[wetyuio-r]
instead, and also two more by doing/^(expr|expr|expr)$/
instead of `/^expr$|^expr$|^expr$/gawk
andmawk
are happy without them.MATL, 54 bytes
This works with current version (14.0.0) of the language/compiler.
Input format is (first example)
or (second example)
Try it online!
Explanation
fonte
Perl, 81 bytes
$a=$1 if/^([wetyuio-r]+|[asdfghjkl]+|[zxcvbnm]+)$/&&1<<y///c>$a=~y///c;END{say$a}
Symbol to letter count pretty high.
fonte
Groovy, 65 characters
Sample run:
Note that the regular expression used by
.grep()
not requires anchoring, allowing to spare the grouping too:fonte