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) :)
Game Developer, Metaverse Creater.
With over four years of experience working in the metaverse, Theo's had the opportunity to work on top experiences, spearheading technical and creative design.
map, filter, and reduce 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.
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):
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.
functioncharactersIndexes(string){returnstring.split("").reduce(function(indexes,character,index){if(!/\s/.test(character)){indexes.push(index);}returnindexes},[]);}functionupperCaseAt(index,string){returnstring.split("").map(function(character,position){if(position===index){returncharacter.toUpperCase();}returncharacter;}).join("");}functionwave(input){returncharactersIndexes(input).map((index)=>upperCaseAt(index,input));}console.log(wave("hello"));// [ 'Hello', 'hEllo', 'heLlo', 'helLo', 'hellO' ]console.log(wave("h e l l o"));// [ 'H e l l o', 'h E l l o', 'h e L l o', 'h e l L o', 'h e l l O' ]
19 yo student *and* a full-time developer.
Building with Rails currently, exploring new frontiers.
I enjoy functional programming, Linux, FOSS, refactoring and mentoring :)
funcmexicanWave(sstring)[]string{if!utf8.ValidString(s){panic("invalid string")}// capacity can be replaced with len(s) if iterating over// the string twice is a bigger problem than allocating too// much memoryss:=make([]string,0,utf8.RuneCountInString(s))fori,r:=ranges{u:=unicode.ToUpper(r)ss=append(ss,s[:i]+string(u)+s[i+utf8.RuneLen(r):])}returnss}
As an Elixir function, including documentation! And a language feature I'm writing about in an upcoming post UPDATE: the post is up!! ๐
defmoduleWdo@doc~S"""
Takes in a string, ensures it's downcased, converts it to a charlist,
and then processes the charlist by subtracting the proper amount from
the `next` raw byte to arrive at the same capitalized character in the
ASCII chart. This letter is then inserted into an iodata list in
between the raw bytes that have already been `waved` through and the
`rest` of the raw bytes. This iodata list is then converted back to a
string, which is added to the head of a list of strings that represent
the in-motion version of the wave. To finalize the in-motion wave, it
is reversed once the input charlist has been fully processed.
When `next` is not a lowercase letter, it is added to `waved` without
adding a new string to `waves`.
## Examples
iex>W.ave("HELLO")
["Hello", "hEllo", "heLlo", "helLo", "hellO"]
iex>W.ave("helloworld!")
["Helloworld!", "hElloworld!", "heLloworld!", "helLoworld!", "hellOworld!",
"helloWorld!", "hellowOrld!", "hellowoRld!", "helloworLd!", "helloworlD!"]
"""defave(string)dostring|>String.downcase|>String.to_charlist|>do_waveend@ascii_upcase_val32# ?a - ?Adefpdo_wave(waving,waves\\[],waved\\'')defpdo_wave([]=_done_waving,waves,_waved),do:Enum.reverse(waves)defpdo_wave([next|rest],waves,waved)whennextin?a..?zdouppercase=next-@ascii_upcase_valdo_wave(rest,[IO.iodata_to_binary([waved,uppercase,rest])|waves],[waved,next])enddefpdo_wave([next|rest],waves,waved),do:do_wave(rest,waves,[waved,next])end
Coding since 11yo, that makes it over 30 years now ~~~
Have a PhD in Comp Sci ~~~
Love to go on bike tours ~~~
I try to stay as generalist as I can in this crazy wide place coding is at now.
Game Developer, Metaverse Creater.
With over four years of experience working in the metaverse, Theo's had the opportunity to work on top experiences, spearheading technical and creative design.
Game Developer, Metaverse Creater.
With over four years of experience working in the metaverse, Theo's had the opportunity to work on top experiences, spearheading technical and creative design.
Nice, I have been having issues with repl loading up.
Not the website or even an repl page just the code within it,
getting that dreaded: Failed to connect, retrying message.
Then after about 5 minutes it loading, have you been having similar?
(sorry for being a pain)
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, andreduceare 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.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
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.
JavaScript
Playground
Play with it on Repl.it.
expect(received).toEqual(expected) // deep equality
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
Rust "one-liner" Playground
Go:
Play with it here.
As an Elixir function, including documentation! And a language feature I'm writing about in
an upcoming postUPDATE: the post is up!! ๐A little JS impl using a regex and
matchAll(avail in recent browsers):Output:
My python sol :
In one line with a generator and a lambda function :
Love it
JAVA
In ruby
Nice, I have been having issues with repl loading up.
Not the website or even an repl page just the code within it,
getting that dreaded:
Failed to connect, retryingmessage.Then after about 5 minutes it loading, have you been having similar?
(sorry for being a pain)
Sorry I haven't had that kind of issue. It's ok
Nice! The runtime complexity is pretty substantial, but it's a really good first pass on the problem ๐
ruby <3
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()));