Longest word using a single row of the qwerty keyboard

30

4

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.

ghosts_in_the_code

Posted 2016-03-17T04:49:40.197

Reputation: 2 907

@MartinBüttner I would really like to see how this could be solved with Retina. Do you think it is easily done? – Jerry Jeremiah – 2016-03-17T05:51:01.073

Are the input words always in lowercase? – nimi – 2016-03-17T05:59:14.397

@nimi Yes, they are. – ghosts_in_the_code – 2016-03-17T06:20:33.110

@ghosts_in_the_code You should state that in the challenge for clarity – Luis Mendo – 2016-03-17T09:00:50.747

@JerryJeremiah it's doable but finding the largest of them will likely dominate the code and make it uncompetitive. I'll add an answer later today. – Martin Ender – 2016-03-17T10:25:05.673

So no newlines/ANSI color codes/other non-suppressible output? – CalculatorFeline – 2016-03-17T15:49:48.717

I wonder if the shortest way of handling the "row checking" is not to just list out all the keystrokes. If a language has built-in separation of vowels and consonants, that might be an advantage. Nearly half of the top row is every vowel except A; the next row contains A and S and all consonants from D through L. The bottom row is almost entirely pairs if you treat X and Z as next to each other in a list of consonants). – TOOGAM – 2016-03-17T17:16:07.003

6

Relevant xkcd: https://what-if.xkcd.com/75/

– John Dvorak – 2016-03-17T17:35:52.213

@JerryJeremiah There you go.

– Martin Ender – 2016-03-17T21:58:08.653

@MartinBüttner thanks so much. I am just facinated by retina but not competent enough to solve these problems myself. – Jerry Jeremiah – 2016-03-17T22:42:38.630

@MartinBüttner Can you please see my sandoboxed post here? It has not received enough attention and I'm not confident of actually posting it.

– ghosts_in_the_code – 2016-03-19T10:51:55.857

Answers

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)

xnor

Posted 2016-03-17T04:49:40.197

Reputation: 115 687

11 / character more for Python 3 ;) – Antti Haapala – 2016-03-17T14:17:05.193

6I didn't know the grammar allowed for no whitespace between 9 and for... – jogloran – 2016-03-18T02:09:08.603

This is by far my favorite answer. – SBI – 2016-03-18T14:12:02.920

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 – 2016-03-18T14:35:47.113

@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 – 2016-03-21T05:10:41.153

@Sp3000 I was browsing my older answers and found this, 4if 0else2 doesn't work in 2.5, 2.6 (and naturally not in 2.4 either as the syntax didn't exist there. Neither does it work in Python3s <3.3 – Antti Haapala – 2016-12-26T17:32:52.497

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.

ETHproductions

Posted 2016-03-17T04:49:40.197

Reputation: 47 880

8Woah, did you just outgolf Dennis? – Morgan Thrapp – 2016-03-17T14:35:35.457

1This is going to be hard to beat – Adnan – 2016-03-17T14:43:12.467

2Do you have "QWERTYUIOP\nASDFGHJKL\nZXCVBNM"a a predefined literal? Well played :-) – Luis Mendo – 2016-03-17T14:58:23.443

1I 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 – 2016-03-17T15:54:09.557

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 – 2016-03-17T16:11:40.020

1I was going to say that changing your language to have a built-in that trivializes the question is probably cheating, but it looks like it's had that since 0e43c90 on Jan 11th... – Tacroy – 2016-03-18T13:01:06.047

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

Antti Haapala

Posted 2016-03-17T04:49:40.197

Reputation: 341

5Welcome to PPCG, nice first post :) – FryAmTheEggman – 2016-03-17T14:20:33.037

Why is the key variable there? I think you can remove it. – CalculatorFeline – 2016-03-17T19:58:18.700

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

– Antti Haapala – 2016-03-17T20:16:28.933

Ack, I forgot about kwargs. – CalculatorFeline – 2016-03-17T20:21:16.217

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

