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
8/15/2022 7:59:36 AMLight
11 AnswersNew Answer
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.
Light If you want to use function then you can do like this https://code.sololearn.com/c3ruose4SOnU/?ref=app
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.
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.
Thank you A͢J
Thats why i would like to get some tips to find and see the more straightforward routes more easily.
That is really clean Can you share your thought process while creating it ?
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?
You think my code can pass the readability test ?
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
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 🌸