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.
Here's mine... would have liked to use String.prototype.matchAll, but my JS engine didn't support it. So used a generator and Array.from instead. :-)
function*matchAll(str,re){letmatch;do{match=re.exec(str);// get the first match of the regular expessionif(match)yieldmatch;// if we have one, yield it}while(match!=null);// keep going until no more matches}constgetLongestSequence=(str,re)=>Array.from(matchAll(str,re))// generators are iterable.map(([seq])=>seq)// extract the string from the regex match result.reduce((longest,cur)=>cur.length>longest.length?cur:longest// keep track of longest,"");constgetLongestVowelSequence=str=>getLongestSequence(str,/[aeiou]+/gi);// use vowels -- g = global; i = case insensitive
I could have done it with a single fold, but I decided to group the characters by groups of vowels and non-vowels, filter out the groups that are not vowels, map the length of each group and pick the largest number.
Also, almost entirely point-free, a pity I couldn't figure out how to make bothVowels point-free.
constword="codewarriors"functionfindLongestVowels(word){constregex=/[aeiou]/;constvowels=Array.prototype.reduce.call(word,(currentLetters,nextLetter,index)=>{if(regex.test(nextLetter)){returnregex.test(word[index-1])?currentLetters.concat('',nextLetter):currentLetters.concat('',nextLetter);}else{returncurrentLetters;}},"");constlongestVowels=vowels.split('').reduce((longest,current)=>{returncurrent.length>longest.length?current:longest;},"");returnlongestVowels.length;}console.log("Length of the longest vowel: "+findLongestVowels(word));
moduleVowelTestexposing(suite)importExpectexposing(equal)importTestexposing(Test,describe,test)importVowelexposing(getLongestVowelSize)suite:Testsuite=describe"Vowel"[test"Should return 0 for a string with no vowels"<|\_->equal0<|getLongestVowelSize"bcdfgh",test"Should return 1 for a string with a one-vowel substring"<|\_->equal1<|getLongestVowelSize"abcdefghij",test"Should return 2 for a string with a two-vowel substring"<|\_->equal2<|getLongestVowelSize"codewarriors"]
Javascript version
The new JS function
matchAllis going to be really useful.Here's a JS quickie
Gotta love JavaScript one-liners
Here's mine... would have liked to use
String.prototype.matchAll, but my JS engine didn't support it. So used a generator andArray.frominstead. :-)Full Gist: gist.github.com/kerrishotts/a3ec30...
Just noticed that the challenge is to return the length ;-) Oh well --
getLongestVowelSequence("codewarriors").lengthwill do ;-)Haskell
I could have done it with a single fold, but I decided to group the characters by groups of vowels and non-vowels, filter out the groups that are not vowels, map the length of each group and pick the largest number.
Also, almost entirely point-free, a pity I couldn't figure out how to make
bothVowelspoint-free.In golang! Could be simpler but lots of loops!
Go Playground example original
EDIT:
Add new example with changes from comments
Go Playground example with new switch
EDIT 2:
Realized it would not handle vowels at the end of the string
Go Playground
Hey @peter , you can also use multiple values in a single case statement
github.com/golang/go/wiki/Switch#m...
Hey good to know thanks! Just learning go so didn't realize I could do that. Thanks!
there's also
strings.Countbuilt-in method ;)golang.org/pkg/strings/#Count
Edit: It being python, I'm condensing my previous code down into a list comprehension.
Hi,
this is my first time sharing my code here, so please don't judge :)
But I really like these little challenges, thank you!
Here we go (python ftw!):
There is probably some room for improvement...
Have a nice day!
Hi @heidrichj you could also use the class Counter from collections package :)
Tip:
Perl solution, tests included.
Javascript with reduce
Elm
Tests
I like your approach!
This is the Swift version :)