by llmzero · 2y ago · 0 comments
solution =: >@:{.@:(\: #&>)@:(((+/@:(e.&'aeiou') <: 2:) # ])&.>)@:;:
Example
solution 'yes, today you are reading something that is not so easy to grasp'
the result is: today
Ruby : frase.split.select{|x| x.count("aeiou")<3}.sort_by(&:length).last => "today"
Edited: Added a comparison with Ruby. It seems Ruby here is easier to read and to compose.
The enumerable module of Ruby provides many methods that can be easily implemented in J. Just to show one of them
max_by =: 1 : '{~ (i. >./)@:(u&>)'
For example the list.max_by(&:length) is (# max_by)F =: >@{.@(\: #@>)@(#~ (2 >: +/@e.&'aeoiu')@>)
which I think maybe composes a little better
This doesn't include the splitting. I would imagine I would be feeding this function from a boxed list from a word list, like: F 'b' fread 'dict.txt'
Also `;:` is unreliable on hyphenated words, etc. Better to use `cut`.
maximumBy (on compare length) . filter ((< 3) . length . filter (`elem` "aeiou")) $ words phrase
That returns "grasp", though, because it doesn't sort the list.`sort(split(p)[count.(r"[aeiou]", split(p)) .< 3]; by=r -> length(r))[end]`
which also returns "grasp".
But this one appeals to me more, because it doesn’t split twice:
`sort(filter(w -> count(r"[aeiou]", w) < 3, split(p)); by=r -> length(r))[end]`
maxBy(on cmp (#)).(((<3).(#).(el"aeiou"|=))|=)$words phrase