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:

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(""))
return answer

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.

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

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

I didn’t compensate for letter cases.

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.

Now all tests are passing!

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