Show newer

@Absinthe how do you feel about importing modules for these challenges? One standard lib module would make the no-division part really elegant.

@masterofthetiger@theres.life @Absinthe
One last style thing is that python has some syntactic sugar regarding mathematical operations so you could do:
output[x] *= n

Which multiplies output[x] by n and stores the value back in outout[x].

A minor thing, but is generally the more pythonic way of doing it

Finished code (Python) 

@masterofthetiger@theres.life @Absinthe and style-wise, i is usually used as the index when looping over numbers, so it's confusing that it's storing your values here. Also, since this is something that's so common to do, python provides a way of looping over both the index and the value at the same time: enumerate

for i, elem in enumerate(input):
stuff

Finished code (Python) 

@masterofthetiger@theres.life @Absinthe
I haven't tried running it yet, but I'm pretty sure you would get the wrong answer if the input list had repeated elements, e.g., [5,1,2,3,4,5]. Because you are checking that n != i, where i is the current element in the input list, if that element occurs more than once, you will not include that in the product. I believe the question wants you to exclude only the element in that given position on the list, not all elements of that value.

Finished code (Python) 

@masterofthetiger@theres.life @Absinthe would you mind if I gave you some feedback on your code?

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.

@Absinthe @freemo ah, I used f-strings for formatting which isn't available in 3.5--they're only available in 3.6+

@Absinthe @freemo I forked the repo and added my solution here: git.qoto.org/zingbretsen/cocon

It should solve the 5 person problem when run as-is, but it could be run for any number of people. I should probably have added that as a command line argument.

It works backwards from possible ending amounts, but is somewhat selective in the numbers it tries. e.g., for the 5 person case, we know that the ending pile must be divisible by both 4 and 5, so the final pile will be a multiple of 20. In addition to that, we know that 5/4 + 1 of the ending pile must be divisible by 5, so 60 is the lowest number that would work for the first round of stepping backwards.

There is a regular pattern that seems to work for all of the numbers of people I tested, not just 5, so we can reduce the search space pretty significantly. For the 5 person problem, I only have to test 13 numbers before finding the answer.

@Absinthe @freemo I did it like your second friend (i.e., the nested function, checking that the result is an integer).

CleverLibre Social

CleverLibre Social is an inclusive social instance for open discussion, learning, and community.
All cultures welcome.
Hate speech and harassment strictly forbidden.