[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[QUIZ] Posix Pangrams (#97

James Gray

10/6/2006 1:19:00 PM

The three rules of Ruby Quiz:

1. Please do not post any solutions or spoiler discussion for this quiz until
48 hours have passed from the time on this message.

2. Support Ruby Quiz by submitting ideas as often as you can:

http://www.rub...

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone
on Ruby Talk follow the discussion. Please reply to the original quiz message,
if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

by Martin DeMello

A pangram is a sentence containing every letter of the alphabet at least once (a
famous example in English being "the quick brown fox jumps over the lazy dog").
For maximum style points a pangram should read smoothly, and have both as few
repeated letters as possible (ideally zero), and as few words as possible.

This quiz extends the idea to the posix utilities[1] - write a program to find
pangrammatic collections of posix utilities that (1) use the fewest utilities
and (2) have the minimum number of repeated letters. In either case, break ties
on the other criterion; that is, your first solution should also have as few
repeated letters as possible, and your second one should use as few utilities as
possible.

[1] http://www.unix.org/version3/ap... has a complete list

21 Answers

Martin Coxall

10/6/2006 1:54:00 PM

0

> Interesting. I had a high school chemistry teacher who had us play
> similar games with symbols of the periodic table.
>

It's really all about trying to make swearwords, isn't it? Copper is
your friend.

On the unix front, I nominate 'mingetty'.

Martin

David Vallner

10/7/2006 2:59:00 AM

0

Martin Coxall wrote:
> It's really all about trying to make swearwords, isn't it? Copper is
> your friend.
>
> On the unix front, I nominate 'mingetty'.
>

My innocent eyes!

*hurries to cover the pickle jar with small white round objects floating
in formaldehyde with a cloth*

David Vallner

Christian Neukirchen

10/7/2006 12:13:00 PM

0

Logan Capaldo <logancapaldo@gmail.com> writes:

> Interesting. I had a high school chemistry teacher who had us play
> similar games with symbols of the periodic table.

We use to play that because the lessons are so boring. :)

Longest English words made up from chemical symbols:

THErMoPHOsPHOrEsCEnCe
NONRePReSEnTaTIONAlISm

Longest German words made up from chemical symbols:

INFLaTiONSINdIKAtOReN
AuSLaNdSINVEsTiTIONeN
KNOTeNReCHNErAPPLiKAtION
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...

Hal E. Fulton

10/7/2006 7:12:00 PM

0

Christian Neukirchen wrote:
> Logan Capaldo <logancapaldo@gmail.com> writes:
>
>
>>Interesting. I had a high school chemistry teacher who had us play
>>similar games with symbols of the periodic table.
>
>
> We use to play that because the lessons are so boring. :)
>
> Longest English words made up from chemical symbols:
>
> THErMoPHOsPHOrEsCEnCe
> NONRePReSEnTaTIONAlISm
>
> Longest German words made up from chemical symbols:
>
> INFLaTiONSINdIKAtOReN
> AuSLaNdSINVEsTiTIONeN
> KNOTeNReCHNErAPPLiKAtION


I love that kind of wordplay. I appreciate your
sharing it.


Thanks,
Hydrogen Aluminum


Christian Neukirchen

10/8/2006 8:36:00 PM

0

Ruby Quiz <james@grayproductions.net> writes:

> A pangram is a sentence containing every letter of the alphabet at least once (a
> famous example in English being "the quick brown fox jumps over the lazy dog").
> For maximum style points a pangram should read smoothly, and have both as few
> repeated letters as possible (ideally zero), and as few words as possible.
>
> This quiz extends the idea to the posix utilities[1] - write a program to find
> pangrammatic collections of posix utilities that (1) use the fewest utilities
> and (2) have the minimum number of repeated letters. In either case, break ties
> on the other criterion; that is, your first solution should also have as few
> repeated letters as possible, and your second one should use as few utilities as
> possible.

Who has a better solution than these?

"awk chgrp df jobs lex mv tty uniq zcat"
"awk chgrp df join lex mv tty qsub zcat"

I think these are optimal for both (1) and (2).
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...

Morton Goldberg

10/9/2006 12:10:00 AM

0

Here is my solution. It's not what I planned. I intended to submit an
algorithm that would find all the minimal Posix pangrams, and I
worked out a rather nice (IMO) recursive algorithm to do just that.
But Ruby doesn't allocate enough stack space for that algorithm to
run to completion <sigh>. So I've settled on an algorithm that finds
just one minimal pangram, but uses pseudo-random numbers to produce a
different pangram each time it is run.

My best pangram so far is

[
"fg", "jobs", "qhold", "stty", "umask",
"unexpand", "vi", "write", "zcat"
]

Regards, Morton

<code>
#! /usr/bin/env ruby -w
#
# Created by Morton Goldberg on October 08, 2006.
#
# Ruby Quiz 97 -- POSIX Pangrams
# quiz_97.3.rb

class Array

def shuffle!
n = size - 1
while n > 0
k = rand(n)
self[k], self[n] = self[n], self[k]
n -= 1
end
self
end

end

