Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

:Re: Comments requested on noob code

9 views
Skip to first unread message

Robert L.

unread,
Nov 29, 2016, 7:24:45 AM11/29/16
to
Peter Seibel wrote:

> John Landahl <jo...@landahl.org> writes:
>
> > Peter Seibel wrote:
> >> Oh, and find-in-file has a bug, it's passing RACK instead of
> >> SORTED-RACK to MATCH-WORD. Dunno if it'll run faster or slower when
> >> you fix that but at least it'll be correct.
> >
> > I did fix that. And by the way, I tried it first without string-downcase
> > and it didn't make that big of a difference time/memory-wise.
>
> Okay, here's my best offer. Basically the same algorithm as your
> original but without all the needless list creation.
>
> (defun find-in-file (file-name rack)
> (with-open-file (in file-name)
> (loop for word = (read-line in nil nil) while word
> when (match-word word rack) collect word)))
>
> (defun match-word (word rack)
> (loop with tiles = (copy-seq rack)
> for char across word
> always (find char tiles :test #'char-equal)
> do (setf tiles (delete char tiles :test #'char-equal :count 1))))


MatzLisp (Ruby):

def match_word word, rack
rack = rack.dup
word.chars.all?{|c| rack.sub!(c,"")}
end

def find_in_file filename, rack
IO.readlines(filename).select{|word|
match_word( word.chomp, rack)}.
map(&:chomp)
end

t=Time.now;p find_in_file("words", "smug");Time.now - t

["g", "gum", "gums", "m", "mg", "ms", "mu", "mug", "mus", "s", "sg",
"sm", "smug", "su", "sum", "u", "us"]
==>0.75


--
When the Israeli bombers and torpedo-planes were sent to attack and destroy the
ship, the Jewish commander, seeing that it was an American vessel, had
misgivings and reported to the High Command, which simply repeated the orders
to attack and sink the Liberty. www.revilo-oliver.com/rpo/Bit_of_Good_News.html
0 new messages