Palavra mais longa usando uma única linha do teclado qwerty


30

As três linhas do teclado qwerty são qwertyuiop, asdfghjkle 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, writeretwerp 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.


@ MartinBüttner Eu realmente gostaria de ver como isso poderia ser resolvido com a Retina. Você acha que é fácil?
Jerry Jeremiah

As palavras de entrada estão sempre em minúsculas?
N

@nimi Sim, eles são.
ghosts_in_the_code

@ghosts_in_the_code Você deve indicar que no desafio para maior clareza
Luis Mendo

Respostas:


18

Python 2, 84 bytes

lambda l:max(l,key=lambda w:(-len({"asdfghjklzxcvbnm".find(c)/9for c in w}),len(w)))

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 to 0, the bottom row to 1, and the top row, which is excluded, to -1, since find gives -1 for missing values.

Other attempts:

lambda l:max((-len({"asdfghjklzxcvbnm".find(c)/9for c in w}),len(w),w)for w in l)[2]
lambda l:max(l,key=lambda w:len(w)-1./len({"asdfghjklzxcvbnm".find(c)/9for c in w}))
lambda l:max([w for w in l if len({"asdfghjklzxcvbnm".find(c)/9for c in w})<2],key=len)

1
1 / character more for Python 3 ;)
Antti Haapala

6
I didn't know the grammar allowed for no whitespace between 9 and for...
jogloran

This is by far my favorite answer.
SBI

1
@jogloran, so long as the characther is not an E or e, you can pretty much always remove whitespace between a number and a variable name/keyword
wnnmaw

@wnnmaw That's no longer true for newer versions of Python, e.g. 4if 0else 2 is valid in 2.7.11 and 3.5.1 (and probably has been valid for a few versions prior)
Sp3000

13

Japt, 32 30 bytes

;Uf_¬£DbXu)f10Ãä¥ eÃn@Yl -XlÃg

Test it online! Input is an array of strings.

How it works

;Uf_  ¬ £  DbXu)f10Ã ä¥  eà n@  Yl -Xlà g
;UfZ{Zq mX{DbXu)f10} ä== e} nXY{Yl -Xl} g

         // Implicit: U = input array of strings
;        // Reset variables A-L to various values.
         // D is set to the string "QWERTYUIOP\nASDFGHJKL\nZXCVBNM".
UfZ{   } // Take U and filter to only the items Z that return truthily to this function:
Zq       //  Split Z into chars, then
mX{    } //  map each char X by this function:
DbXu)    //   Return D.indexOf(X.toUpperCase()),
f10      //   floored to a multiple of 10.
         //  This maps each char to 0 for the top row, 10 for the middle, 20 for the bottom.
q ä==    //  Split the resulting string into chars and check each pair for equality.
e        //  Check that every item in the result is truthy. This returns true if all chars
         //  are on the same row; false otherwise.
         // Now we have only the words that are entirely on one row.
nXY{   } // Sort by passing each two args X and Y into this function:
Yl -Xl   //  Return Y.length - X.length. Sorts the longest to the front.
g        // Get the first item in the resulting array. Implicitly output.

8
Woah, did you just outgolf Dennis?
Morgan Thrapp

1
This is going to be hard to beat
Adnan

2
Do you have "QWERTYUIOP\nASDFGHJKL\nZXCVBNM"a a predefined literal? Well played :-)
Luis Mendo

1
I can't seem to find where it is stated that D is set to QWERTYUIOP\nASDFGHJKL\nZXCVBNM, even the page you refer to seems to state Variables <...> D 13
sukhmel

1
@sukhmel A ; at the beginning of the program resets variables A-L to various values. D is set to the keyboard string. You can find more info here.
ETHproductions

11

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 if a solely consists of characters that occur in b.

The function takes list of strings and returns a string.

lambda a:max(a,key=lambda x:(~all(map(x.strip,['qwertyuiop','asdfghjkl','zxcvbnm'])),len(x)))

