How I learn from codewars

I like to do codewars challenges.

They are called katas on the site. Basically a prompt to write a small program that does something. Based on the difficulty of the kata you are given points after you pass all of the tests. After you pass the tests you are also able to see all of the ways that other people have solved the problem!

There is generally an expectation that your code will be as short and pythonic as possible. I think the ‘best’ solutions are often overly short, and not very readable, but then again I’m just a beginner.

Simply doing the challenge is a good coding practice. Try to make it simple and clean, and keep thinking about better ways to do the task. The real learning opportunities come after you have passed the tests and you get to look at other accepted answers! Here I’ll paste my code for one kata, and then paste one of the highest rated answers and comment it up so I can see what they did.

This is my code. It does what needs to be done, but it could be better.

This is the highest rated answer. They made some interesting choices when writing this. Three things stand out as interesting.

  1. They used the regex sub function to find “Caught Snitch” + any other number of characters and replace it with just “Caught Snitch” That is an interesting way of stopping the scoring process when the snitch is caught.
  2. Their use of list comprehension saves code, but requires processing the string twice. That is totally ok with this use case, but might be less efficient on a huge dataset. Either way I need to start using them more.
  3. F-strings. I’m jealous. I’d love to use them just to save on typing, but that is one convenience that I am not certain I am willing to throw away backwards compatibility over. (ie in my simple code that may be the best/only thing I use in py37!)

I’ve added a ton of comments to this image so I could figure out what was happening where. I’d love to know how to more easily keep the code live in wordpress, but everything that I have tried has either barely made it better, or totally messed it up.

After seeing that answer I might take the idea of a score_dict and the score_dict.get() piece. With that I could combine my second and third if loops into one loop. The first one would still be needed to test for snitch and break. I’m glad my answer wasn’t totally terrible, but it still has room for improvement.

Tell me what you think.
