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
matchwill return anullwhich 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
matchso 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'].includesFixed, 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!
Here is my try at it in JavaScript:
Why not so simple as this (JS)...
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
Python regex example
JavaScript, using reduce:
JS one that's probably pretty speedy
Note that:
and vowels go pretty much eaiou in terms of how common they are in english.
TI-Basic Calculator, where the string is in
Ans:And yes you can omit ending parens, which helps saves some bytes (:
This is my solution in Swift (with an extension 😊) :
Haxe
iterators library looked useful but you can't
foldan iterator, only an iterable, disappoint.Could also
Dart
//TS
my solution with TDD:
test:
and func
In F#
Python
My solution is a bit longer because I don't know regex, but it gets the job done.
CodePen
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.
Go:
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
Ruby:
GO
Link to playground - play.rust-lang.org/?version=stable...
A solution in Elixir
Will have to double check the syntax (on Mobile) but I think this will work
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.
Ruby Language
with specs
output
ledgible I hope
Here's my solution:
PHP:
SQL (Postgres)
In go!
Go playground example
Uses
fallthroughmostly because I wanted to,count++would be smaller, but all cases behave the same (also its more easily maintainable).Here is my simple solution with PHP:
Clojure:
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?
BASH
Clojure
ReasonML
Sketch: sketch.sh/s/jjxviGaqjQ2P0ZI0SFC2Sg/
Shell/awk oneliner:
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
Python
def vowel(str): count = 0 vowels = set("aeiouAEIOU") for alphabet in str: if alphabet in vowels: count = count + 1 print("No. of vowels :", count) str = "The Practical Dev" vowel(str)R with some Regex fun:
C++
Python
Given the list :
We can define a function:
Or simply in a one-liner:
Rubeh
Groovy has a nice built-in Pattern matcher
Using Regex in C#
Here comes Another 🚀
Python
Haskell:
Haskell
Ruby
Java
Why there's no
y? 🤔;)
var vowelCount = Regex.Matches(str, @"[AEIOUaeiou]").Count;
Console.WriteLine(vowelCount);
Ruby :
// Java
public static int countVowels(String input) {
return input.length()-input.replaceAll("(?i)[aeiou]", "").length();
}
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.
Python :
def count_vowels(word):
word=word.lower()
data={x:word.count(x) for x in "aeiou"}
return data