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
@zingbretsen Okay, this is less of a criticism as it is a question of understanding:
You used the deque to rotate and slice the list for the multiplication, Could you have also used list comprehension something like this:
current_index = the one we are working on
[ value for idx, value in enumerate(list) if idx != current_idx]
I know you wanted to use the deque
A Python solution
@Absinthe Rather than maintaining state with the deque, it would probably be better to pass the index in to the product function.
A Python solution
@zingbretsen right... bbiab, errand time
A Python solution
@zingbretsen The only problem I have with all of these no-division solutions is that you have to do the multiplication in all cases. I wonder if there is a mathematical trick to somehow solve without having to calculate the whole product each time, like the division solution. @freemo ... any ideas?
A Python solution
@Absinthe @freemo there's probably a dynamic programming approach where you cache the results of sub problems.
Maybe a recursive solution where you split the filtered list in half and return the product of the result of calling the function on both halves. Python has a decorator you can use called lru_cache which can cache the results of calling a function with specific inputs. This way, when it hits a problem that it's already seen before, it can just serve the result up from the cache instead of recomputing.
A Python solution
@Absinthe Yes, you could do it that way. And that would also probably be better with the functional approach.