Why does the creation of this list in the coding environment cause a "Code ran out of execution time" error?
the same list is created in two different ways, but one creation leads to that error.
method one (leads to an error):
from itertools import permutations
nums = [2, 1, 0, 0, -1, -1, -2, -2, -3, 7]
perms = list(permutations(nums))
method two (no error):
from itertools import permutations
nums = [2, 1, 0, 0, -1, -1, -2, -2, -3, 7]
perms = permutations(nums)
the first method seems to create a list from a list, but the result seems to be the same.
So where does the error happens?
(to check if the output is the same, a permutation of up to 9 values can be used.
the runtime error happens when at least 10 values are permutated.)
Easy Math Editor
This discussion board is a place to discuss our Daily Challenges and the math and science related to those challenges. Explanations are more than just a solution — they should explain the steps and thinking strategies that you used to obtain the solution. Comments should further the discussion of math and science.
When posting on Brilliant:
*italics*
or_italics_
**bold**
or__bold__
paragraph 1
paragraph 2
[example link](https://brilliant.org)
> This is a quote
\(
...\)
or\[
...\]
to ensure proper formatting.2 \times 3
2^{34}
a_{i-1}
\frac{2}{3}
\sqrt{2}
\sum_{i=1}^3
\sin \theta
\boxed{123}
Comments
@Carsten Kaminski have u seen this in the daily challenge of Nov 3rd? if you can spare some time: can you help me to understand why "perms = list(permutations(nums))" cause a runtime error?
Log in to reply
the code is correct in both ways. The first snippet creates a very huge list with 10! elements; lists handling is very slow in Python (lists in Python are more dynamic arrays or linked lists). The second one is an itertools object with generates output by demand; type(perms): <class 'itertools.permutations'>
Log in to reply
ok. i thought both create lists. i will check the docu of the datatypes to understand the difference.
when i print them (based on 4 elements), both show 4! elements. is this wrong?
Log in to reply
Log in to reply
Log in to reply
and by the way: don't use Brilliant's Coding Environment; the interpreter is very slooooooooooooow. Try repl.it
Log in to reply
good hint ty.
there is another Carsten Kaminski here, same name, but not your picture.
you always create very impressive code. today i thought there is no way to solve the triangle problem with good looking code. but u proved me wrong: even for this problem u created very cool code. ty for presenting your creative way of coding. to read your codes is a great enrichment.
Log in to reply
Thank you for the hint. I deleted the second account made by mistake....
Log in to reply
hello Carsten, i tried to re-read your solution for "How Many Ones", but it is vanished.
did u delete it? or is there a bug?
Log in to reply
Hi, I was not really satisfied with the code... print(f"{str(bin(p:=int('1'*9,2) * int('11', 2)).count('1'))} ones in {p:b}")
Log in to reply
Umm... You can use PythonTutor's Visualiser as well
Log in to reply
good hint. thank you.
i tried the code for todays solution that just executes all 10000 cases and got this:
i reduced the code and saw that it can be run step by step.
this is very helpful. thank you
Log in to reply