Letter frequency: Difference between revisions
Content added Content deleted
m (→version 1: reformatted the REXX section header.) |
mNo edit summary |
||
Line 1,871: | Line 1,871: | ||
printf('"%c":%i\n',[find(A)-1,A(A>0)]') |
printf('"%c":%i\n',[find(A)-1,A(A>0)]') |
||
end</lang> |
end</lang> |
||
=={{header|Nanoquery}}== |
|||
<lang Nanoquery>// define a list to hold characters and amounts |
|||
$characters = list() |
|||
$amounts = list() |
|||
// define the alphabet as a string to check only letters and numbers |
|||
$alpha = "abcdefghijklmnopqrstuvwxyz0123456789" |
|||
// get the filename as an argument |
|||
$fname = $args[len($args) - 1] |
|||
// read the entire file into a string |
|||
$contents = new(Nanoquery.IO.File, $fname).readAll() |
|||
// loop through all the characters in the array |
|||
for ($i = 0) ($i < len($contents)) ($i = $i + 1) |
|||
// get the character to check |
|||
$toCheck = str($contents[$i]).toLowerCase() |
|||
// check if the current character is in the array |
|||
if (($alpha .contains. $toCheck) && ($characters .contains. $toCheck)) |
|||
// if it's there, increment its amount |
|||
$index = $characters[$toCheck] |
|||
$amounts[$index] = $amounts[$index] + 1 |
|||
else |
|||
if ($alpha .contains. $toCheck) |
|||
// if it's not, add it |
|||
append $characters $toCheck |
|||
append $amounts 0 |
|||
end |
|||
end if |
|||
end for |
|||
// output the amounts |
|||
println format("%-20s %s", "Character", "Amount") |
|||
println "="*30 |
|||
for ($i = 0) ($i < len($characters)) ($i = $i + 1) |
|||
println format("%-20s %d", $characters[$i], $amounts[$i]) |
|||
end for</lang> |
|||
{{out}} |
|||
<pre>$ java -jar ../nanoquery-2.3_1462.jar -b letterfreq.nq sherlock-holmes.txt |
|||
Character Amount |
|||
============================== |
|||
p 7239 |
|||
r 25708 |
|||
o 34866 |
|||
j 544 |
|||
e 54972 |
|||
c 11118 |
|||
t 40545 |
|||
g 8311 |
|||
u 13604 |
|||
n 29701 |
|||
b 6645 |
|||
s 27941 |
|||
h 29588 |
|||
a 36146 |
|||
d 19064 |
|||
v 4567 |
|||
f 9362 |
|||
l 17633 |
|||
k 3684 |
|||
m 12150 |
|||
y 9776 |
|||
i 31240 |
|||
w 11554 |
|||
2 45 |
|||
9 23 |
|||
0 104 |
|||
1 127 |
|||
6 30 |
|||
8 46 |
|||
z 152 |
|||
x 578 |
|||
q 437 |
|||
5 27 |
|||
4 29 |
|||
7 25 |
|||
3 25</pre> |
|||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |