[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: A simple Lisp program -- algorithm and speed issues

William James

5/5/2015 4:20:00 AM

Hrvoje Niksic wrote:

> Here is an interesting, not entirely academic problem that me and a
> colleague are "wrestling" with. Say there is a file, containing
> entries like this:
>
> foo 5
> bar 20
> baz 4
> foo 6
> foobar 23
> foobar 3
> ...
>
> There are a lot of lines in the file (~10000), but many of the words
> repeat (there are ~500 unique words). We have endeavored to write a
> program that would sum the occurences of each word, and display them
> sorted alphabetically, e.g.:
>
> bar 20
> baz 4
> foo 11
> foobar 26
> ...

Gauche Scheme:

(let1 table (make-hash-table 'string=?)
(with-input-from-file "delete-me.text" (lambda ()
(generator-for-each
(lambda (line)
(if-let1 m (rxmatch #/^(\S+) +(\S+)/ line)
(hash-table-update!
table (m 1) (cut + <> (string->number (m 2))) 0)))
read-line)))
(for-each
(cut apply format #t "~a ~a\n" <>)
(sort (hash-table-map table list) string<? car)))

===>
bar 20
baz 4
foo 11
foobar 26

--
The report card by the American Society of Civil Engineers showed the national
infrastructure a single grade above failure, a step from declining to the point
where everyday things simply stop working the way people expect them to. ---
washingtonpost.com/local/trafficandcommuting/us-infrastructure-gets-d-in-annual-report/2013/03/19/c48cb010-900b-11e2-9cfd-36d6c9b5d7ad_story.html
2 Answers

William James

11/29/2015 7:42:00 AM

0

WJ wrote:

> Hrvoje Niksic wrote:
>
> > Here is an interesting, not entirely academic problem that me and a
> > colleague are "wrestling" with. Say there is a file, containing
> > entries like this:
> >
> > foo 5
> > bar 20
> > baz 4
> > foo 6
> > foobar 23
> > foobar 3
> > ...
> >
> > There are a lot of lines in the file (~10000), but many of the words
> > repeat (there are ~500 unique words). We have endeavored to write a
> > program that would sum the occurences of each word, and display them

I think he means: sum the numbers associated with the words.

> > sorted alphabetically, e.g.:
> >
> > bar 20
> > baz 4
> > foo 11
> > foobar 26
> > ...

Ocaml:

#load "str.cma";;

let table = Hashtbl.create 999 ;;
let chan = open_in "data.txt" in
try
while true do
let [word; numstr] = Str.split (Str.regexp " +") (input_line chan) in
let num = int_of_string numstr in
try Hashtbl.replace table word (num + (Hashtbl.find table word))
with Not_found -> Hashtbl.add table word num
done
with End_of_file -> close_in chan ;;
Hashtbl.fold (fun k v acc -> (k,v)::acc) table []
|> List.sort compare
|> List.iter (fun (word,n) -> Printf.printf "%s %d\n" word n) ;;

bar 20
baz 4
foo 11
foobar 26

--
Elie [Wiesel] thus could have remained at Birkenau to await the Russians.
Although his father had permission to stay with him as a hospital patient or
orderly, father and son talked it over and decided to move out with the
Germans. --- Robert Faurisson

William James

2/3/2016 9:45:00 PM

0

WJ wrote:

> Hrvoje Niksic wrote:
>
> > Here is an interesting, not entirely academic problem that me and a
> > colleague are "wrestling" with. Say there is a file, containing
> > entries like this:
> >
> > foo 5
> > bar 20
> > baz 4
> > foo 6
> > foobar 23
> > foobar 3
> > ...
> >
> > There are a lot of lines in the file (~10000), but many of the words
> > repeat (there are ~500 unique words). We have endeavored to write a
> > program that would sum the occurences of each word, and display them
> > sorted alphabetically, e.g.:
> >
> > bar 20
> > baz 4
> > foo 11
> > foobar 26
> > ...
>
> Gauche Scheme:
>
> (let1 table (make-hash-table 'string=?)
> (with-input-from-file "delete-me.text" (lambda ()
> (generator-for-each
> (lambda (line)
> (if-let1 m (rxmatch #/^(\S+) +(\S+)/ line)
> (hash-table-update!
> table (m 1) (cut + <> (string->number (m 2))) 0)))
> read-line)))
> (for-each
> (cut apply format #t "~a ~a\n" <>)
> (sort (hash-table-map table list) string<? car)))
>
> ===>
> bar 20
> baz 4
> foo 11
> foobar 26

MatzLisp (Ruby):

table = Hash.new(0)
IO.foreach("input.dat"){|line| word,val = line.split; table[word] += val.to_i}
puts table.sort.map{|x| x.join " "}

===>
bar 20
baz 4
foo 11
foobar 26

--
77.6 percent of the country's rapists are identified as "foreigners".... And
even this likely understates the issue, since the Swedish government---in an
effort to obscure the problem---records second-generation Muslim perpetrators
simply as "Swedes." http://redicecreations.com/article.ph...