Skip to content

Instantly share code, notes, and snippets.

@andrewwoods
Last active July 6, 2024 02:57
Show Gist options
  • Save andrewwoods/44a265e6272606e708ab6f099487bb7e to your computer and use it in GitHub Desktop.
Save andrewwoods/44a265e6272606e708ab6f099487bb7e to your computer and use it in GitHub Desktop.
The longest word that can be typed on the top row of a QWERTY keyboard

The Longest Word

Someone mentioned recently that "typewriter was the longest word that can be typed on the top row of a qwerty keyboard". I thought that was interesting. I was curious to know what other words were in the running. So I thought would write some code to find out.

The List of Words

First, I need a list of all the words in the English language. I didn't have to look very far. Every Linux and Mac system has a list available. On my 2023 16-inch Macbook Pro, it lives at /usr/share/dict/words. My mac is set to US English.

Second, I need to filter that list so that it only has words made up of the letters qwertyuiop. To get this, you just need a little regular expression magic. I always start looking at grep for this. I try what I expect work. When it doesn't, I re-learn for the ump-teenth time, that grep on Mac doesn't support PCRE (Perl Compatible Regular Expressions). No matter how you feel about Perl, their regular expression engine is the one that every other programming language emulates. It turns out that Mac has a program called pcre2grep, which does exactly what I want.

pcre2grep '^[qwertyuiop]+$' /usr/share/dict/words > qwertyuiop.txt

The Script

I wrote a CLI script using PHP. It's designed to read STDIN. Here's how you run the script.

cat qwertyuiop.txt | php sort_by_word_length.php

The Results

It turns out that typewriter is pretty long at 10 letters. Here are "all" the 10-letter words. I use quotes here, becuase it's possible that the dictionary used to make my /usr/share/dict/words is missing some words from other regions and locales.

pepperroot
pepperwort
perpetuity
pewterwort
pirouetter
prerequire
pretorture
proprietor
repertoire
repetitory
tetterwort
typewriter

There are however, 3 words with 11 letters. So we have a 3-way tie!

proprietory
proterotype
rupturewort

If you're thinking - I've never even heard of these words - you're not alone. So here's what proprietory , proterotype, and rupturewort mean.

