How do I optimize this code in Python?(Missing Numbers code coach) | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 2

How do I optimize this code in Python?(Missing Numbers code coach)

So, I've completed the Missing Numbers code coach in Python, and it works as expected. However, I think it needs to be optimized. Because, I want to avoid making bad code over and over again when coding, which makes my code become less readable. And I don't want my code to become unreadable. It'd be better if I make a good,more readable and optimized code. Here's my code along with the Code Coach challenge: https://code.sololearn.com/c0gyqvTNf7yD/?ref=app (The challenge is displayed in the code) I would love to see your tips and advice in the comments!

15th May 2023, 3:32 PM
Dragon RB
Dragon RB - avatar
9 ответов
+ 6
Dragon RB , here are some hints about optimizing: for the first part of data input, a for loop should be used. this reduces the code by 2 lines: howmany = int(input()) #repeat = 0 #while repeat < howmany: for _ in range(howmany): getin = int(input()) getlist.append(getin) #repeat+=1 for the second part (checking for missing numbers), we have a duplication of code parts: range(min(getlist),max(getlist)) also using the sum of list elements and other parts can be optimized and made more obvious. approach: > using a range from min element in input list upto the max element in input list. this range contains all numbers we should have. > iterate over this range, and check each element if it is in the input list. if not: append this number to the "missing" list. >after the loop is done, our "missing" list contains all missing numbers and can be output.
15th May 2023, 5:16 PM
Lothar
Lothar - avatar
+ 4
Dragon RB You just need to remove some lines from your code. n = int(input()) lis = [] for i in range(n): lis.append(int(input())) #in one line li1 = [j for j in range(min(lis), max(lis)) if j not in lis] print (li1)
15th May 2023, 6:32 PM
A͢J
A͢J - avatar
+ 3
Good variable names are essential for readability. For starters, pick and use (consistently) either camel case or snake case to help increase readability of the names themselves (eg getlist to getList or get_list). Also use more descriptive names, where if you saw them completely out of context you'd be able to guess what they were for. eg "getlist" to something like "givenList" or even "incompleteList", and definitely avoid super generic names like x, n, or list2 and list3. Lastly, make better use of white space. 4 spaces is the preferred standard for indentations in python, for example, and even on the app sololearn's built in tab button is exactly 4 spaces, so use it. In plain English, we add spaces after commas and extra line breaks between paragraphs, so be sure to do that in your code too (eg extra lines between for blocks). Even in python, where some of the spaces have semantic meaning, you have a lot of flexibility with this.
15th May 2023, 7:29 PM
Orin Cook
Orin Cook - avatar
+ 3
Read pep 8 Style Guide for Python Code, it's the best way to get that
15th May 2023, 7:34 PM
Smith Welder
Smith Welder - avatar
+ 2
nm=[int(input()) for u in range(int(input()))] print(*([u for u,g in enumerate(nm,start=nm[0])if u not in nm])) Just for example, it's not complete👌💯
15th May 2023, 7:18 PM
Smith Welder
Smith Welder - avatar
+ 2
Yep, there's nothing wrong with Python really, and even "thousands of times slower" is plenty fast on a modern computer. Just saying, don't worry too much about optimization; not much point worrying about air drag on a tortoise lol. Plus, even once you do move to a compiled language, the compiler will handle most of the optimizations anyway.
16th May 2023, 3:00 AM
Orin Cook
Orin Cook - avatar
+ 1
Lothar Thank you, that helps a lot!
15th May 2023, 5:43 PM
Dragon RB
Dragon RB - avatar
+ 1
Supplemental, an aside: don't worry too much about performance optimization in Python, as the language itself is literally thousands of times slower than others. Obviously avoid the bigger inefficiencies in your logic, but write for readability first. If you want fast code, write in a compiled language (pretty much everything except JavaScript, Python, PHP, or Ruby). If you want even faster code, eg for enterprise-scale operations or high-end gaming, learn one of the few languages with no garbage collection: C, C++, or Rust. But if you're writing in Python, nothing you can do on your end will make your code fast; just avoid making it too much slower.
15th May 2023, 7:39 PM
Orin Cook
Orin Cook - avatar
+ 1
Orin Cook I think it's better to learn Python before learning anything else, as it gives me a better understanding when learning a new language. I've learned a few C++ before learning Python. I gotta say that it's quite hard to understand what it says, and I'm not able to solve medium problem or easy problem with C++ even I have learned till working with files lesson. Now, maybe learning C++ or C#/Java at this time right now would be a good choice. Anyways, these writing code tips are pretty helpful. Although I didn't mention that.. But this may help me a lot when coding in the future.
16th May 2023, 2:55 AM
Dragon RB
Dragon RB - avatar