Output speed difference after the for statement result immediately vs for statement result list append (SW Expert question 3750)

Asked 2 years ago, Updated 2 years ago, 158 views

Hi, how are you?

There was a problem I didn't know while solving SW Expert Academy question 3750 I couldn't find a solution even if I googled, so I asked this question.

The problem is that the unauthorized reproduction is not possible, so to give you a brief explanation, it is a problem that adds the sum of the digits until it becomes one digit.

The first code I thought of is as follows.

When I turned that code on my local piecham, there was no problem. But when I submit it to the site, I keep getting the problem timeout.

I was wandering and with the help of a friend who solved the problem, I fixed it as follows.

I appended the calculated value that comes out every time the for statement was turned to the list called answer, and when I wrote the for statement separately and printed it out, the execution time was greatly reduced.

What the hell is wrong with this?

No matter how much I think about it, the code below will take more time to append to the list and go around a separate for door I can't understand because the execution time is rather reduced.

python3 for list append print

2022-09-21 12:15

1 Answers

The difference is... The number of times you create a variable or overwrite the value of the variable is different. For example, if T is 100:


2022-09-21 12:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.