How to resolve the algorithm Letter frequency step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Letter frequency step by step in the Wren programming language

Table of Contents

Problem Statement

Open a text file and count the occurrences of each letter. Some of these programs count all characters (including punctuation), but some only count letters A to Z.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Letter frequency step by step in the Wren programming language

Source code in the wren programming language

import "io" for File
import "/fmt" for Fmt

var text = File.read("mit10000.txt")
var freqs = List.filled(26, 0)
for (c in text.codePoints) {
    if (c >= 97 && c <= 122) {
        freqs[c-97] = freqs[c-97] + 1
    }
}
var totalFreq = freqs.reduce { |sum, f| sum + f }
System.print("Frequencies of letters in mit10000.txt:")
System.print("\n    freq     \%")
System.print("----------------")

for (i in 0..25) {
    Fmt.print("$c  $5d  $6.2f", i+97, freqs[i], freqs[i]/totalFreq * 100)
}
System.print("   -----  ------")
Fmt.print("   $5d  100.00", totalFreq)

Fmt.print("\nTotal characters in text file = $d minus 10000 \\n's = $d", text.count, totalFreq)

  

You may also check:How to resolve the algorithm Permutations step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Odd word problem step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Ramer-Douglas-Peucker line simplification step by step in the Java programming language
You may also check:How to resolve the algorithm Sierpinski arrowhead curve step by step in the C programming language
You may also check:How to resolve the algorithm Strip a set of characters from a string step by step in the Applesoft BASIC programming language