How to become a better programmer using Code Wars

avatar
(Edited)

image.png

I am a big fan of Code Wars, I really wish I did it more. It is a fantastic resource for becoming a better programmer.

There are endless amount of challenges available in around 30 different popular programming languages. Each language has thousands of challenges you can do at various difficulties to challenge yourself on a regular basis.

In my previous post, I said one of the most powerful ways to learn a language is not doing tutorials but actually building something on your own. Coming up with a project and doing it. Nowhere you will run into so many roadblocks you will be forced to solve that are not explained in some tutorial.

Code Wars gives you this experience without having to come up with an idea. Even better, it gives you projects that are bite sized and can build finished in one sitting, even for an inexperienced developer. Your own projects always tend to be more complicated and time consuming than you expect and are really just a ton of small challenges strung together into one big challenge.

I'm going to walk you through getting signed up for Code Wars, and working through a single challenge.

When you go to sign up for Code Wars you will be prompted to choose from one of the available languages, then solve a problem to prove you are human.

The obvious answer to this challenge is to add a return statement so it can return the result.

When you answer the question correctly, you will see one of the coolest parts of Code Wars that you may not quite understand right off.

Here it takes your function, and runs it against a series of tests designed to find bugs in your code by giving different inputs that may expose issues you didn't account for.

Once you get past the initial challenge and setup a username and password you will be prompted to select any other languages you know and may want to challenge yourself with.

Click the appropriate languages and your current experience, than hit save.

The level you choose will affect the difficulty of the initial problems you receive.

You can always go into your account settings under Training Setup to adjust your settings here or add/remove languages.

From here you will be presented with the main overview page where you can start your first challenge (called Kata, similar to how you learn Karate).

This is the place you will spend most of your time on with Code Wars, you will be presented with an introduction to a problem, a coding window, and a set of predefined tests to test your solution.

Seeing as this is a 7kyu problem, it is relatively easy and quick to solve. Code Wars uses a ranking system to rank the difficulty of problems and your progress. You start out as rank 8kyu, and move on to 1kyu, you then move on from 1 dan to 4 dan.

Let's get back to the first challenge, and see if we can come up with a solution, test it, submit it, then compare it to others. This is the loop you will follow for each challenge.

The challenge is to add two numbers and return the result in binary.

As a good programmer, you will take every problem and break it down into smaller problems. This challenge involves two problems.

  • Add two inputs
  • Convert result into binary

Let's start with the first problem. In Python, to add two numbers you just use the + operator.

result = a + b

Let's test our solution, and see how it does.

Well, we didn't do good at all. First, we never actually returned the result. Second, it isn't in binary. Let's solve the first problem and test again.

Well now we are returning a result as you can see from the test results, but we are not converting to binary. If you look at the test results, you can see the output and the result of the test.

image.png

This first line says your function returned "2" but the test expected "10" the binary equivalent of 2.

Ok, right now I am very happy with our code, it solves everything cleanly except for the 2nd part of the challenge, outputting to binary.

When doing challenges, I like to use built in functions whenever possible so I don't have to include additional libraries. Although you are free to do so, reducing your dependencies is an important goal as a developer. This will drastically reduce problems with your code in the future.

To convert a number to binary you use the bin() function. Let's give it a try.

We want to wrap the output of the addition with the bin() function. This is really simple as you can see here.

Technically, we solved the challenge but there is one unexpected issue (which is very common in development).

Bin() does not just output in binary, it actually leads the output with the two characters "0b" to denote it is a binary number. This is consistent and predictable, so we need a way to fix this. Luckily Python has fantastic string support with the slicing functionality. This is an easy problem to fix.

One simple change, and we pass all tests.

But wait, what did I do?

In Python, you can slice a string by using slicing notation. This is a set of brackets after the string with a start/end. [2:] means we want to start at the 3rd character (remember most programming languages start indexes at 0) and then go to the end. Since we don't know how long the string is, I omitted the second parameter which results in going to the end of the string.

Let's submit our results and see how we did compared to other solutions. This is a very good solution as it is easy to read, solves all problems, and not prone to bugs.

image.png

After you click attempt, your function will be tested with many more tests, tests that are not presented to you initially. Many of them are random to try to break your function in every possible way.

