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!
Top comments (47)
Got it to one line of js :)
It's a bit unreadable but... you know 🤣
Without use slice
oh, haha - nice!
In my quest for a single line function, I missed the part about skipping the character if it's whitespace... so here's that tacked on too (though I added the filter to the end in order to keep the whole thing to one line) :)
Always amazes me when people get it into one line. 👍
map
,filter
, andreduce
are your friend! Whenever I'm dealing with translating one string, array, or object into another one, there's probably a way to do it by just chaining those three methods together.Nice, I'm a python man myself and will be sure to check it out! 👍
(I believe map,filter and reduce are available in python as well....)
Minimising your javascript will get everything onto one line. I'm not sure about why you'd want to do that when showing your code to anyone though.
It's only "one line" because you have no whitespace restriction in javascript.
It is also a single method chain. The result of each call flows immediately into the next subsequent call, and the variable space doesn't become overcomplicated, accidentally overwritten, or unnecessarily populated, as a result.
I'm curious which languages hit upon a "whitespace restriction" that would force the answer onto multiple lines? AFAIK it would also be possible to create such a method chain in any of the languages people have implemented their answers in so far. Well, maybe not C++, I have no idea whether arrays there have
map
,filter
, andreduce
🤔Personally, I wish more people would notice my tight Elixir guard clauses and Erlang VM optimization tricks, but hey, you win some, you lose some ¯\_(ツ)_/¯
Python?
Mostly I just mean that this is a "daily challenge" to produce a solution to a problem, not a code golf challenge, but answers like this are basically unreadable.
Python's got
map
,filter
, andreduce
😶 It's even got comprehensions... If anything, it's easier to do this as a one-liner there than in JS.And you're still missing the fundamental point... For starters, nobody is looking at strings in modern languages and saying, "Man, you know what's a hard problem that needs solving? Capitalizing exactly one letter at a time within
"I am the very model of a modern, major general."
If we only could find an engineer who knew how, all of this company's problems would vanish overnight."And beyond that, the one-line solution can still be broken out into multiple "lines" while retaining the fact that it is one chain of methods, acting upon a stream of data, in a way that allows us to obviate the need for loops, potentially-overwritten variables, and layers of cyclomatic complexity. It's literally just a matter of adding newlines, at any of the method boundaries, in any language you're working in. Chris can rewrite this, trivially, as
All he would need to do — all anybody who felt like this (definitely code-golf challenge) were unreadable would need to do — would be to place their cursor at a spot where they felt would make sense to break the method chain out onto another line, find the appropriate button on their keyboard for creating a line break, and press it. As far as scrolling left and right goes, I've dealt with much, much bigger pains in code than the readability of this, and it's usually been around an actual bug that's manifested from some contrived double
for-loop
logic, or, you know, the complexities involved in modeling real-world systems within code.Yes, and that's my point. It can be written in an easy-to-read-and-understand way, if you wanted to write it in a way that people could learn from or maintain.
If it was an interview question, would you obfuscate your answer? It's not, but I bet you wouldn't.
This isn't a golf challenge. It's to see what different people's approaches to the problem might be.
I'm sure we all have, but just because you can come up with some examples of something that's worse doesn't make a good argument for anything. I mean, I don't like squid, but I'm not going to pick it off the menu because my date points out that anchovies are worse when there're whole other pages of options.
I kind of joked about it being unreadable - but in real-life code, I would have done something very similar. Yes, I would have put the map and filter on different lines, and I probably would have filtered the whitespace first, instead of at the end like I did... but the actual solution would have been pretty close to the same.
I think there are a huge variety of coding styles, and I happen to like using map/filter/reduce combos when I can. I don't think they're unreadable - * as long as everyone on the team is ok with them *. So I think that's a good conversation to have with your team about code style.
Okay. Well... maybe you could lighten up, as I'm now just asking you, right out, to do that? However you regard these daily challenges, I promise you that some people are actually doing them to just have a little fun, and some of them are going to be trying to solve them — as some do with golf — in as few strokes as possible.
The nitpicking of other people's code is giving me huge Dwight Schrute vibes, and it's turning daily puzzles into a joyless trial, and there doesn't seem to be any reason for you to need to. Unless we're secretly all in The Last Starfighter r/n.
EDIT: replying to the wrong comment, sorry 😅
😅
Yep, very true
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
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++
That's crazy.
from where did you learn that u should use 32 , i didn't know that before
Thank you
Python
JavaScript
Playground
Play with it on Repl.it.
expect(received).toEqual(expected) // deep equality
Rust, with iterators:
My python sol :
In one line with a generator and a lambda function :
Love it
Funny thing. The other day I was creating a wave animation using text in CSS. It used font-weights instead of case change, but updating it to adapt to this challenge should be fairly easy. Although it wouldn't return an array, but generate an animation instead.
Rust "one-liner" Playground