0

How to Optimize code

Hi, am light am searching for tips to optimize writing my code, I always seem to overcomplicate the code with unnecessary steps without realising it Like for example this code of fizz buzz challenge https://code.sololearn.com/capW9B5W5416/?ref=app It seems like so many people just did it in much more simpler way . How can i simplfy my code. https://code.sololearn.com/ceNv01Qyfgn6/?ref=app Like look at this version made by Dustin. So simple

15th Aug 2022, 7:59 AM
Light
Light - avatar
11 Respostas
+ 3
Light It comes from practice so just try to solve any problem in different way. You can do it in one line also but for that we need to practice on oneliner code.
15th Aug 2022, 8:59 AM
A͢J
A͢J - avatar
+ 2
Light If you want to use function then you can do like this https://code.sololearn.com/c3ruose4SOnU/?ref=app
15th Aug 2022, 8:55 AM
A͢J
A͢J - avatar
+ 2
Light Your code is readable don't worry but I already said about complexity of code so it comes from practice. As much as you do practice in different way you will learn to make it short and less complex code.
16th Aug 2022, 3:07 PM
A͢J
A͢J - avatar
+ 1
Light As you are already using loop so 'x in solo' is not making any sense. you can do only x % 3 == 0: //this will take less time than (x in solo) because 'solo' = [3, 6, 9] if n = 10 so on every iteration of for loop everytime x will be check in 'solo'. There will be a performance issue. For example: if x = 3 then (x in solo) may take 1 second if x = 6 then it may take 1 second again if x = 9 then it may take 1 second again so total is 3 second which (x % 3 == 0) may take only 1 second so your logic is taking 2 more seconds.
15th Aug 2022, 8:44 AM
A͢J
A͢J - avatar
+ 1
Thank you A͢J
15th Aug 2022, 9:02 AM
Light
Light - avatar
0
Thats why i would like to get some tips to find and see the more straightforward routes more easily.
15th Aug 2022, 8:46 AM
Light
Light - avatar
0
That is really clean Can you share your thought process while creating it ?
15th Aug 2022, 8:56 AM
Light
Light - avatar
0
Sometimes it is better to trade making optimal code for readability. In a commercial environment you have to to make code readable. It's boring but making variable names clear honestly that helps. I prefer making things readable the key thing to remember is if something goes wrong at night and you wrote it a year ago can you easily remember what you did?
16th Aug 2022, 1:12 PM
Gray Adamson
Gray Adamson - avatar
0
You think my code can pass the readability test ?
16th Aug 2022, 1:54 PM
Light
Light - avatar
0
I tell you how i approach to optimal solution of a code. First i understand the problem and then I searched on Google to find the easiest way to do this problem after solving problem i checked out other solution and compare which one is better if other code is better i observed and trying same thing in my code and think why I missed that part
17th Aug 2022, 5:04 AM
Noman
Noman - avatar
0
I mean if it works for you it works. But for me i struggle severly if i tried to copy any code. It removes a significant part for which is making errors first before trying to perfect whatever am doing Anyawys thank you for sharing 🌸
17th Aug 2022, 5:41 AM
Light
Light - avatar