Create a function which accepts one arbitrary string as an argument, and return a string of length 26. The objective is to set each of the 26 characters of the output string to either '1' or '0' based on the fact whether the Nth letter of the alphabet is present in the input (independent of its case).
So if an 'a' or an 'A' appears anywhere in the input string (any number of times), set the first character of the output string to '1', otherwise to '0'. if 'b' or 'B' appears in the string, set the second character to '1', and so on for the rest of the alphabet.
For instance:
"a **& cZ"
=> "10100000000000000000000001"
'Abc e $$ z'
=> "11101000000000000000000001"
Tests
change("!!a$%&RgTT")
change("")
change("abcdefghijklmnopqrstuvwxyz")
change("aaaaaaaaaaa")
Good luck!
This challenge comes from Roy Gardiner on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (11)
C++17
This is a one statement in JS that works:
Here is a C++ solution,
Rust
yay fun with bitsets
// "Test"
On infinitely long strings it would be good to:
0b11111111111111111111111111
but on such short strings these would both be slower
Edit:
I have stolen the ascii case sensitivity bit from @miketalbot and will now proceed to pleasure myself with this fish
Is this actually faster?
No idea.
Alternatively
or even just
🤷
Python 3.7
Try it online!
(Sorry. I didn’t manage for format the code in markdown properly on my iphone)I had roughly the same idea:
Here is a Ruby solution
Here is the Python solution:
Thanks for the challenge!
There's also
String.prototype.includes
Thanks Michael, I like that better.