Jump to content

Letter frequency: Difference between revisions

m
mNo edit summary
Line 1,874:
=={{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 =in range(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 =in range(0) ($i <, len($characters)) ($i = $i +- 1)
println format("%-20s %d", $characters[$i], $amounts[$i])
end for</lang>
{{out}}
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.