[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

re: William James Asked Me to Post From The Trenches #23b

William James

10/18/2015 2:02:00 PM

Kenneth Tilton wrote:

> The precise fun part tho is typing it all in in the final form versus
> dividing the thing up into steps to get intermediate results, ie, a test
> of one's mastery of one's language. Non-functional languages I guess
> have no choice but to stop and assign temporaries.
>
> Anyway, I have to stop encouraging my client, he specifies requirements
> like a woman ordering in a restaurant. Here goes:
>
> Given:
>
> (defparameter pets
> '((dog ((blab 12)(glab 17)(cbret 82)(dober 42)(gshep 25)))
> (cat ((pers 22)(siam 7)(tibet 52)(russ 92)(meow 35)))
> (snake ((garter 10)(cobra 37)(python 77)(adder 24)(rattle 40)))
> (cow ((jersey 200)(heiffer 300)(moo 400)))))
>
> Write:
>
> (defun digest-tag-population (tag-population pick-tags count)...)
>
> Such that:
>
> (digest-tag-population pets '(dog cat snake) 5)
>
> => ((DOG CBRET 82) (DOG DOBER 42) (CAT RUSS 92) (CAT TIBET 52) (SNAKE
> PYTHON 77))
>
> ...the rules being:
>
> - consider only the populations of tags (the first symbol in each
> sublist) found in the parameter pick-tags, a list
>
> - take only the <count> most populous of the union of the populations
>
> - return (tag name population) of the most populous in this order:
>
> firstly, by position of the tag in pick-tags
> second, ie within a tag, in descending order of population
>
> Scroll waaaaaaaaay down for my code.
>
> Remember, you have to code this in one go.
>
> kt
>
> (defun subseq-ex (st e s)
> (subseq s st (min e (length s))))
>
> (defun digest-tag-population (tag-population pick-tags count)
> (flet ((tagpos (tag) (position tag pick-tags)))
> (stable-sort (subseq-ex 0 count
> (sort (loop for (tag population) in tag-population
> when (tagpos tag)
> append (loop for pop in population
> collecting (list* tag pop)))
> '> :key (lambda (x)
> (caddr x))))
> '< :key (lambda (x) (tagpos (car x))))))
>
> (defparameter pets
> '((dog ((blab 12)(glab 17)(cbret 82)(dober 42)(gshep 25)))
> (cat ((pers 22)(siam 7)(tibet 52)(russ 92)(meow 35)))
> (snake ((garter 10)(cobra 37)(python 77)(adder 24)(rattle 40)))
> (cow ((jersey 200)(heiffer 300)(moo 400)))))
>
> #+test
> (digest-tag-population pets '(dog cat snake) 5)


MatzLisp (Ruby):

require 'pp'

pets =
"dog blab 12 glab 17 cbret 82 dober 42 gshep 25
cat pers 22 siam 7 tibet 52 russ 92 meow 35
snake garter 10 cobra 37 python 77 adder 24 rattle 40
cow jersey 200 heiffer 300 moo 400".
lines.map{|line| line.split}.map{|type, *rest|
[type, *rest.each_slice(2).map{|breed,quan| [breed, quan.to_i]}]}

def digest_tag_population pets, pick_tags, quantity
pets.select{|type, *breeds| pick_tags.include?(type)}.
flat_map{|type, *breeds| breeds.map{|xs| [type, *xs]}}.
sort_by{|x| -x.last}[0,quantity].
sort_by{|x| [pick_tags.index(x.first), -x.last]}
end

pp digest_tag_population(pets, %w(dog cat snake), 5)

[["dog", "cbret", 82],
["dog", "dober", 42],
["cat", "russ", 92],
["cat", "tibet", 52],
["snake", "python", 77]]

pp digest_tag_population(pets, %w(dog cat snake), 99)

[["dog", "cbret", 82],
["dog", "dober", 42],
["dog", "gshep", 25],
["dog", "glab", 17],
["dog", "blab", 12],
["cat", "russ", 92],
["cat", "tibet", 52],
["cat", "meow", 35],
["cat", "pers", 22],
["cat", "siam", 7],
["snake", "python", 77],
["snake", "rattle", 40],
["snake", "cobra", 37],
["snake", "adder", 24],
["snake", "garter", 10]]

--
You have politicians saying that ... as many Africans as want to come into
Sweden should be able to come.... They've already said that everybody from
Syria can come to Sweden.... [T]hey are actually thinking of commandeering
people's vacation homes because they need more housing for immigrants.
--- Dr. Kevin MacDonald (http://lnrlive.com/tpc/tpc201...)