Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

what is the best language to do this in?

Name: Anonymous 2010-01-14 19:13

I have a set of data:
Sally has attribute A and attribute B
Bob has attribute D and attribute R
John has attribute S and attribute T
Melissa has attribute G and attribute M

etc.  There are over 300 names and 21 different attributes (let's call them A, B, ..., T, U) in varying combinations.

I want to pick out a set of 5 names with 2 attributes each that fit in a pattern of (for example) A,B,C,D,E,F,H,I,J,K

What's the best way to go about this?  I'm programming language neutral, though I do have some experience in bash, python and perl.  It would have to take input from a text file (either tab delimited or csv) and output which names to pick and what their attributes are.

Name: Anonymous 2010-01-15 0:52


(defun build-attribute-hashtable (person-attribute-alist)
  (let ((attribute-hashtable (make-hash-table :test #'equal)))
    (dolist (person-attribute-pair person-attribute-alist attribute-hashtable)
      (destructuring-bind (name . attributes) person-attribute-pair
          (dolist (attribute attributes)
            (push name (gethash attribute attribute-hashtable)))))))

(declaim (inline lookup-attribute))
(defun lookup-attribute (attribute attribute-hashtable)
  (gethash attribute attribute-hashtable))

(defun lookup-persons-with-attributes (attributes attribute-hashtable)
  (remove-duplicates
   (loop
      for attribute in attributes
      appending (lookup-attribute attribute attribute-hashtable))
   :test #'equal))
;;; And an example usage scenario:

;;; You'll normally have to parse the input somehow, but for
;;; simplicity's sake, I'm using an alist here.
;(defparameter *person-attributes*
;  '(("Sally" . (;; one could use strings instead of keywords if required
;                :a :b))
;    ("Bob" . (:d :r))
;    ("John" . (:s :t))
;    ("Melissa" . (:g :m)))
;  "Person attribute association list.")

;CL-USER> (build-attribute-hashtable *person-attributes*)
;#<HASH-TABLE :TEST EQUAL :COUNT 8 {24CB7489}>

;;; Upon inspection, the hashtable looks like this:

;#<HASH-TABLE {24CB7489}>
;--------------------
;Count: 8
;Size: 16
;Test: EQUAL
;Rehash size: 1.5
;Rehash threshold: 1.0
;[clear hashtable]
;Contents:
;:A = ("Sally") [remove entry]
;:B = ("Sally") [remove entry]
;:D = ("Bob") [remove entry]
;:G = ("Melissa") [remove entry]
;:M = ("Melissa") [remove entry]
;:R = ("Bob") [remove entry]
;:S = ("John") [remove entry]
;:T = ("John") [remove entry]

;CL-USER> (lookup-persons-with-attributes '(:a :b :d :r :s) *)
;("Sally" "Bob" "John")

Easy isn't it?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List