After a week of declaring my job search, I was fortunate enough to be getting a couple of phone screenings and even a back-to-back double tech interview just last week. It was my first EVER tech interview(s) and I could have been better prepared. Plus, the nerves did not help whatsoever. It was two 1-hour long interviews with a half-hour to recollect myself. I will try to attempt to do similar code challenges to the ones I failed horribly in my tech interview in future posts (which I will eventually link here) to redeem myself from the turmoil of that day. I guess it's a right of passage!
One was converting USD to a denomination or something to that effect, and the second one was returning the frequency of sequential characters. The closest challenges that I could find to them are this (the original one was much more complex) and this.
Let's do the challenge!
Anyway, today I'm giving myself a little bit of a mental break by trying my hand at doing a fun, simpler code challenge taken from Codewars. There's nothing like a code challenge that I can do to build up my self-esteem again.
In this simple Kata your task is to create a function that turns a string into a Mexican Wave. You will be passed a string and you must return that string in an array where an uppercase letter is a person standing up.
- The input string will always be the lower case but may be empty.
- If the character in the string is whitespace then pass over it as if it was an empty seat
wave("hello") => ["Hello", "hEllo", "heLlo", "helLo", "hellO"]
I'm trying to be better at jotting down what my thought process is before trying to code.
# input = str # use input size to determine how many iterations needed # capitalize letter in first position, append that to new arr # output is an arr of strs with each character capitalized by position # algo: create a range from 0 to string.size # call 'map' on that range with 'index' as a parameter # use str element reference and concatenations to convert character at index position into uppercase # next if str[index] == '' # str[0...index] + str[index].upcase + str[index + 1..-1]
string = "hello" in the console:
Now let's write it out. Don't forget to test it to make sure it's doing what it's supposed to:
I ended up using
.each instead of
.map, since I'm not transforming anymore and used a
next if, so it skips that particular number.
And ta-dah! It returned
true. How did you do? What could have be done better?
def wave(str) result =  (0...str.size).each do |index| next if str[index] == ' ' result << str[0...index] + str[index].upcase + str[index + 1..-1] end result end p wave("hello") => ["Hello", "hEllo", "heLlo", "helLo", "hellO"]
Top comments (0)