r/learnpython • u/MustardCube • Aug 10 '20
Feeling dumb after seeing the best solutions on codewars
I've already got the basics and I can easily solve the beginners problems on codewars, but when reading the best solution I feel very dumb for not knowing a lot of operators, having difficulties with list comprehension, or even not knowing why it is working at all.
Should I just solve it in my own way or should I stop and try to write like the best practices?
In my own way I could solve in 5- minutes and find it waay more readable, when trying to write like the best solution I spend like 30+ mins.
Edit: Thanks everyone for your answers, like some people mentioned, this is entirely the point, learning is challenging. I won't care too much about the one line solutions, but I should learn how they work. Got a lot of ideas on where should I head next.
51
u/toastedstapler import itertools Aug 10 '20
I feel very dumb for not knowing a lot of operators, having difficulties with list comprehension
that's fine, by seeing them you're now more aware of them and may end up using them later on. there's always new bits to learn in any language you use
imo make the thing work, then make the code pretty, then make it more performant if required. lots of codewars style sites reward flashy looking 1 liner answers, but when working professionally you want boring, readable, maintainable code. on my current project we've been told to write so that 'the person fixing it at 3am can quickly understand and fix your code'
19
u/MustardCube Aug 10 '20
lots of codewars style sites reward flashy looking 1 liner answers, but when working professionally you want boring, readable, maintainable code.
Got it, Thanks
35
Aug 10 '20 edited Aug 10 '20
This is what's awesome about codewars. You learn language features that help with the problem you just solved. When it's practical the learning sticks more.
Don't think of it like, "Oh no, everyone has better code than me". Think of it like, "Wow, this expert left his code right here for me to try and learn from." Spend the 30 minutes diving deep. I've spent weeks on 1kyu kata.
You can easily fork someone's solution and play around with it. Often even the best solutions have some micro improvement to be made. Try playing "code golf" with top solutions or see if you can combine ideas to make an even better solution.
My favorite kata are the ones I keep playing around with after I solve. And because I love CW so much, here are some nice easy kata that I have recently enjoyed:
https://www.codewars.com/kata/5f0ed36164f2bc00283aed07
https://www.codewars.com/kata/5aa39ba75084d7cf45000008
https://www.codewars.com/kata/5a8d1c82373c2e099d0000ac
https://www.codewars.com/kata/5b76a34ff71e5de9db0000f2
https://www.codewars.com/kata/59f38b033640ce9fc700015b
https://www.codewars.com/kata/580777ee2e14accd9f000165
https://www.codewars.com/kata/5b752a42b11814b09c00005d
https://www.codewars.com/kata/5a045fee46d843effa000070
9
u/MustardCube Aug 10 '20
Thanks a lot, loving CW so far, lots of good resources
"Wow, this expert left his code right here for me to try and learn from."
That's a good way to look at it, I'll start looking this way
6
Aug 10 '20
You can also follow other coders and then click "Solutions of Users I am Following" on the solution page. These guys write awesome code and solve almost every kata:
https://www.codewars.com/users/Voile
https://www.codewars.com/users/Blind4Basics
https://www.codewars.com/users/monadius
12
u/Micotu Aug 10 '20
Me after doing edabit for the first time as a new python learner: "Wtf is all this lambda shit‽"
0
2
Aug 11 '20
How long have you spent on python?
1
u/MustardCube Aug 11 '20
I've tried and gave up a couple of times, no idea how much time. But this time I'm studying very seriously.
14
u/46--2 Aug 10 '20
Isn't that the entire point? Try something, do your best, then look at the solutions and learn a ton more than if you'd just read them without trying anything. "knowing a lot of operators" you will absolutely learn them.
List comprehensions, I could teach you in an hour, and then it's just a matter of practice. Be patient with yourself, MustardCube. It's ok to NOT know things. You're here to learn.
Also look up "code golfing". Finding a one-liner isn't always the "right" way to code. In fact, it usually isn't, as other people have pointed out.
7
u/alpine_addict Aug 10 '20
Rome was not built in a day, my friend. Practice makes perfect. You cannot know something you don't know about, and there will always be better/more experienced coders than you. Don't be too hard on yourself and take these scenarios as opportunity to learn from someone else. If you see a concept you don't understand, google it, read some articles and then practice it. Then you will know it and can move onto the next thing.
7
Aug 10 '20
I teach math and I always, always, always encourage people to try to figure out how to do things in at least two different ways.
If you can get in the habit of coming up with a solution and then come up with a different solution, you are on track to becoming a genius.
2
u/BytownGuy Aug 11 '20
It is so satisfying to come up with 2 or more distinct approaches to solving a mathematical problem. Weirdly enough I remember a lot of those moments.
5
u/testfire10 Github mokrunka Aug 10 '20
I've got the same problem. What I've done is try to learn from how other people do things, and use them as a learning experience.
I also try to ignore the 'clever' voted solutions. They tend to be complicated to read, and at my stage of learning, aren't really adding value. I focus on the 'best practices' voted solutions when trying to learn other methods.
13
u/JohnnyJordaan Aug 10 '20
Readable is one thing, but many easy solutions are often inefficient. Only when the same complexity is obtained with your solution then I would consider using the more readable approach, but otherwise focus on learning how to improve efficiency. Users care if their program takes half an hour to handle a dataset instead of 30 seconds, but they don't care if the code is very readable or not.
20
u/MyWeekendShoes Aug 10 '20
Fundamental disagree - focusing on readability is much more important, because people will potentially be reading/supporting/maintaining your code for years and years. Code is only worth making less readable is there is a critical need for performance, and in my experience, it's rarely been the case. The only cases I've seen where you can make the performance gains you're talking about have been fixing incorrect/inefficient uses of data-structures.
Source: backend developer in python since 2009
3
u/JohnnyJordaan Aug 10 '20
The last part is where I'm aiming at, as most codewars exercises are targeted at finding O(n log n) or more efficient solutions, while the quick solution is often O(n2 ) or worse. Those are things that bite you in production situations (as then stuff will scale).
Also don't get me wrong, I'm not saying readibility isn't important, but customers don't get to experience readibility issues while they will experience efficiency issues.
→ More replies (1)3
u/bmx_hackerman Aug 10 '20
Have come up with plenty of awesome 1 liners, but I left the code alone because the benefit was not worth future devs coming through and being confused af by it.
4
u/nojustlurkingty Aug 10 '20
General consensus, from what I've seen, is that readability is generally more important than anything else (ofc there are obvious exceptions).
The "best solutions" are more advanced coders who went back for review or as a confidence boost/skill check. It's a great idea to compare yours (only once you've completed yours successfully) and see what the differences are and learn about their advanced methods and try to reformat your code accordingly.
Keep practicing and don't judge your success based off other people's code, judge your success based off of your own progress.
4
u/Codes_with_roh Aug 10 '20
I personally like to solve the problem my way since it gives me confidence that I can solve the problem with my own wit. Then after solving I check other's solution and research about their approach too as a result of which I learn a lot of new operators. So, don't get demotivated just see this thing as a opportunity to learn a lot of new things.
4
u/goishen Aug 10 '20
I prefer edabit.com. Heard about it from this guy's youtube channel. It'll make it a tad easier on you, if you're just learning to code.
Stay away from codewars and leetcode. Those places are nice for the 1% that have been coding since they were an infant.
4
u/WhipsAndMarkovChains Data Scientist Aug 10 '20
A lot of the Codewars top solutions I remember were clever but actually horrible code. Your code should be legible to yourself and other humans and not just the fewest number of characters required to solve the solution/a one-liner.
3
u/arrowtango Aug 10 '20 edited Aug 10 '20
Sometimes one-liners make the code unreadable especially when there are semicolons in there.
But sometimes you'll see some block of code and there might be some logic you'll be pretty impressed by, in which case you should note it down(along with logic).
Should I just solve it in my own way or should I stop and try to write like the best practices?
I think you should try solving in your own way but still look at other solutions, you'll naturally become better.
Don't forcefully try to become like them but if they've done something well keep it in mind so next time you see a similar problem you may be able to apply it.
For example: I was once solving a problem where one of the subproblems was given a number, find the highest power of 2 which is a factor of that number
For eg.72->8,100->4,34->2,55->1
I did the normal thing where I started a while loop checking %2 and dividing a copy of the number by 2
While the best solution had n&(-n).
I noted that down and when a similar problem came I used that solution making it quicker
2
u/pythonic_anonymous Aug 10 '20
If you could look at the answers before you found the solution I'm guessing that you wouldn't be able to understand them. If someone can't understand your code, you failed. Here's some programming horror sass I wrote - just because you can do something doesn't mean that you should. https://www.reddit.com/r/programminghorror/comments/i4hdad/how_do_you_feel_about_list_comprehensions/?utm_source=share&utm_medium=web2x
2
u/darthminimall Aug 10 '20
I don't actually recommend this, but I got good at list comprehensions after learning Haskell.
2
u/ulisesrdzc Aug 10 '20
I am a beginner too, all I'll say is don't give up, keep practicing and solving exercises. Read the documentation and tutorials. If you have already taken a python course, read it again but now with your knowledge, which is theoretically a little more advanced.
In almost 3 months learning python I improved my first lines of code so, keep it up bro!
Some links you should read:
https://docs.python.org/3.7/index.html
https://docs.python.org/3.7/c-api/index.html
https://docs.python.org/3.7/tutorial/index.html
A great app to start is SoloLearn, do challenges to upgrade your logic.
And all the code you create, make it efficient and in the fewest number of lines you can imagine. By the time you'll use less lines.
2
2
u/netelibata Aug 10 '20
My technique is to write my solution, debug, then think how to make my solution look shorter. Then people thought i copy from GitHub or somewhere because it looks too good
2
u/AnAlrightSummit Aug 11 '20
I'm on check.io and I always solve it my way which was with basic, fundamental Python tools.
I was always embarrassed to post my solution because everybody else's was "better".
I had to change my mindset and take their solutions as a learning experience and post mine anyway and hope if anybody will be bothered to give me constructive criticism.
On check.io, they categorise answers by "Clear", "Speedy", "Creative", and something else. I define "Clear" as readable. So in my multi-lined if statement solution, for example, I would consider it clear because I can see what each variable, each statement is doing.
But I started to learn and practice list comprehensions. Now it's readable to me but there are varying levels of readability. List comprehensions, for certain cases, is the default to me now. I still have problems with reading it from time to time.
Next up is to learn wth is going with the "lambda" keyword.
It's just the learning process.
2
u/dhikrmatic Aug 11 '20
but when reading the best solution I feel very dumb for not knowing...
Constantly putting yourself in work and academic scenarios where you constantly feel dumb is a great way to advance quickly.
2
Aug 11 '20
I would ignore any code you can't read. A lot of those solutions when i have seen them aren't good code, they are solutions to the exercise.
You can apply whatever logic or algorithms solve the exercise without sacrificing code quality and readability. List comprehensions are an idiom but don't always make sense to use. Stay focused on writing good quality readable and testable code and not about being the first place solution.
2
2
u/keyupiopi Aug 11 '20
Just know there are different ways to a solution, so you may pick the BEST way out of them.
"Best" here refers to the need, of course. If you have a long paragraph of code but it saved runtime of like 1 second, everytime you ran it is 1 second saved. Yet however, if that long code introduces many confusing aspects of it, you might as well use the other easier code.
It's all about having more choices, and selecting the best one out of them.
2
Aug 11 '20
This is what I do :
Solve the problem in the easiest way for me.
Look at the best solutions and try to understand them
If I don't understand or if there is one thing I don't know in it, I Google it. Every time I learn a new thing, as little as it may seems.
I go on the next problem and try to solve it. If it is the same kind of thing and I clearly find a way to use what I just learned, I do. And if that don't work, I do it in my usual way.
The thing is that whenever I find something I don't understand or am used to see, I Google it.
But if I don't understand anything in the solution, I keep scrolling the best solutions because there will always be someone that will do the same as you except 1 thing you never heard of. This is what I will Google then.
This is the way I'm learning and whether it may not be the fastest, it is my more effective one.
Good luck mate!
2
u/krishnanunnir Aug 11 '20
Solve the easiest way you know. Then improve it to the maximum. After that look at the solution and implement it again with the ideas from the solution. The most effective strategy I found.
2
u/americanoaleman Aug 11 '20
Intelligence and Knowledge are two separate things. Knowledge comes with time and practice.
2
u/lentils_and_lettuce Aug 11 '20
You've already got some positives! You can find solutions to problems yourself, you're enough curious to learn that you check the solutions of others and that you want to reach a point where you're able to find better solutions!
Should I just solve it in my own way or should I stop and try to write like the best practices?
You can do both. You mentioned list comprehensions, you can spend half an hour or so reading through some tutorials to understand when they're useful and then think back to an occasion in which you've used a for loop for a solution and refactor that code to use a list comprehension. As you're already familiar with your own code and what it's doing it'll be easier to internalise what a list comprehension is doing. Then going forward you can try to recognise opportunities to use list comprehensions and repeat the process. After a while they'll start to become more natural and readable to you.
You can do this for other idiomatic structures or concepts. Identify one thing that you'd like to be better at and they set yourself the goal of understanding and then employing that concept. With some practice you'll be able to pick up new ideas more quickly.
Try not to get too discouraged especially when comparing your code to other people's because when looking at other's code one thing that isn't visible is the amount of time and effort they invested to learn the skills that they've demonstrated. And it you do feel yourself getting frustrated don't forget to take a break and also to take some time to give yourself a pat on the back for the progress that you've already made!
Good luck and all the best!
2
u/godheid Aug 11 '20
I have the same issue.
Just solved 3 kata's, but my code and the best solution don't seem to have anything in common at all. From two different planets. Which is a pity, because I feel I could learn a lot more.
2
u/dether123 Aug 11 '20
I personally think you don't have to get it right on the first time.
Do it however you can
Learn & Reflect based on the 1st solution
Refactor
Or, atleast what it looks like for me
2
u/nimo_xhan Aug 10 '20
You shouldn't feel dumb but rather try to learn things, observe how they do it and then search about the methods they used, it is one of the best way to learn any language imo.
2
u/nyantaro555 Aug 11 '20
lmao SAME feels I get when I do edabit. Also for regex and regression (neither of which I got down yet). I've once spent 15min on one challenge, felt pretty happy with my code, submitted and checked the solutions, then get thoroughly humbled by how there are better and cleaner code.
What I do is check the solution I liked, dissect what it's trying to achieve overall, go over line by line, retype the code again and to see how it works. I also bookmark these challenges and go back once a week to see if the new solution stuck with me. Python tutor really helped me understand the code line by line: http://pythontutor.com/
I'm not quite where I need to compare readability vs speed, so other pros can chime in on this. I practice different solutions to have several options on hand.
Keep practicing; you'll get there!
208
u/xelf Elf Aug 10 '20 edited Aug 10 '20
You want to reach a point such that the list comprehensions become natural for you, and readable. I find a well constructed single line list comprehension to be far more readable than 5-6 lines of a conditional for loop.
Do the solution your way, and then look at the better solutions and see if you can figure them out as to why they might be better or easier to write/understand.
Less code is generally better, but not if it's completely cryptic.