# Code ran out of execution time

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.)

Note by Num Ic
7 months, 2 weeks ago

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:

• Use the emojis to react to an explanation, whether you're congratulating a job well done , or just really confused .
• Ask specific questions about the challenge or the steps in somebody's explanation. Well-posed questions can add a lot to the discussion, but posting "I don't understand!" doesn't help anyone.
• Try to contribute something new to the discussion, whether it is an extension, generalization or other idea related to the challenge.

MarkdownAppears as
*italics* or _italics_ italics
**bold** or __bold__ bold
- bulleted- list
• bulleted
• list
1. numbered2. list
1. numbered
2. list
Note: you must add a full line of space before and after lists for them to show up correctly
paragraph 1paragraph 2

paragraph 1

paragraph 2

[example link](https://brilliant.org)example link
> This is a quote
This is a quote
    # I indented these lines
# 4 spaces, and now they show
# up as a code block.

print "hello world"
# I indented these lines
# 4 spaces, and now they show
# up as a code block.

print "hello world"
MathAppears as
Remember to wrap math in $$ ... $$ or $ ... $ to ensure proper formatting.
2 \times 3 $2 \times 3$
2^{34} $2^{34}$
a_{i-1} $a_{i-1}$
\frac{2}{3} $\frac{2}{3}$
\sqrt{2} $\sqrt{2}$
\sum_{i=1}^3 $\sum_{i=1}^3$
\sin \theta $\sin \theta$
\boxed{123} $\boxed{123}$

Sort by:

@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?

- 7 months, 2 weeks ago

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'>

- 7 months, 2 weeks ago

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?

- 7 months, 2 weeks ago

do you mean printing in a loop: for p in perms...?

- 7 months, 2 weeks ago

i did this:

a = [1,2,3,6]
c = permutations(a)
b = list(permutations(a))
print(a)
print("b:", *b)
print("c:", *c)


- 7 months, 2 weeks ago

you used the unpacking operator to unpack all values from an iterator (e. g. list oder itertools object)

- 7 months, 2 weeks ago

and by the way: don't use Brilliant's Coding Environment; the interpreter is very slooooooooooooow. Try repl.it

- 7 months, 2 weeks ago

good hint ty.

- 7 months, 2 weeks ago

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.

- 7 months, 2 weeks ago

Thank you for the hint. I deleted the second account made by mistake....

- 7 months, 1 week ago

and by the way: thanks for the compliment! But I am only an experienced beginner...

- 7 months, 1 week ago

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?

- 7 months, 1 week ago

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}")

- 7 months, 1 week ago

ty. i m glad to hear that you are ok.

- 7 months, 1 week ago

Umm... You can use PythonTutor's Visualiser as well

- 3 months, 3 weeks ago

good hint. thank you.
i tried the code for todays solution that just executes all 10000 cases and got this:

Stopped after running 1000 steps. Please shorten your code, since Python Tutor is not designed to handle long-running code.

i reduced the code and saw that it can be run step by step.
this is very helpful. thank you

- 3 months, 3 weeks ago

Welcome. I use the visualiser to see where I made a mistake in the code by (kind of) zooming it into the part which isn't functioning correctly

- 3 months, 3 weeks ago