[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: Duplicate elements in list

William James

3/11/2016 7:53:00 PM

> > We have a list of elements, some are duplicates. We're trying to figure
> > out how to find the duplicate elements and increase a counter value by 1
> > for each instance of the element found. The list consists of lists with
> > two elements, the first being the incremental counter, the second being
> > the string sought. Example:
> >
> > ((1 "one") (1 "two") (1 "three") (1 "one") (1 "four") (1 "two"))
> >
> > The result should be:
> >
> > ((2 "one") (2 "two") (1 "three") (1 "four"))
> >
> >
> > This is the function for adding 1 (incremental counter element):
> >
> > (defun add-one-to-n (indata)
> > (setq n (car indata))
> > (setq outdata
> > (cons (+ 1 n) (cdr indata))))
> >
> > Any ideas on how we should go about achieving this?
>
> I'd use a hash table, much like this:
>
> (defun count (list)
> (let ((hash (make-hash-table)))
> (dolist (el list)
> (incf (gethash (cadr el) hash 0) (car el)))
> (let (result)
> (maphash (lambda (key val)
> (push (list val key) result))
> hash)
> result)))

MatzLisp (Ruby):

h = Hash.new(0)
[[1,"one"],[1,"two"],[1,"three"],[1,"one"],[1,"four"],[1,"two"]].
each{|n,s| h[s] += n}
h.to_a
===>
[["one", 2], ["two", 2], ["three", 1], ["four", 1]]

--
In Stockholm ... 20 Muslim men ... began to assault the children, ripping their
swimsuits off.... [T]he men cornered one of the [11-year-old] girls in a
grotto in the bathhouse and gang-raped her. The police refused to press any
charges. www.liveleak.com/view?i=807_1369627137