Hope you’re ready for another challenge! Let’s get started with Day 3.
Today’s challenge is modified from user @jayeshcp on CodeWars.
Write a ...
For further actions, you may consider blocking this person and/or reporting abuse
JavaScript:
Demo on CodePen.
Nice solution, mine was similar in using regex match, but not as short. This one will error on a string without any vowels because
match
will return anull
which doesn't have a length.That's a good point. This could be avoided by checking if the result of the match is null and using an empty string instead. Something like this:
I also used template literals before the
match
so numeric values or null would be process too... and now the code is even uglier than before :PTypo
${s}
Good catch! I corrected it. Thank you for letting me know!
I think this is the best solution if you're ok with regex.
Ruby
Long Solution (my first solution)
Short Solution (my refactored solution)
It would be interesting to know if
str.count("aeiouAEIOU")
was faster, or if reordering the letters to try to get the most likely match first was better – e.g.str.count("eiaouEIAOU")
[...'aeiou'].includes
Fixed, thanks!
Hi, i loved this solution. Took me a bit to get my head around the use of reduce here and it's awesome. Thanks. I learned something nice today.
Cool., I didn't know we can use spread on a string. 👍🏻
Thinking the string as a Set with O(1) for look up operations: solution is O(n), where n = len(str)
JS lambda way
My python solution!
Oh I've been waiting for this one all morning! Decided I wanted to go all out on this on!
Come check it out while I work my way through this challenge! I'm live now and about to get started!
twitch.tv/coreyja
Here is my Rust solution all TDD'ed out!
I'm still live streaming as I type this out, but once I wrap up I'll post a link to the video here!
Ahhh! :panic:
Turns out OBS didn't want to behave for me today, and split my stream into 2 and dies before I finished, but we got almost all of it recorded! Learnings for next stream!
Links to the video that recorded for the longest here
Livestream - Dev.to Challenge Vowel Count in Rust
Corey Alexander ・ Jun 30 ・ 1 min read
Why not so simple as this (JS)...
Here is my try at it in JavaScript:
This is my solution in Swift (with an extension 😊) :
TI-Basic Calculator, where the string is in
Ans
:And yes you can omit ending parens, which helps saves some bytes (:
JS one that's probably pretty speedy
Note that:
and vowels go pretty much eaiou in terms of how common they are in english.
JavaScript, using reduce:
Python regex example
SQL (Postgres)
Ruby Language
with specs
output
My solution is a bit longer because I don't know regex, but it gets the job done.
CodePen
Go:
Here is my simple solution with PHP:
Could also
This is very easy in Perl, as the transliteration operator returns the number of matches:
One can also use the Saturn (or Goatse) "secret" operator with a regex match:
The global match returns all the matches in list context, the assignment to () enforces list context, and enforcing scalar context on it by a scalar assignment returns the number of elements.
Java:
Will have to double check the syntax (on Mobile) but I think this will work
In go!
Go playground example
Uses
fallthrough
mostly because I wanted to,count++
would be smaller, but all cases behave the same (also its more easily maintainable).Haxe
iterators library looked useful but you can't
fold
an iterator, only an iterable, disappoint.You also have to traverse the tree to get to the next letter. But I see what you mean. Only counting equality checks, hard-coding behaves like a linear search. My bad.
I think you might still be missing Brian's point that a hash set has O(1) lookup time, which is faster than the tree set's O(log n).
(On paper. Real-world implementations vary. e.g. Ruby's hashes just do linear search at this size.)
Nim
Link to playground - play.rust-lang.org/?version=stable...
A solution in Elixir
Ruby:
ledgible I hope
Here's my solution:
PHP:
Dart
In F#
//TS
my solution with TDD:
test:
and func
A little late to the party, but this is my JavaScript solution:
Powershell
I do try to keep it as an actual function and give pretty/usable output.
Python
GO
ReasonML
Sketch: sketch.sh/s/jjxviGaqjQ2P0ZI0SFC2Sg/
Python
Clojure
Shell/awk oneliner:
BASH
Not sure if I follow. O(n log n) is worst than O(n). Insertions in a binary tree are expensive to keep it balanced.
If you use a set or hashmap assuming zero collisions, the look up is O(1). Then the bottke neck is in the string iteration. Right?
Python
Given the list :
We can define a function:
Or simply in a one-liner:
R with some Regex fun:
function findVowelLetters(s){
var v = ["a","e","o","i","u"];
var vow = "";
var slowerCase = s.toLowerCase();
for(var f of v){
for(var i =0; i<s.length ; i++){
if(slowerCase[i] === f){
vow += f;
}
}
}
return vow;
}
PHP
C++
Clojure:
;)
Using Regex in C#
Ruby
Rubeh
Haskell:
Why there's no
y
? 🤔Java
Here comes Another 🚀
Python
Groovy has a nice built-in Pattern matcher
Haskell
Python :
def count_vowels(word):
word=word.lower()
data={x:word.count(x) for x in "aeiou"}
return data
We don't care about the vowels being ordered for this problem, so you'd be paying extra time (vs. a hash-based set, or just hard-coded equality checks) for a property you're not using.
// Java
public static int countVowels(String input) {
return input.length()-input.replaceAll("(?i)[aeiou]", "").length();
}
var vowelCount = Regex.Matches(str, @"[AEIOUaeiou]").Count;
Console.WriteLine(vowelCount);
Ruby :