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.
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.
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.