5
Welcome to PPCG, nice first post :)
FryAmTheEggman

Why is the key variable there? I think you can remove it.
CalculatorFeline

@CatsAreFluffy no, that is not possible. the key argument of max function is keyword-only.
Antti Haapala

Ack, I forgot about kwargs.
CalculatorFeline

8

Retina, 73 bytes

G`^([eio-rtuwy]+|[adfghjkls]+|[bcmnvxz]+)$
1!`(.)+(?!\D+(?<-1>.)+(?(1)!))

Try it online!

Conclusion: Retina needs a sorting stage.

Explanation

G`^([eio-rtuwy]+|[adfghjkls]+|[bcmnvxz]+)$

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.

1!`(.)+(?!\D+(?<-1>.)+(?(1)!))

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).


This is cool! I'll have to look at balanced groups sometime. Although they seem to be hard to understand. I've tried this (and it looks like it works), but I wanted to wait for your answer to see what kind of thing will you create.
daavko

@daavko Sorry for the self-promotion, but have you read my Stack Overflow post on balancing groups? I'm being told it's a good introduction. The concept itself really isn't that complicated, especially if you don't use the (?<a-b>...) syntax which is rarely needed in code golf.
Martin Ender

I don't think I've seen that post. Most likely because I don't browse Stack Overflow often. Thanks for the link, I'll bookmark it and read it.
daavko

1
Probably off topic for a comment, but pretty much 100% of my knowledge of balancing groups comes from having read your post. I think you can self-promote it any time, it's a great help :) Anyway more on topic, would sort stage work something like <code>O-1`.*</code> to get the line with the longest input? Maybe it should work like transliteration and have more than one backtick separated region, a regex to split on and one to count? Actually maybe this also belongs in chat... ¯_(ツ)_/¯
FryAmTheEggman

Never would have thought of o-r, awesome.
Emanuel Vintilă

6

Java, 154 142 or 142 130 bytes

Because, ya know, Java.

C#, for comparison.

146 bytes if input has to be a single string with values separated by \n:

s->java.util.Arrays.stream(s.split("\n")).filter(g->g.matches("[wetyuio-r]*|[asdfghjkl]*|[zxcvbnm]*")).max((a,b)->a.length()-b.length()).get()

134 bytes if I can assume input as String[] instead:

s->java.util.Arrays.stream(s).filter(g->g.matches("[wetyuio-r]*|[asdfghjkl]*|[zxcvbnm]*")).max((a,b)->a.length()-b.length()).get()

Slightly ungolfed:

UnaryOperator<String> longestQwertyStr = s -> 
        java.util.Arrays.stream(s.split("\n")) // Split string input over `\n` and convert to Stream<String>
                .filter(g->g.matches("[wetyuio-r]*|[asdfghjkl]*|[zxcvbnm]*")) // Filter to Strings that only use characters from a single row
                .max((a,b)->a.length()-b.length()) // Find the max by comparing String length
                .get(); // Convert from Optional<String> to String and implicit return single statement lambda

Second lambda is a Function<String[],String>.


My updated version now even beats the shorter lambda as a full program :)
SBI

@SBI Curse Java's long method invocations! (All in good fun)
CAD97

Being a Java developer myself, it's nice to see a verbose language that manages to be concise at the same time :)
SBI

If we're going to make an input assumption, lets make the one that helps us the most, input comes in as a List<String>:l->l.stream().filter(g->g.matches("[wertyuio-r]*|[asdfghjkl]*|[zxcvbnm]*")).max((a,b)->a.length()-b.length()).get() (116 chars)
Andreas

@Andreas As per my recent meta discussion, if a lambda takes a List, you have to include 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 using max instead of reduce for a gain of -7 bytes.
CAD97

4

Jelly, 40 34 bytes

p“£vẈ¬ḣ“£AS°GƤg“£ḷḳƤ²ƤȤḤ»f/€fµL€Mị

Try it online!

How it works

