How I go about solving a code challenge.

I’m going to pick a random kata from code wars and document my thought process as I go through it.

The Kata chosen is:

Image for post
Image for post

So for this Kata I need to find the first non repeating character in a string.

My thought is, I can pop the first letter of the string, and check it against the remaining string. If it’s included in the string, move on to the next letter and test it.

The first thing I have is

def  first_non_repeating_letter(s) 
# Code go here
answer = ""
array = s.split("")
answer = array.shift
if array.include?(answer)
p answer
return first_non_repeating_letter(array.join(""))
else
return answer
end
end

I’m splitting the string into an array. I hold the first item in the array (a letter) in a variable called answer. If the array contains that answer letter, then we call the function again with the remaining letters.

If the array doesn’t include the answer, then that answer should be the first non repeating character.

I didn’t pass all the tests. In the example of “moonmen”, when I get to the second “o”, it thinks thats the first non repeating character.

What I’m thinking now is when i check to see if the array contains the answer, if it does, remove all instances of that letter, and then call the method again.

Image for post
Image for post

No errors on the test except for dealing with an empty string. Easy enough. Just return “” if s = “”.

Image for post
Image for post

After submitting the code, I ran into some more errors.

Image for post
Image for post

I didn’t compensate for letter cases.

Image for post
Image for post

Now the if statement compensates for letter cases. But, it doesn’t remove the characters based on letter case. So I’ll need to filter both cases of the letter to make sure.

Image for post
Image for post

Now all tests are passing!

Image for post
Image for post

Fullstack Software Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store