Martin Ender

Posted 2016-03-17T04:49:40.197

Reputation: 184 808

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 – 2016-03-17T22:33:55.777

@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 – 2016-03-17T22:46:38.187

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 – 2016-03-17T22:57:21.793

1Probably 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 – 2016-03-18T03:26:37.633

Never would have thought of o-r, awesome. – Emanuel Vintilă – 2016-03-19T06:29:46.393

Are the parentheses in the first line needed? – Leaky Nun – 2016-03-31T04:39:48.230

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

CAD97

Posted 2016-03-17T04:49:40.197

Reputation: 1 367

My updated version now even beats the shorter lambda as a full program :) – SBI – 2016-03-22T08:36:50.577

@SBI Curse Java's long method invocations! (All in good fun) – CAD97 – 2016-03-22T14:12:09.220

Being a Java developer myself, it's nice to see a verbose language that manages to be concise at the same time :) – SBI – 2016-03-22T14:26:16.623

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 – 2016-03-23T21:29:11.200

@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 – 2016-03-23T21:48:02.457

We have to score the imports!? That is a deathblow for java ever to score well. I trimmed a couple chars around the regex, too: [wetyuio-r]*|[asdfghjkl]*|[zxc‌​vbnm]* – Andreas – 2016-03-23T22:04:46.297

@Andreas Java will never score well. ^()$ need to be in the regex to assert that we match the whole string, but yes, you managed to save another character (that I miscounted as not saving anything earlier)! Thank you!

– CAD97 – 2016-03-23T22:08:46.907

String::matches already matches the whole string. You're thinking of String::find – Andreas – 2016-03-23T22:19:18.787

@Andreas I have been illuminated. Sorry for not believing your (or at least testing my assumption).

– CAD97 – 2016-03-23T22:23:05.377

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'

Morgan Thrapp

Posted 2016-03-17T04:49:40.197

Reputation: 3 574

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.

Dennis

Posted 2016-03-17T04:49:40.197

Reputation: 196 637

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

AdmBorkBork

Posted 2016-03-17T04:49:40.197

Reputation: 41 581

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

Denker

Posted 2016-03-17T04:49:40.197

Reputation: 6 639

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}

Value Ink

Posted 2016-03-17T04:49:40.197

Reputation: 10 608

No, you add .split('\n') before the .select, right? And why no stabby lambdas? – CalculatorFeline – 2016-03-17T17:28:04.770

I didn't know about the stabby lambda until just now, when you mentioned it. Thanks! – Value Ink – 2016-03-17T17:34:59.317

Can you add a space between the -88- and 82? – CalculatorFeline – 2016-03-17T19:56:11.357

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 – 2016-03-18T07:48:42.940

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());

SBI

Posted 2016-03-17T04:49:40.197

Reputation: 211

1The 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 – 2016-03-18T14:01:59.763

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 – 2016-03-18T14:07:03.150

Added another alternative, using only Last with a predicate instead of using where. This shaves off another 8 bytes. – SBI – 2016-03-21T11:16:38.487

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

Olivier Dulac

Posted 2016-03-17T04:49:40.197

Reputation: 209

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 – 2016-03-19T19:35:47.043

@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 – 2016-03-21T04:42:41.003

@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 – 2016-03-21T04:45:28.767

Are you sure the parenthesis around the regular expression are necessary? gawk and mawk are happy without them. – manatwork – 2016-03-21T10:32:32.053

@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 – 2016-03-21T10:40:38.260

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

Wolfgang

Posted 2016-03-17T04:49:40.197

Reputation: 319

The awk code can be written shorter as $0=length"\t"$0. – manatwork – 2016-03-21T10:39:43.883

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

Luis Mendo

Posted 2016-03-17T04:49:40.197

Reputation: 87 464

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.

Dale Johnson

Posted 2016-03-17T04:49:40.197

Reputation: 509

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]

manatwork

Posted 2016-03-17T04:49:40.197

Reputation: 17 865