Okay, here is another freebie :) I will put in a real one before the end of the weekend.

Basically, you are given a list from which you need to create a new list where each element is the product of all the "OTHER" elements.

I found a few interesting corner cases.

I challenge you to give it a try!

Read the challenge here:

git.qoto.org/Absinthe/productn

My attempt is checked into that repo as well.

Follow

@Absinthe I didnt get the chance to do the last one... This one is super easy but the follow up is kina cool. I can see a way of doing the follow-up in log-time, anyone have anything better?

@freemo @Absinthe #programmingchallenge

Common Lisp again:

(defun excluded-product (sequence)
(let ((product (reduce #'* sequence :initial-value 1)))
(map 'list (lambda (x) (/ product x)) sequence)))

(defun excluded-product-without-division (sequence)
(flet ((prod (seq)
(reduce #'* seq :initial-value 1)))
(loop while (cdr sequence)
for tail = (cdr sequence) then (cdr tail)
for head = nil then (append head (list (pop sequence)))
collect (* (prod head) (prod tail)))))

@freemo @Absinthe
My attempt without division
git.sr.ht/~namark/mercury_stuf
It's linear but requires 3x the space. The idea is to calculate partial products of the list, from left to right, and right to left, then the final values based on corresponding neighbouring values in partial product lists.

Probably the worst explanation and my mercury code seems unreadable to me, so here's a c++ version
ix.io/1KzG/cpp

#toyprogrammingchallenge
#mercurylang #cpp

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.