As you can see our solution is solid and passes all these tests. It is very common in programming and machine learning you solve your test cases but when you test against real data you run into issues. If these additional tests were given to you ahead of time, you might build a solution that passes those conditions but fails in the real world.

At this point, we can submit our solution. This can take a few seconds, just let it sit, you can see a progress bar in the top of your window.

After your solution is submitted, you are presented with a results page with the most popular solutions to the challenge. This in my opinion is when you really start to learn. You went through the efforts to come up with your solution, now you can review the best solutions to the problem and learn how you could have done it better, pick up new utilities and functions you didn't even know about, and see some cleaner solutions to the problem.

Because this was a simple challenge, you will find most of the solutions are fairly similar. The topped ranked solution is usually the smallest and most clever, but this is not necessarily the best as clever tends to use obscure features that many won't be able to understand easily.

After reviewing other solutions, you can either do another Kata or take a break. I highly recommend doing at least one challenge a day, some may only take you a minute or so, others may take an hour or more. Some will come naturally, others will be challenging, not because it's a difficult challenge, but you are unfamiliar with the features needed to solve the challenge.

If the challenge you receive is too difficult, I recommend hitting skip.

But I don't recommend skipping it because the "challenge" is too difficult, only if the difficult is too high.

As a 8kyu starter, 7kyu is the correct difficulty for you as it is one rank above you. Even if this challenge is "hard for you", I do not recommend skipping it. I would only recommend skipping if you get a 6kyu problem and you find it hard. That's two ranks above you, and I would be comfortable skipping it. If it is within one rank of you, I recommend pushing through and challenging yourself. You cannot grow if you do not challenge yourself.

If you liked this breakdown, let me know in the comment section and I will do a series and build on it.


Securely chat with me on Keybase

Why you should vote me as witness



0
0
0.000
13 comments
avatar

Going to have to check this out. Saw your orevious post about learning to code and I really need to dust off what little JS skills I have so I can be of more use as a cider here on Hive, lol.

0
0
0.000
avatar

Code Wars sounds amazing!

I've been using Sololearn for my coding crash courses, but Code Wars definitely seems more immersive. I'll be sure to check it out. Thanks!

0
0
0.000
avatar

First I have to learn a language, then I can try this out. 🤣

I am such a copycat visual learner. My biggest pitfall with learning coding languages was in following the tutorials so closely, I could never bridge things from one lesson into the next very well.

When I write my own code, I'd be hunting through for some kind of syntax glossary all day to create a mad logic statement to do something simple in a complicated way, only to be told by someone that there is a library with a function I didn't know about that will do that much cleaner/easier. Makes me want to pull my hair out when I am stuck teaching myself how to build a vacuum cleaner when all I have is toaster oven instructions.

Practice, practice, practice is the only way to fix my bad memory problems and lack of experience.

0
0
0.000
avatar

My biggest pitfall with learning coding languages was in following the tutorials so closely

This is very common I think. Analysis Paralysis is a common phrase in productivity, it means to continuously focus on solving a problem but never actually solving it. I think the same idea applies to tutorials, many just keep doing tutorial after tutorial rather than actually building something.

0
0
0.000
avatar

Love the idea, and the multiple language selections. As my job entails scripting it's useful for me.

0
0
0.000
avatar

I used to do these challenges in codewars a lot but recently I've moved on to hackerrank and codeforces. The contests in codeforces are really intense and I like the pressure of timed events which you can't get in codewars.

Also another reason why I moved to codeforces was that in codewars you only need to complete the given function while in codeforces you need to write the whole program from taking inputs to returning the outputs which I think is better in the long run.

0
0
0.000
avatar

I can totally recommend this. The "learning curve" can be - depending on the language - quite steep but you will definitely learn from it

0
0
0.000
avatar
(Edited)

I signed up this morning, looks interesting. I'll try it out but i would love to hear more about this site. There seems to be quite a bit to it; earning extra honour, community, badges, upgrades to red ...

0
0
0.000
avatar

I've not tried Code Wars, but I did get into the Project Euler challenges for a while and there was another called the Python Challenge. These can be as much about puzzle solving as programming, but anything that expands you skills is good. I have been doing a Coursera course that used ML (Meta Language). The weekly homework on that was pretty tough. I do get that actually creating a proper project is the way to build real skills. I just need to come up with something. I would like to do something around Hive. I need to play with the available libraries and see what I can come up with.

0
0
0.000