p“£vẈ¬ḣ“£AS°GƤg“£ḷḳƤ²ƤȤḤ»f/€fµL€Mị

 “£vẈ¬ḣ“£AS°GƤg“£ḷḳƤ²ƤȤḤ»           Use dictionary compression to yield
                                    ['quipo twyer', 'adj flash jg', 'bcmnz xv'].
p                                   Cartesian product; for all pairs of an input
                                    string and one of the rows.
                         f/€        Reduce each pair by filter, keeping only the
                                    letters in the input string that are on that
                                    particular keyboard row.
                            f       Filter the results, keeping only filtered words
                                    that occur in the input.
                             µ      Begin a new chain.
                              L€    Get the length of each kept word.
                                M   Get the index corr. to the greatest length.
                                 ị  Retrieve the word at that index.

4

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.

lambda a:max(a,key=lambda x:(any(map(set(x).__le__,['qwertyuiop','asdfghjkl','zxcvbnm'])),len(x)))

Test cases:

assert f(['asdf', 'qwe', 'qaz']) == 'asdf'
assert f('''artist
home
gas
writer
geology
marine
twerp'''.splitlines()) == 'writer'
assert f('''wrhuji
bxnzmmx
gllwssjjd
vnccbb
lrkjhgfdsa
tttttt'''.splitlines()) == 'bxnzmmx'

3

PowerShell v2+, 72 bytes

($args-match"^([qwertyuiop]+|[asdfghjkl]+|[zxcvbnm]+)$"|sort Length)[-1]

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 into Sort-Object that sorts by the property Length. We can do this since strings in PowerShell are all of the System.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

PS C:\Tools\Scripts\golfing> .\longest-word-qwerty-keyboard.ps1 asdf qwe zxc typewriter halls establishment
typewriter

3

Pyth, 45 35 bytes

Thanks to @FryAmThe Eggman for saving me some bytes!

elDf}k-LTc."`z:I¿Ç  Ì(T4²ª$8·"\`Q

Try it here!

Takes input as a list of words.

Explanation

elDf}k-LTc."..."\`Q   # Q = list of all input words

   f              Q   # Filter input with T as lambda variable
         c."..."\`    # List of all keyboard rows
      -LT             # Remove all letters of the current input row from the current input
                      # word. Results in a list of 3 string with one being empty if
                      # the word can be typed with one row
    }k                # Check if the list contains an emtpy string
elD                   # order result list by length and take the last

3

Ruby, 88 82 69

If 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

->x{x.grep(/^([o-rwetyui]+|[asdfghjkl]+|[zxcvbnm]+)$/).max_by &:size}

No, you add .split('\n') before the .select, right? And why no stabby lambdas?
CalculatorFeline

I didn't know about the stabby lambda until just now, when you mentioned it. Thanks!
Value Ink

Can you add a space between the -88- and 82?
CalculatorFeline

No need to assign it to a variable, anonymous functions are allowed; if the only thing to do inside .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}
manatwork

3

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:

static void Main(string[]a){Console.WriteLine(a.OrderBy(x=>x.Length).Last(x=>Regex.IsMatch(x,"^([qwertzuiop]+|[asdfghjkl]+|[yxcvbnm]+)$")));}

Only output without Where:

Console.WriteLine(a.OrderBy(x=>x.Length).Last(x=>Regex.IsMatch(x,"^([qwertzuiop]+|[asdfghjkl]+|[yxcvbnm]+)$")));

Only output (original):

Console.WriteLine(a.Where(x=>Regex.IsMatch(x,"^([qwertzuiop]+|[asdfghjkl]+|[yxcvbnm]+)$")).OrderBy(x=>x.Length).Last());

1
The challenge says you have to use querty and since you are hardcoding this anyway, I don't see any reason to change that. Also you can use a function (maybe even a lambda) instead of a full program to save some bytes. This is always allowed unless explicitly forbidden in the challenge.
Denker

Being proud of my layout :P, it's not like I have a chance at winning anyway using C#. Edited in a version that's only the output.
SBI

Added another alternative, using only Last with a predicate instead of using where. This shaves off another 8 bytes.
SBI

2

bash, 105 bytes

And various other utilities, of course.

egrep -x '[wetyuio-r]+|[asdfghjkl]+|[zxcvbnm]+'|awk '{print length($0)"\t"$0;}'|sort -n|cut -f2|tail -n1

The awk code can be written shorter as $0=length"\t"$0.
manatwork

2

awk , 92 84 81 bytes

(/^([wetyuio-r]+|[asdfghjkl]+|[zxcvbnm]+)$/)&&length>length(a){a=$0}END{print a}  

saved 3 bytes thanks to @Wolfgang suggestion


You can subtract a byte by using [wetyuio-r] instead, and also two more by doing /^(expr|expr|expr)$/ instead of `/^expr$|^expr$|^expr$/
Wolfgang

