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:
https://git.qoto.org/Absinthe/productnot/blob/master/README.md
My attempt is checked into that repo as well.
A Python solution
@Absinthe Here is my #toyprogrammingchallenge no-division solution:
```
#!/usr/bin/env python
from collections import deque
from functools import reduce
from random import randint
def get_product_and_rotate(d):
"""Gets product of all but the first items in the queue"""
prod = reduce(lambda a, b: a * b, list(d)[1:])
d.rotate(-1) # Rotates left so that the next
return prod
if __name__ == "__main__":
n_items = randint(2, 10)
input_list = [randint(0, 9) for _ in range(n_items)]
print(input_list)
product_queue = deque(input_list)
print([get_product_and_rotate(product_queue) for _ in input_list])
```
Uses `reduce` for the multiplication, and uses a `deque` (double-ended queue) to rotate the list. I've read that using a deque is actually more efficient than just taking slices out of the list.
A Python solution
@zingbretsen What about this... as a one liner:
[reduce(lambda a, b: a *b, [ y for x, y in enumerate(list) if x != idx]) for idx, _ in enumerate(list)]
A Python solution
@Absinthe looks right to me
A Python solution
@zingbretsen This works...
print("One Liner no division")
print([reduce(lambda a, b: a *b,
[ y for x, y in enumerate(random_list) if x != idx])
for idx, _ in enumerate(random_list)])
It's a one liner, except I am sticking to 80 columns so I split it a few times.. still calling it a one liner :)
A Python solution
@Absinthe looks good.
I'll try to do something with the dynamic programming approach tomorrow or Monday.
For something like multiplication, I don't think it will make much of a difference in the execution time, but for a more expensive operation it would
re: A Python solution
I can’t think of any way to avoid the multiplications.
Here’s the lisp solution using rotate
. Destructive, so ugly, but actually leaves the input list the same as it came in. Conses only the result list.
(defun rotate (list)
(prog1 (nconc (cdr list) list)
(setf (cdr list) nil)))
(defun excluded-product-with-rotation (list)
(loop with l = list
collect (reduce #'* (cdr l) :initial-value 1)
do
(setf l (rotate l))
(when (eq l list) (loop-finish))))
re: A Python solution
@billstclair @zingbretsen Hey bill please remember to include #toyprogrammingchallenge hashtag in your responses so people can find them by searching for it.
Can I run this in gcl or emacs or do I need more code to have the list and output?
re: A Python solution
re: A Python solution
@billstclair @zingbretsen I have access to gcl but it looks like I need more to make this function
re: A Python solution
Works for me:
wws@Xossbow:~/lisp/toy-programming-challenge$ git remote -v
origin https://github.com/billstclair/toy-programming-challenge.git (fetch)
origin https://github.com/billstclair/toy-programming-challenge.git (push)
wws@Xossbow:~/lisp/toy-programming-challenge$ ls
beer.lisp excluded-product.lisp LICENSE README.md zero-to-nine.lisp
wws@Xossbow:~/lisp/toy-programming-challenge$ gcl
GCL (GNU Common Lisp) 2.6.12 CLtL1 Oct 29 2015 23:21:28
Source License: LGPL(gcl,gmp), GPL(unexec,bfd,xgcl)
Binary License: GPL due to GPL'ed components: (XGCL READLINE UNEXEC)
Modifications of this banner must retain notice of a compatible license
Dedicated to the memory of W. Schelter
Use (help) to get some basic information on how to use GCL.
Temporary directory for compiler files:
/tmp/
>(load "excluded-product.lisp")
Loading excluded-product.lisp
Finished loading excluded-product.lisp
T
>(apropos "excluded" *package*)
EXCLUDED-PRODUCT Function
EXCLUDED-PRODUCT-WITH-ROTATION Function
EXCLUDED-PRODUCT-WITHOUT-DIVISION Function
>(excluded-product '(1 2 3 4))
(24 12 8 ...)
>*print-length*
3
>(setf *print-length* nil)
NIL
>***
(24 12 8 6)
>(excluded-product-with-rotation '(1 2 3 4))
(24 12 8 6)
>
re: A Python solution
@billstclair @zingbretsen right, so I had to do more than just run it :) It just gave me some funtions to call. I got it now.
re: A Python solution
@billstclair @zingbretsen got it
re: A Python solution
@billstclair @zingbretsen excellent! I can't wait.
re: A Python solution
I'm going to do the next one in Elm, working example published at https://ellie-app.com
#toyprogrammingchallenge