On Tue, 17 Jan 2012, Cedric Greevey wrote:

> On Tue, Jan 17, 2012 at 3:46 PM, Dennis Haupt <d.haup...@googlemail.com> 
> wrote:
> > after the "wtf"s have worn off a bit, go on reading.
> > imagine a simple problem: you have a collection of numbers and you have
> > to write a function which collects all the numbers that are contained
> > uneven times. for example, for a collection (1,2,3,2,3,4) the correct
> > result is (1,4)
[...]
> (defn odd-occurrences [x]
>   (map first
>     (filter
>       (fn [[_ v]] (odd? v))
>       (frequencies x))))

Haven't checked effiency, but my first thought is to go for something
like this, which seems fairly close (at least in terms of the fundmental
insight behind the algorithm) to your putative "child" solution:

  (defn odd-occurences-2 [coll]
    (reduce
      #(if (contains? %1 %2) (disj %1 %2) (conj %1 %2))
      #{} coll))

I think you'll see a lot of variance in the way children approach this, as
well...are you guessing what one would answer based on perceived
simplicity, or did you actually ask one?  Also, the way that the problem
is posed influences the answer you might get...the use of 'uneven' in
the problem description is something that anyone (child or adult) might
latch onto.

The above code seems very natural to me since comp sci courses frequently
have you design an FSA to detect oddness, and the phrasing is in a Clojure
idiom I've seen a lot.  It's fair to say that your knowledge, training, and
experience will influence the solutions you're likely to see, but I think a
non-programmer would not find the code above to be something they could
produce without a fair amount of thought and practice.

Cheers,
Paul

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to