Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The three rows of the qwerty keyboard are qwertyuiop, asdfghjkl and zxcvbnm. Your task is to find the longest word that can be typed using only one row of the keyboard, from a given list of words.

Sample input 1

artist
home
gas
writer
geology
marine
twerp

Output

writer

(Of the given words, only gas, writer and twerp can be written using a single row, and writer is the longest)

The words may not be actual words (so don't assume the third row to be invalid). However, you can assume that there will always be exactly one answer (no more, no less).

Sample input 2

wrhuji
bxnzmmx
gllwssjjd
vnccbb
lrkjhgfdsa
tttttt

Output

bxnzmmx

Additional punctuation and whitespaces can be provided in input (as per language requirements). However, no extra output should be given. Input and output are in lower case. Shortest code wins.

share|improve this question
    
@MartinBüttner I would really like to see how this could be solved with Retina. Do you think it is easily done? – Jerry Jeremiah Mar 17 at 5:51
    
Are the input words always in lowercase? – nimi Mar 17 at 5:59
    
@nimi Yes, they are. – ghosts_in_the_code Mar 17 at 6:20
    
@ghosts_in_the_code You should state that in the challenge for clarity – Luis Mendo Mar 17 at 9:00
6  
Relevant xkcd: what-if.xkcd.com/75 – Jan Dvorak Mar 17 at 17:35

16 Answers 16

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)
share|improve this answer
1  
1 / character more for Python 3 ;) – Antti Haapala Mar 17 at 14:17
6  
I didn't know the grammar allowed for no whitespace between 9 and for... – jogloran Mar 18 at 2:09
    
This is by far my favorite answer. – SBI Mar 18 at 14:12
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 Mar 18 at 14:35
    
@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 Mar 21 at 5:10

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.
share|improve this answer
8  
Woah, did you just outgolf Dennis? – Morgan Thrapp Mar 17 at 14:35
1  
This is going to be hard to beat – Adnan Mar 17 at 14:43
2  
Do you have "QWERTYUIOP\nASDFGHJKL\nZXCVBNM"a a predefined literal? Well played :-) – Luis Mendo Mar 17 at 14:58
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 Mar 17 at 15:54
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 Mar 17 at 16: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)))
share|improve this answer
5  
Welcome to PPCG, nice first post :) – FryAmTheEggman Mar 17 at 14:20
    
Why is the key variable there? I think you can remove it. – CalculatorFeline Mar 17 at 19:58
    
@CatsAreFluffy no, that is not possible. the key argument of max function is keyword-only. – Antti Haapala Mar 17 at 20:16
    
Ack, I forgot about kwargs. – CalculatorFeline Mar 17 at 20:21

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

share|improve this answer
    
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 Mar 17 at 22:33
    
@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 Mar 17 at 22:46
    
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 Mar 17 at 22:57
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 Mar 18 at 3:26
    
Never would have thought of o-r, awesome. – Psioniax Mar 19 at 6:29

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

share|improve this answer
    
My updated version now even beats the shorter lambda as a full program :) – SBI Mar 22 at 8:36
    
@SBI Curse Java's long method invocations! (All in good fun) – CAD97 Mar 22 at 14:12
    
Being a Java developer myself, it's nice to see a verbose language that manages to be concise at the same time :) – SBI Mar 22 at 14:26
    
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]*|[zxc‌​vbnm]*")).max((a,b)->a.length()-b.length()).get() (116 chars) – Andreas Mar 23 at 21:29
    
@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 Mar 23 at 21:48

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.
share|improve this answer

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'
share|improve this answer

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
share|improve this answer

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
share|improve this answer

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}
share|improve this answer
    
No, you add .split('\n') before the .select, right? And why no stabby lambdas? – CalculatorFeline Mar 17 at 17:28
    
I didn't know about the stabby lambda until just now, when you mentioned it. Thanks! – Kevin Lau - not Kenny Mar 17 at 17:34
    
Can you add a space between the -88- and 82? – CalculatorFeline Mar 17 at 19:56
    
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 Mar 18 at 7:48

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());
share|improve this answer
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. – DenkerAffe Mar 18 at 14:01
    
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 Mar 18 at 14:07
    
Added another alternative, using only Last with a predicate instead of using where. This shaves off another 8 bytes. – SBI Mar 21 at 11:16

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
share|improve this answer
    
The awk code can be written shorter as $0=length"\t"$0. – manatwork Mar 21 at 10:39

awk , 92 84 81 bytes

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

saved 3 bytes thanks to @Wolfgang suggestion

share|improve this answer
    
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 Mar 19 at 19:35
    
@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 Mar 21 at 4:42
    
@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 Mar 21 at 4:45
    
Are you sure the parenthesis around the regular expression are necessary? gawk and mawk are happy without them. – manatwork Mar 21 at 10:32
    
@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 Mar 21 at 10:40

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
share|improve this answer

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.

share|improve this answer

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]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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