Steps to Help You Tackle Code Wars

Alexandra Bruno
4 min readJan 26, 2021

--

Using websites like Code Wars or Hacker Rank is a great way to keep on top of your programming skills. Being a Data Science student, I have struggled with tackling problems coding problems on these websites, especially when I am under a time crunch. In order to improve my programming skills and those of my readers I thought it would be helpful to try and lay out a simple framework on how to tackle a code war problem.

Step 1: Dissect the problem

Before trying to formulate a solution, making sure you fully understand the scope of the coding problem and what your output is supposed to be. Depending on the problem, understanding what you need and where you need to go can be a challenge. I know I have a hard time fully understanding a lot of the problems on Code Wars so let’s go through one together.

Remember these words — read the question, understand the scope of the problem and the output you are supposed to get for ultimate success in life and happiness.

Question: There is an array with some numbers. All numbers are equal except for one. Try to find it.

Examples:

find_unique([1,1,1,2,1,1,]) == 2

find_uniq([ 0, 0, 0.55, 0, 0 ]) == 0.55

I really like this problem because the expected output is clearly defined and I think it is a great problem to get your toes wet with tackling a code war challenge. In this instance, the problem and the expected output are clearly defined. We need one unique integer from the array that is being passed through the function, and now that we know this we can get started on our pseudocode. Pseudocode is writing out what you want your code to do in plain English before writing it out in a formal programming language. I know it feels like a step that may not prove to be useful but if you are unsure what kind of code you will need to solve the problem I can almost guarantee that writing out some pseudocode prior to writing code will help.

Step 2: Write pseudocode…please

Now that we have our pseudocode paving the way for us let’s write out real code.

I like this solution because it is very straightforward and easy for a beginner programmer to follow. However, we all know that most coding problems can be solved in multiple ways, so let’s take a look at another solution.

Step 3: Look at other solutions or try to rewrite your code

Once you have solved the problem, look at how other people chose to solve this problem. Or, better yet, try to revamp your initial solution! Rewriting code or looking at other solutions can be a great way to help you get some new ideas for when you are trying to tackle your next programming question.

This solution is exactly what we did in our first solution but this time around we are using a list comprehension. Let’s try and reverse engineer this solution by explaining it in pseudocode. This code is saying to create a list that contains a number if the unique array has a count of elements that are equal to one, then return the first element of the list aka x aka the unique number in the array that is being passed through the function. A list comprehension may not be as easy on the eyes as a regular for loop but it is a more sophisticated solution for this question.

Let’s try another question implementing the same steps.

Step 1: Dissect the problem

Question: Your task will be to return the count of pairs that have consecutive numbers as follows:

pairs([1,2,5,8,-4,-3,7,6,5]) = 3

The pairs are selected as follows [(1,2),(5,8),(-4,-3),(7,6),5]

— the first pair is (1,2) and the numbers in the pair are consecutive; Count = 1

— the second pair is (5,8) and are not consecutive

— the third pair is (-4,-3), consecutive. Count = 2

— the fourth pair is (7,6), also consecutive. Count = 3.

— the last digit has no pair, so we ignore.

Looks like we need to return that number of pairs that are in consecutive order, sounds simple enough. Let’s get started on a pseudo code.

Step 2: Our beloved pseudocode

Now that we have a blueprint of what we want our code to do in natural language, let’s transform it into a formal programming language.

Step 3: “ONE MORE TIME” -Daft Punk

And now for a more sophisticated, but still easy to follow, solution.

Look at that, you’re solving code war challenges in three easy steps and learning a ton about becoming a better programmer. Try these and your programming and interview skills will go through the roof.

Thanks for reading!

--

--

No responses yet