+ 1

How not to create duplicates in the first place instead of resorting to sorting and sets?

I've been trying to do one of the challenges of John Wells. (pool rotation) The question: In a list of 1 to 15, find all the sums that give 61, such that all numbers picked until the last one should mske it possible for you to have the same sum with the remaining numbers in the list, before you pick the last one. So if you had 5 numbers and if your target sum were 8, for the combo (1, 3, 4), order (1, 3) would have been a no no, and (1, 4) a yes, because, with (1, 4) you'd have (5, 3) remaining, making the sum still possible. I cannot quite tell where I'm getting the duplicates from(uncomment fstring to see). I think it's the for j in pows(A) bit that causes the problem: all combos for j in A = original ball list minus the last ball might have common elements with all combos in the next j. But maybe it's the function itself? Any help appreciated. Thanks. https://code.sololearn.com/cbtdw3MYdohf/?ref=ap

15th Aug 2022, 6:14 PM
Korkunç el Gato
Korkunç el Gato - avatar
5 Respuestas
+ 2
abpatrick catkilltsoi The original question link is at the top of the code and in the comments to that code. (I had space issues in discussion) But here you are: https://www.sololearn.com/post/1706125/?ref=app
15th Aug 2022, 7:04 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 1
Um…I quite get your code now, this is a very difficult question. Actually I got execute time out when running your code in sololearn. I suspicious that j does not include i (last Ball), then nj should include i already, why you add [I] to nj then. Also it might help if you use set only without touching any list.
15th Aug 2022, 7:37 PM
abpatrick catkilltsoi
abpatrick catkilltsoi - avatar
+ 1
abpatrick catkilltsoi i is also excluded from list A so I get the rest of A and then put back i(last ball) Changed the last bit of code to sort according to second element of tuple. It's not related but I thought I should say. Also, yeah, my tags are eto and Execution_Timed_Out
15th Aug 2022, 7:48 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 1
abpatrick catkilltsoi But how will that change execution time, I'm not sure I understand. You say to initialize a set without changing any list? But if I cannot remove duplicates until after loop how will that change things? Things get harder when we try to integrate what we know. May I ask about better ways to solve this but, with about this much knowledge? I think my design is bad but I cannot figure it out. Though pen and paper are available now, so I'll write it down with a smaller sum and list until I make sure what part produces the duplicates. It's not guaranteed though I'll be able to refactor it so as to get rid of dups on the fly.
15th Aug 2022, 7:56 PM
Korkunç el Gato
Korkunç el Gato - avatar
0
I think twice about the question, suggest that it should be clarified. Also the original question page cannot be accessed…
15th Aug 2022, 7:00 PM
abpatrick catkilltsoi
abpatrick catkilltsoi - avatar