Daily Challenge #205 - Consonant String Value dev.to staff on March 14, 2020 Given a lowercase string that has alphabetic characters only and no spaces, implement a function that will return the highest value of consonant su... Read full post Submit Preview Dismiss Collapse Expand Avalander Avalander Avalander Follow "Biography is irrelevant" - Seven of nine (probably) if she was asked that question. Joined Mar 12, 2018 • Mar 15 '20 Dropdown menu Copy link Hide Scala lazy val values = "abcdefghijklmnopqrstuvwxyz".toList.zip(Stream.from(1)).toMap def solve (str: String): Int = { ("[aeiou]".r.split(str).toList map evaluate).max } def evaluate (str: String): Int = { (str map (values.getOrElse(_, 0))).sum } solve("zodiacs") // 26 solve("chruschtschov") // 80 solve("khrushchev") // 38 solve("strength") // 57 solve("catchphrase") // 73 solve("twelfthstreet") // 103 solve("mischtschenkoana") // 80 Collapse Expand @nobody @nobody @nobody Follow Email i@jffrydsr.codes Location @nowhere Joined Jul 25, 2019 • Mar 16 '20 Dropdown menu Copy link Hide I've always appreciated Scala solutions for their mixed-notation to function-call chaining, it's truly bizarre. 😲 ...(str map ([expr]) // prefix-notation chaining... ...[expr]).sum // object-notation chaining... Collapse Expand Vidit Sarkar Vidit Sarkar Vidit Sarkar Follow Studying Computer Science & Engineering. Prefer to write clean and easy-to-understand code. Languages I use Python, C++, C, Java Script and Java. Location India Education Bachelor of Computer Science & Engineering Joined Feb 10, 2020 • Mar 14 '20 Dropdown menu Copy link Hide Here is a Python one-liner import re solve = lambda s : max([sum([ord(letter)-ord('a')+1 for letter in consOnly]) for consOnly in re.split("a|e|i|o|u",s)]) print(solve("zodiacs")) # output : 26 print(solve("chruschtschov")) # output : 80 print(solve("rhythm")) # output : 92 Collapse Expand Sabin Pandelovitch Sabin Pandelovitch Sabin Pandelovitch Follow Started low with PHP. Climbed the ladder and stumbled upon jQuery. Reached the clouds on NodeJs and ended in heaven with ReactJs. Location Romania Work Full stack JavaScript developer Joined Oct 19, 2019 • Mar 14 '20 • Edited on Mar 14 • Edited Dropdown menu Copy link Hide Javascript solution const values = [...Array(26).keys()] .map(i => String.fromCharCode(i + 97)) .reduce((o, l, i) => ({ ...o, [l]: i + 1 }), {}); const groupValue = group => group.split("").reduce((acc, val) => acc + values[val], 0); const solve = word => word .split(/[aeiou]/) .filter(e => e.length) .reduce((acc, val) => (groupValue(val) > acc ? groupValue(val) : acc), 0); const tests = [ "zodiacs", //26 "chruschtschov", //80 "strength", //57 "catchphrase", //73 "twelfthstreet", //103 "mischtschenkoana" //80 ]; console.log(tests.map(solve)); Collapse Expand Matt Ellen-Tsivintzeli Matt Ellen-Tsivintzeli Matt Ellen-Tsivintzeli Follow Ultra-fullstack software developer. Python, JavaScript, C#, C. Location Earth Education I am a master of science Pronouns He/him/his/his Work Software Engineer Joined May 2, 2017 • Mar 17 '20 • Edited on Mar 17 • Edited Dropdown menu Copy link Hide Nice! Here's my implementation: const solve = word => word.split(/[uoiea]/) .map(s => s.split('') .reduce((acc, c) => acc + (c.charCodeAt(0) - 96), 0)) .sort((a, b) => b-a)[0]; Collapse Expand Sabin Pandelovitch Sabin Pandelovitch Sabin Pandelovitch Follow Started low with PHP. Climbed the ladder and stumbled upon jQuery. Reached the clouds on NodeJs and ended in heaven with ReactJs. Location Romania Work Full stack JavaScript developer Joined Oct 19, 2019 • Mar 17 '20 Dropdown menu Copy link Hide Very nice, I wasn't aware of the c.charCodeAt(0) - 96 rule, didn't work with chars very much in all these years to be honest. Matt Ellen-Tsivintzeli Matt Ellen-Tsivintzeli Matt Ellen-Tsivintzeli Follow Ultra-fullstack software developer. Python, JavaScript, C#, C. Location Earth Education I am a master of science Pronouns He/him/his/his Work Software Engineer Joined May 2, 2017 • Mar 17 '20 Dropdown menu Copy link Hide It only works with lowercase a to z, because UTF-16 represents them as contiguous code points, starting at 97. Collapse Expand SavagePixie SavagePixie SavagePixie Follow Always learning new things. I love web development and coding in general. Joined Jul 23, 2019 • Mar 15 '20 • Edited on Mar 15 • Edited Dropdown menu Copy link Hide Elixir defmodule Challenges do import Enum, only: [ map: 2, max: 1, sum: 1 ] def solve(str) do String.split(str, ~r{[aeiou]}, trim: true) |> map(&_calculate_value/1) |> max end defp _calculate_value(str) do String.to_charlist(str) |> map(&(&1 - 96)) |> sum end end Collapse Expand Vidit Sarkar Vidit Sarkar Vidit Sarkar Follow Studying Computer Science & Engineering. Prefer to write clean and easy-to-understand code. Languages I use Python, C++, C, Java Script and Java. Location India Education Bachelor of Computer Science & Engineering Joined Feb 10, 2020 • Mar 14 '20 Dropdown menu Copy link Hide C++ solution using simple for-loop int solve(string s){ int tempMax = 0, maxSoFar = 0; for(int i=0;i<s.length();i++){ if(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u'){ maxSoFar = max(tempMax, maxSoFar); tempMax = 0; } else{ tempMax += s[i]-'a'+1; } } return max(maxSoFar, tempMax); } Collapse Expand Amin Amin Amin Follow Education École Supérieure de Génie Informatique Work Teacher, Freelance Developer & Chief Technical Officer @ Doc2wheels Joined Aug 17, 2018 • Mar 15 '20 • Edited on Mar 15 • Edited Dropdown menu Copy link Hide C #include <stdio.h> int isVowel(const char letter); int highestConsonantSubstring(const char* string); int main(void) { printf("%d\n", highestConsonantSubstring("strength")); printf("%d\n", highestConsonantSubstring("chruschtschov")); return 0; } int isVowel(const char letter) { switch (letter) { case 'a': case 'e': case 'i': case 'o': case 'u': return 1; default: return 0; } } int highestConsonantSubstring(const char* string) { const char* letter = string; unsigned int highest = 0; unsigned int current = 0; for (const char* letter = string; *letter; letter++) { if (!isVowel(*letter)) { current += *letter - 96; continue; } if (highest < current) { highest = current; } current = 0; } return highest; } Collapse Expand matej matej matej Follow Joined Jan 17, 2020 • Mar 15 '20 Dropdown menu Copy link Hide Java Streams public int solve(String input) { return Arrays.stream(input.split("[aeiou]")) .map(s -> s.chars().map(ch -> ch - 96).sum()) .reduce(0, (a, b) -> a > b ? a : b); } Collapse Expand Emmanuel Goh Emmanuel Goh Emmanuel Goh Follow Location Singapore Work Software Engineer at Visenze Joined Oct 20, 2019 • Mar 16 '20 Dropdown menu Copy link Hide Go, single-pass package value func ConsonantStringValue(s string) int { highest := 0 total := 0 for _, c := range s { if isVowel(c) { highest = max(highest, total) total = 0 continue } total += toValue(c) } highest = max(highest, total) return highest } func max(a, b int) int { if a > b { return a } return b } func isVowel(c rune) bool { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' } func toValue(c rune) int { return int(c) - 96 } Collapse Expand Idan Arye Idan Arye Idan Arye Follow Education B.Sc. Computer Science and Mathematics Joined Dec 2, 2017 • Mar 16 '20 Dropdown menu Copy link Hide Rust: pub fn solve(text: &str) -> u32 { text.split(|c| match c { 'a' | 'e' | 'i' | 'o' | 'u' => true, _ => false, }).map(|seq| { seq.chars().map(|c| (c as u32) - ('a' as u32) + 1u32).sum::<u32>() }).max().unwrap_or(0) } Code of Conduct • Report abuse
Scala
I've always appreciated Scala solutions for their mixed-notation to function-call chaining, it's truly bizarre. 😲
Here is a Python one-liner
Javascript solution
Nice! Here's my implementation:
Very nice, I wasn't aware of the
c.charCodeAt(0) - 96rule, didn't work with chars very much in all these years to be honest.It only works with lowercase a to z, because UTF-16 represents them as contiguous code points, starting at 97.
Elixir
C++ solution using simple for-loop
C
Java Streams
Go, single-pass
Rust: