The wave (known as the Mexican wave in the English-speaking world outside North America) is an example of metachronal rhythm typically achieved in a packed stadium. Spectators will start a cheer in one corner and then roll it around the arena, with each section rising from its seat as it yells.
Today's challenge is to write 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 start lower-case. If the character in the string is whitespace then pass over it.
Ex.
wave("hello") => []string{"Hello", "hEllo", "heLlo", "helLo", "hellO"}
This challenge comes from user adrian.eyre on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge for a future post? Email yo+challenge@dev.to with your suggestions!
Latest comments (47)
Python
Rust, with iterators:
One long, ugly line of haskell. (I guess 2 including the import)
Edit: missed note about skipping whitespace. Will fix it soon
ruby <3
My python sol :
In one line with a generator and a lambda function :
Love it
JAVA
CSS
This is not exactly what is requested in the challenge, but close (at least for CSS). The letters need to be wrapped on their own span, and then add "wave" to the parent element. An animation is added that transform one letter at a time into uppercase (not exactly an array, sorry, and it heavily depends on length):
Here is a demo (with some other animations too):
CSS solution is amazing :D
A little JS impl using a regex and
matchAll
(avail in recent browsers):Output:
As an Elixir function, including documentation! And a language feature I'm writing about in
an upcoming postUPDATE: the post is up!! 😃Go:
Play with it here.
Rust "one-liner" Playground
In Rust, although there is almost certainly a more efficient way of doing this.
Playground link here.
You can use concatenation to avoid a character by character copy.
I don't know the first thing about Rust, so this is probably not optimal either.
Playgrund link here
Nice!
C#
var wave = str.Select((c, i)=> str.Substring(0, i) + str.Substring(i, 1).ToUpper() + str.Substring(i+1, str.Length-i-1)).Where(s => !s.All(c => c.ToString() == c.ToString().ToLower()));