@Wolfgang: thanks for the tip. I tried already the 2nd one and on my awk it gave me the longest word instead of the right one... I'll try again. I agree with the first one though, saves 1 byte
Olivier Dulac

@Wolfgang: hmm, does work this time (I may have fumbled the parenthesis on my own test, friday). I edit your tips in, thanks.
Olivier Dulac

Are you sure the parenthesis around the regular expression are necessary? gawk and mawk are happy without them.
manatwork

@manatwork: the inner ones are necessary, the outer one I prefer to have them to ensure the logic and evaluation order. .. maybe at the "cost" of 2 chars
Olivier Dulac

1

MATL, 54 bytes

[]y"@Y:nh]2$SP"@Y:!t'asdfghjkl'mw'zxcvbnm'myy+~hhAa?@.

This works with current version (14.0.0) of the language/compiler.

Input format is (first example)

{'artist' 'home' 'gas' 'writer' 'geology' 'marine' 'twerp'}

or (second example)

{'wrhuji' 'bxnzmmx' 'gllwssjjd' 'vnccbb' 'lrkjhgfdsa' 'tttttt'}

Try it online!

Explanation

[]               % push empty array. Will be used for concatenation
y                % take input array implicitly at bottom of stack, and copy onto top
"                % for each string
  @Y:            %   push current string
  nh             %   get its length. Concatenate with array of previous lengths
]                % end for each
2$S              % sort the original copy of input array by increasing string length
P                % flip: sort by decreasing length
"                % for each string in decreasing order of length
  @Y:!           %   push that string as a column char vector
  t'asdfghjkl'm  %   duplicate. Tru for chars in 2nd row of keyboard
  w'zxcvbnm'm    %   swap. True for chars in 3rd row of keyboard
  yy+~           %   duplicate top two arrays, sum, negate: true for chars in 1st row
  hh             %   concatenate horizontally twice
  Aa             %   true if any column has all true values
  ?              %   if that's the case
    @            %     push string  
    .            %     break for each loop
                 %   end if implicitly
                 % end for each
                 % display implicitly

1

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.


0

Groovy, 65 characters

{it.grep(~/[o-rwetyui]+|[asdfghjkl]+|[zxcvbnm]+/).max{it.size()}}

Sample run:

groovy:000> ({it.grep(~/[o-rwetyui]+|[asdfghjkl]+|[zxcvbnm]+/).max{it.size()}})(['wrhuji', 'bxnzmmx', 'gllwssjjd', 'vnccbb', 'lrkjhgfdsa', 'tttttt'])
===> bxnzmmx

Note that the regular expression used by .grep() not requires anchoring, allowing to spare the grouping too:

groovy:000> ['ab', 'ac', 'bc', 'abc', 'aca', 'bbc'].grep ~/[ac]+|b+/
===> [ac, aca]
Ao utilizar nosso site, você reconhece que leu e compreendeu nossa Política de Cookies e nossa Política de Privacidade.
Licensed under cc by-sa 3.0 with attribution required.