e
eer
eerie
epee
epitrite
epitrope
epopee
epopt
equerry
equip
equipper
equity
er
ere
erept
err
errite
error
erupt
etiquette
etui
eu
eurite
ewe
ewer
ewerer
ewery
ewry
ey
eye
eyepit
eyer
eyeroot
eyewort
eyey
eyot
eyoty
eyre
eyrie
eyrir
i
ie
iiwi
io
ire
irrupt
it
iter
iturite
iyo
o
oe
oer
oii
ootype
ope
operette
opt
optotype
or
ore
orrery
ort
ortet
ory
otter
otterer
otto
our
ourie
out
outer
outeye
outpeep
outpeer
outpipe
outpity
outpop
outport
outporter
outpour
outpourer
outpry
output
outputter
outquote
outre
outrooper
outroot
outrow
outtire
outtop
outtower
outtrot
outweep
outwit
outwitter
outwoe
outwore
outwrite
ow
owe
ower
oyer
p
pee
peeoy
peep
peeper
peepeye
peepy
peer
peerie
peery
peetweet
peewee
pep
pepo
pepper
pepperer
pepperroot
pepperwort
peppery
peppy
per
peri
peripety
periptery
perique
perit
perite
perpetuity
perrier
perry
pert
perty
pet
pete
peter
peterwort
petit
petite
petitor
petitory
peto
petre
petreity
petrie
petter
petty
pew
pewee
pewit
pewter
pewterer
pewterwort
pewtery
pewy
peyote
pi
pie
pier
pierrot
piet
piety
piewipe
piotty
pioury
pip
pipe
piper
pipery
pipet
pipette
pipewort
pipi
pipiri
pipit
pipper
pippy
pipy
pique
piquet
piqure
pir
piripiri
pirouette
pirouetter
pirr
pit
pitier
pitpit
pitter
pittite
pituite
pituri
pity
piuri
po
poe
poet
poetito
poetry
poi
poop
poor
poot
pop
pope
popery
popeye
popper
poppet
poppy
poppywort
pore
porer
porite
poroporo
porotype
porr
porret
porry
port
porter
portiere
portio
portitor
porto
porty
pory
pot
pote
poter
poteye
potoo
potoroo
potpie
potpourri
pott
potter
potterer
pottery
potto
potty
potwort
pour
pourer
pourie
pout
pouter
pouty
pow
power
powwow
powwower
poy
poyou
prep
prequote
prereport
prerequire
preroute
prerupt
preterit
pretire
pretorture
pretry
pretty
prewire
prey
preyer
prier
prior
priorite
priority
priory
pro
proo
prop
proper
property
propopery
propper
proprietor
proprietory
propriety
prore
prote
proterotype
proto
protopope
protore
prototype
protutor
protutory
protype
prow
prut
pry
pryer
pu
puerer
puerpery
pup
puppet
puppeteer
puppetry
puppy
pur
pure
puree
purer
puriri
purity
purport
purpure
purpurite
purr
purre
purree
purrer
purry
put
putt
puttee
putter
putterer
puttier
putty
puttyroot
puture
pyoureter
pyr
pyre
pyrite
pyro
pyrope
q
qere
qeri
queer
queerer
queerity
queery
queet
quei
querier
query
quet
queue
quey
quiet
quieter
quip
quipo
quipper
quippy
quipu
quire
quirt
quit
quite
quitter
quittor
quo
quoit
quoiter
quop
quot
quote
quotee
quoter
quotiety
quotity
r
re
ree
reeper
reet
reit
reiter
reoutput
rep
repertoire
repertory
repetitory
repew
repipe
repique
repope
report
reporter
repot
repour
repp
repute
require
requirer
requit
requite
requiter
requote
reree
reroot
rerope
reroute
rerow
ret
retepore
retie
retier
retip
retire
retirer
retort
retorter
retorture
retour
retree
retrip
retrot
retrue
retry
retter
rettery
rettory
retype
reutter
rewet
rewire
rewrite
rewriter
rie
rier
rio
riot
rioter
riotry
rip
ripe
riper
ripier
ripper
rippet
rippier
rippit
ripup
riroriro
rit
rite
roe
roer
roey
roi
roit
root
rooter
rootery
rooty
rope
roper
roperipe
ropery
ropp
ropy
roque
roquer
roquet
roquette
rorty
rory
rot
rote
roter
roto
rotor
rotter
roue
roup
rouper
roupet
roupit
roupy
rout
route
router
row
rower
rowet
rowport
rowty
rowy
royet
royt
rue
ruer
ruewort
rupee
rupie
rupture
rupturewort
ruru
rut
ruttee
rutter
rutty
rye
ryot
rype
t
te
tee
teer
teerer
teet
teeter
teeterer
teety
tepee
tepor
terete
tereu
terp
terret
terrier
territory
terror
terry
terutero
tete
tetter
tetterwort
tettery
tew
tewer
tewit
ti
tie
tier
tierer
tip
tipe
tipiti
tippee
tipper
tippet
tippy
tipteerer
tiptoe
tiptop
tiptopper
tipup
tire
tirer
tirr
tirret
tirrwirr
tirwit
tit
tite
titer
titi
titre
titter
titterer
tittery
tittie
tittup
tittupy
titty
to
toe
toetoe
toi
toit
toity
too
toop
toorie
tooroo
toot
tooter
top
tope
topee
topepo
toper
topi
topo
topotype
topper
toppy
toprope
toque
tor
tore
torero
torii
toro
torotoro
torpor
torque
tort
torture
torturer
toru
tory
tot
tote
toter
toto
totquot
totter
totterer
tottery
totty
toty
tou
toup
toupee
toupet
tour
tourer
tourette
tourte
tout
touter
tow
tower
towerwort
towery
towrope
towy
toy
toyer
toywort
tree
treetop
treey
tret
trey
tri
trier
trio
trior
trip
tripe
tripery
tripper
trippet
triptote
triptyque
tripy
trite
tritor
troop
trooper
troot
trope
troper
trot
trotter
trottie
trottoir
trotty
troupe
trouper
trout
trouter
trouty
trow
troy
true
truer
try
tryout
tryp
tryt
tu
tue
tui
tup
tuque
tur
turio
turp
turr
turret
tururi
tut
tute
tutee
tutor
tutorer
tutory
tutoyer
tutti
tutty
tutu
tuwi
tuyere
twee
tweet
tweeter
twere
twerp
twire
twit
twite
twitter
twitterer
twittery
twitty
two
tye
tyee
type
typer
typewrite
typewriter
typo
typp
typy
tyre
tyro
tyt
u
up
upo
upper
upperer
uppity
uppop
uppour
upprop
uprip
uproot
uprooter
uproute
uptie
uptower
uptree
ur
ure
ureter
urite
urtite
ut
uteri
utter
utterer
utu
w
we
wee
weep
weeper
weepy
weet
weewow
weir
wept
wer
were
weri
wert
wet
wetter
wey
wi
wipe
wiper
wir
wire
wirer
wirr
wiry
wit
wite
witter
witty
wo
woe
woo
wooer
wop
wore
worrier
worrit
worriter
worry
wort
wot
wote
wow
wowt
woy
wrier
writ
write
writee
writer
writter
wro
wrote
wry
wup
wur
wut
wy
wye
wype
y
ye
yee
yeo
yep
yer
yere
yet
yew
yip
yirr
yite
yo
yoe
yoi
yoop
yor
yore
yot
yote
you
youp
your
yow
yowie
yowt
yoy
yperite
yr
yurt
yutu
<?php
/**
* Sort By Word Length
*
* This script keeps the original alphabetical order, but
* also groups words together by their length.
*/
function read_the_data(): array
{
$lines = [];
while ($line = fgets(STDIN)) { // noted STDIN here is not a string
$lines[] = trim($line);
}
return $lines;
}
function sort_by_length(array $input): array
{
$output = [];
$buckets = [];
$max_length = 1;
foreach ($input as $line) {
$word_length = strlen($line);
if ($word_length > $max_length) {
$max_length = $word_length;
}
$buckets[$word_length][] = $line;
}
foreach (range(1, $max_length) as $i) {
if (isset($buckets[$i])) {
foreach ($buckets[$i] as $item) {
$output[] = $item;
}
}
}
return $output;
}
function main(): void
{
$input = read_the_data();
$output = sort_by_length($input);
foreach ($output as $line) {
echo "$line\n";
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment