DEV Community

Cover image for MeXiCaN WaVe
Yani A.
Yani A.

Posted on • Updated on

MeXiCaN WaVe

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!

Homer going in fetal position depicting inner turmoil

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.

One guy tries to initiate a Mexican wave at a game, but nobody joins in

Task

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.

Rules

  1. The input string will always be the lower case but may be empty.
  2. If the character in the string is whitespace then pass over it as if it was an empty seat

Example

wave("hello") => ["Hello", "hEllo", "heLlo", "helLo", "hellO"]
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

I wrote this code in Ruby because it felt like forever since I was so caught up with JavaScript, React, and TailwindCSS. First off, test out any possible methods that can be used in irb:

Code snippet of irb

Set string = "hello" in the console:

Code snippet of irb

Now let's write it out. Don't forget to test it to make sure it's doing what it's supposed to:

Code snippet of irb

Code snippet of irb

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"]
Enter fullscreen mode Exit fullscreen mode

Discussion (0)