# Removed c99, fort77, and m4 -- the complication they add is IMHO
# unedifying.
WORDS = %w[
admin alias ar asa at awk basename batch bc bg cal cat cd cflow
chgrp chmod chown cksum cmp comm command compress cp crontab csplit
ctags cut cxref date dd delta df diff dirname du echo ed env ex
expand
expr false fc fg file find fold fuser gencat get getconf getopts
grep hash head iconv id ipcrm ipcs jobs join kill lex link ln locale
localedef logger logname lp ls mailx make man mesg mkdir mkfifo more
mv newgrp nice nl nm nohup od paste patch pathchk pax pr printf
prs ps
pwd qalter qdel qhold qmove qmsg qrerun qrls qselect qsig qstat qsub
read renice rm rmdel rmdir sact sccs sed sh sleep sort split strings
strip stty tabs tail talk tee test time touch tput tr true tsort tty
type ulimit umask unalias uname uncompress unexpand unget uniq
unlink
uucp uudecode uuencode uustat uux val vi wait wc what who write
xargs
yacc zcat
]

# Return true if _wds_ is a pangram.
def pangram?(wds)
wds.join.split(//).uniq.size == 26
end

# Return array giving pangram statistics:
# [<words>, <total-chars>, <repeated-chars>]
def stats(pan)
tmp = pan.join.split(//)
[pan.size, tmp.size, tmp.size - tmp.uniq.size]
end

# Given a pangram, return list of pangrams, where each panaram in the
list
# is derived from the given one by removing one word.
def diminish(pan)
result = pan.collect do |item|
rest = pan - [item]
rest if pangram?(rest)
end
result.compact.shuffle!
end

# Given a list of pangrams return a minimal pangram that can be derived
# from it.
def find_minimal(pans)
pan = pans.pop
reduced = diminish(pan)
return pan if reduced.empty?
find_minimal(reduced)
end

# Find a minimal pangram.
pangram = find_minimal([WORDS])
p pangram # =>
# [
# "fg", "jobs", "qhold", "stty", "umask",
# "unexpand", "vi", "write", "zcat"
# ]
p stats(pangram) # => [9, 39, 13]
</code>


Morton Goldberg

10/9/2006 12:59:00 PM

0

I woke up this morning with the realization that the code I posted
yesterday could be considerably simplified. Here is the simpler version.

Regards, Morton

<code>
#! /usr/bin/env ruby -w
#
# Created by Morton Goldberg on October 09, 2006.
#
# Ruby Quiz 97 -- POSIX Pangrams
# quiz_97.4.rb

# Removed c99, fort77, and m4 -- the complication they add is IMHO
# unedifying.
WORDS = %w[
admin alias ar asa at awk basename batch bc bg cal cat cd cflow
chgrp chmod chown cksum cmp comm command compress cp crontab csplit
ctags cut cxref date dd delta df diff dirname du echo ed env ex
expand
expr false fc fg file find fold fuser gencat get getconf getopts
grep hash head iconv id ipcrm ipcs jobs join kill lex link ln locale
localedef logger logname lp ls mailx make man mesg mkdir mkfifo more
mv newgrp nice nl nm nohup od paste patch pathchk pax pr printf
prs ps
pwd qalter qdel qhold qmove qmsg qrerun qrls qselect qsig qstat qsub
read renice rm rmdel rmdir sact sccs sed sh sleep sort split strings
strip stty tabs tail talk tee test time touch tput tr true tsort tty
type ulimit umask unalias uname uncompress unexpand unget uniq
unlink
uucp uudecode uuencode uustat uux val vi wait wc what who write
xargs
yacc zcat
]

# Return true if _wds_ is a pangram.
def pangram?(wds)
wds.join.split(//).uniq.size == 26
end

# Return array giving pangram statistics:
# [<words>, <total-chars>, <repeated-chars>]
def stats(pan)
tmp = pan.join.split(//)
[pan.size, tmp.size, tmp.size - tmp.uniq.size]
end

# Given a pangram, return a pangram derived from it by removing one
word.
def remove_one(pan)
result = pan.collect do |item|
diff = pan - [item]
diff if pangram?(diff)
end
result.compact!
result[rand(result.size)] unless result.empty?
end

# Given a pangram return a minimal pangram derived from it.
def find_minimal(pan)
nxt = remove_one(pan)
return pan unless nxt
find_minimal(nxt)
end

# Find a minimal pangram.
pangram = find_minimal(WORDS)
p pangram # =>
# [
# "expr", "getconf", "jobs", "mv", "qdel",
# "type", "unlink", "what", "zcat"
# ]
p stats(pangram) # => [9, 39, 13]
</code>

camerooni

10/9/2006 4:09:00 PM

0

I haven't found a solution with fewer repeated characters, but

"zcat stty jobs cxref newgrp iconv cksum qhold"

uses one fewer utility

Christian Neukirchen wrote:
> Ruby Quiz <james@grayproductions.net> writes:
>
> Who has a better solution than these?
>
> "awk chgrp df jobs lex mv tty uniq zcat"
> "awk chgrp df join lex mv tty qsub zcat"
>
> I think these are optimal for both (1) and (2).
> --
> Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...

Martin Coxall

10/9/2006 4:16:00 PM

0

On 10/9/06, camerooni@gmail.com <camerooni@gmail.com> wrote:
> I haven't found a solution with fewer repeated characters, but
>
> "zcat stty jobs cxref newgrp iconv cksum qhold"
>

Isn't it supposed to use every letter?

Martin

camerooni

10/9/2006 4:23:00 PM

0

I'm under the impression that it does? What letter am I missing?

-Cameron

Martin Coxall wrote:
> On 10/9/06, camerooni@gmail.com <camerooni@gmail.com> wrote:
> > I haven't found a solution with fewer repeated characters, but
> >
> > "zcat stty jobs cxref newgrp iconv cksum qhold"
> >
>
> Isn't it supposed to use every letter?
>
> Martin