How to resolve the algorithm Letter frequency step by step in the Erlang programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Letter frequency step by step in the Erlang 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 Erlang programming language
Source code in the erlang programming language
%% Implemented by Arjun Sunel
-module(letter_frequency).
-export([main/0, letter_freq/1]).
main() ->
case file:read_file("file.txt") of
{ok, FileData} ->
letter_freq(binary_to_list(FileData));
_FileNotExist ->
io:format("File do not exist~n")
end.
letter_freq(Data) ->
lists:foreach(fun(Char) ->
LetterCount = lists:foldl(fun(Element, Count) ->
case Element =:= Char of
true ->
Count+1;
false ->
Count
end
end, 0, Data),
case LetterCount >0 of
true ->
io:format("~p : ~p~n", [[Char], LetterCount]);
false ->
io:format("")
end
end, lists:seq(0, 222)).
letter_freq( Data ) ->
Dict = lists:foldl( fun (Char, Dict) -> dict:update_counter( Char, 1, Dict ) end, dict:new(), Data ),
[io:fwrite( "~p : ~p~n", [[X], dict:fetch(X, Dict)]) || X <- dict:fetch_keys(Dict)].
You may also check:How to resolve the algorithm Rep-string step by step in the BCPL programming language
You may also check:How to resolve the algorithm Sum digits of an integer step by step in the Ezhil programming language
You may also check:How to resolve the algorithm Compare sorting algorithms' performance step by step in the REXX programming language
You may also check:How to resolve the algorithm Write entire file step by step in the Maple programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the 360 Assembly programming language