Jump to content

Letter frequency: Difference between revisions

jq
(jq)
Line 1,007:
return freqs;
}</lang>
 
=={{header|jq}}==
The following program will report the frequency of all characters in the input file, including newlines, returns, etc, provided the file will fit in memory.<lang jq>
# Input: an array
# Output: an array of [item, count] for each run
def runs:
reduce .[] as $item
( [];
if . == [] then [ [ $item, 1] ]
else .[-1] as $last
| if $last[0] == $item
then (.[0:length-1] + [ [$item, $last[1] + 1] ] )
else . + [[$item, 1]]
end
end ) ;
 
explode | sort | runs | map( [ ( [.[0]] | implode) , .[1] ] ) | .[]</lang>
Example:<lang sh>jq -s -R -c -f Letter_frequency.jq somefile.txt</lang>
{{Out}}
<pre>["\n",12]
[" ",124]
["#",1]
["$",8]
["(",4]
[")",4]
["+",3]
[",",4]
["-",4]
[".",9]
["0",3]
["1",7]
[":",2]
[";",2]
["=",4]
...</pre>
 
=={{header|Julia}}==
2,502

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.