Count letters: Difference between revisions

From Rosetta Code
Content added Content deleted
(Add Factor)
No edit summary
Line 2: Line 2:
;Task:
;Task:
Count the letters used in a sentence, by code point, generating a hash or associative array, or appropriate construct for the language.
Count the letters used in a sentence, by code point, generating a hash or associative array, or appropriate construct for the language.

Update: Apparently, this is relatively the same task as at the following page, so this one should likely be deleted.

https://rosettacode.org/wiki/Letter_frequency
<br>
<br>



Revision as of 19:41, 11 March 2020

Task
Count letters
You are encouraged to solve this task according to the task description, using any language you may know.
Task

Count the letters used in a sentence, by code point, generating a hash or associative array, or appropriate construct for the language.

Update: Apparently, this is relatively the same task as at the following page, so this one should likely be deleted.

https://rosettacode.org/wiki/Letter_frequency


Factor

Works with: Factor version 0.99 2020-01-23

<lang factor>USING: kernel math.statistics prettyprint sequences strings unicode ;

count-letters ( seq -- assoc )
   [ Letter? ] filter [ 1string ] { } map-as histogram ;

"fuzzy furry kittens" "ασδ ξκλ ασδ ξα" [ count-letters . ] bi@</lang>

Output:
H{
    { "e" 1 }
    { "f" 2 }
    { "i" 1 }
    { "k" 1 }
    { "n" 1 }
    { "r" 2 }
    { "s" 1 }
    { "t" 2 }
    { "u" 2 }
    { "y" 2 }
    { "z" 2 }
}
H{ { "α" 3 } { "σ" 2 } { "δ" 2 } { "κ" 1 } { "λ" 1 } { "ξ" 2 } }

langur

Works with: langur version 0.7.0

<lang langur>var .countLetters = f(.s) {

   for[=h{}] .s2 in split(replace(.s, RE/\P{L}/)) {
       _for[.s2; 0] += 1
   }

}

writeln .countLetters("fuzzy furry kittens") writeln .countLetters("ασδ ξκλ ασδ ξα") # random Greek letters</lang>

Works with: langur version 0.6.11

<lang langur>var .countLetters = f(.s) {

   foldfrom(
       f(var .h2, .s2) { .h2[.s2; 0] += 1; .h2 },
       h{},
       split(replace(.s, RE/\P{L}/)),
   )

}

writeln .countLetters("fuzzy furry kittens") writeln .countLetters("ασδ ξκλ ασδ ξα") # random Greek letters</lang>

Output:
h{"f": 2, "u": 2, "z": 2, "y": 2, "r": 2, "k": 1, "i": 1, "t": 2, "e": 1, "n": 1, "s": 1}
h{"α": 3, "σ": 2, "δ": 2, "ξ": 2, "κ": 1, "λ": 1}