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, 1 week ago

No vote yet
1 vote

  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:

  • 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.
  • Stay on topic — we're all here to learn more about math and science, not to hear about your favorite get-rich-quick scheme or current world events.

MarkdownAppears as
*italics* or _italics_ italics
**bold** or __bold__ bold

- bulleted
- list

  • bulleted
  • list

1. numbered
2. 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 1

paragraph 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×3 2 \times 3
2^{34} 234 2^{34}
a_{i-1} ai1 a_{i-1}
\frac{2}{3} 23 \frac{2}{3}
\sqrt{2} 2 \sqrt{2}
\sum_{i=1}^3 i=13 \sum_{i=1}^3
\sin \theta sinθ \sin \theta
\boxed{123} 123 \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?

num IC - 7 months, 1 week ago

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

A Former Brilliant Member - 7 months, 1 week ago

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?

num IC - 7 months, 1 week ago

Log in to reply

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

A Former Brilliant Member - 7 months, 1 week ago

Log in to reply

@A Former Brilliant Member i did this:

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

num IC - 7 months, 1 week ago

Log in to reply

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

A Former Brilliant Member - 7 months, 1 week ago

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

A Former Brilliant Member - 7 months, 1 week ago

Log in to reply

good hint ty.

num IC - 7 months, 1 week 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.

num IC - 7 months, 1 week ago

Log in to reply

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

Log in to reply

@A Former Brilliant Member and by the way: thanks for the compliment! But I am only an experienced beginner...

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?

num IC - 7 months ago

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

@A Former Brilliant Member ty. i m glad to hear that you are ok.

num IC - 7 months ago

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

Atin Gupta - 3 months, 2 weeks ago

Log in to reply

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

num IC - 3 months, 2 weeks ago

Log in to reply

@Num Ic 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

Atin Gupta - 3 months, 2 weeks ago
×

Problem Loading...

Note Loading...

Set Loading...