CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Fun With Perl: Perl Golf:
Dictionary - 10 non-repeating letters from /usr/dict/words

 



Jasmine
Administrator / Moderator

Feb 18, 2002, 8:57 PM

Post #1 of 2 (25800 views)
Dictionary - 10 non-repeating letters from /usr/dict/words Can't Post

Here's one of the examples provided at http://www.sysarch.com/perl/golf/. Try playing this yourself, then see other solutions at [url=http://www.sysarch.com/perl/golf/example.text]http://www.sysarch.com/perl/golf/example.text


In Reply To
From: Peter Marksteiner <hump@concealed>
Subject: Golf problem
Date: 28 Jun 2000 09:34:14 GMT

Suppose you want to find words with exactly ten non-repeating letters such
as "binoculars", "fishmonger", or "paintbrush", suitable for games or simple
encryption of numbers where every decimal digit is represented by a letter.

I've tried to write the shortest possible Perl program to extract
such words from a wordlist and have found the following two solutions:

perl -pe '%c=();length==11&!grep{$c{$_}++}split""or$_=""' /usr/dict/words
perl -pe '$_=""if(keys%{{map{$_,1}split""}})*length!=121' /usr/dict/words

Is there anybody who can do it with fewer keystrokes?

Peter



(This post was edited by Jasmine on Feb 18, 2002, 9:03 PM)


mhx
Enthusiast / Moderator

Feb 22, 2002, 1:58 PM

Post #2 of 2 (25784 views)
Re: [Jasmine] Dictionary - 10 non-repeating letters from /usr/dict/words [In reply to] Can't Post

Please ignore the redundant spaces in my code around the &&, I had to put them there because the forum seems to have some problems with it...

Here's my try:


Code
perl -ne'/^((.)(?!.*\2)){10}$/ && print'


The above will give the same output as the given examples, but it is unfortunately only correct from an ASCII point of view. As a bunch of words in the file start with capitals, you will find the following words in the output that have a repetition of the capital letter in them:


Code
Andromache 
Babylonize
Candlewick
Copernicus
Ektachrome
Gatlinburg
McLaughlin
McNaughton
Normanizes
Polyphemus
Richardson
Romanizers
Stravinsky


The following is slightly longer, but fixes the misbehaviour:


Code
perl -ne'/^((.)(?!.*\2)){10}$/i && print'


-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives