Pure Programmer
Blue Matrix


Cluster Map

Project: Frequency Table (Characters)

Write a program to generate a table of frequencies. The program should accept a stream on stdin and total the number of each character seen. Once the stream has been read the program should print a tab-delimited table with columns for Codepoint in hexadecimal, Character, Count, and Frequency. The frequency of each character is the count for that character divided by the total number of characters in the file.

You can capture webpages using the 'curl' program as tests for your program. If the webpage is encoded in ISO-8859-1 (Latin-1) instead of Unicode you can convert it using the 'iconv' program. For example:

$ curl https://pt.lipsum.com/ | iconv -f ISO-8859-1 -t UTF-8 | ./FrequencyTableChars $ curl https://cn.lipsum.com/ | ./FrequencyTableChars

Output
$ rustc FrequencyTableChars.rs error: unknown format trait `d` --> FrequencyTableChars.rs:27:49 | 27 | println!("{}", format!("{0:05x}\t0x{0:x}\t{1:d}\t{2:.4f}", x, countTable.at(x), freq)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `f` --> FrequencyTableChars.rs:27:58 | 27 | println!("{}", format!("{0:05x}\t0x{0:x}\t{1:d}\t{2:.4f}", x, countTable.at(x), freq)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `c` --> FrequencyTableChars.rs:29:40 | 29 | println!("{}", format!("{0:05x}\t{1:c}\t{2:d}\t{3:.4f}", x, char::from_u32(x), countTable.at(x), freq)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `d` --> FrequencyTableChars.rs:29:47 | 29 | println!("{}", format!("{0:05x}\t{1:c}\t{2:d}\t{3:.4f}", x, char::from_u32(x), countTable.at(x), freq)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `f` --> FrequencyTableChars.rs:29:56 | 29 | println!("{}", format!("{0:05x}\t{1:c}\t{2:d}\t{3:.4f}", x, char::from_u32(x), countTable.at(x), freq)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error[E0425]: cannot find function `getcodepoint` in this scope --> FrequencyTableChars.rs:13:8 | 13 | while getcodepoint(c) { | ^^^^^^^^^^^^ not found in this scope error[E0425]: cannot find function `mapContains` in this scope --> FrequencyTableChars.rs:14:7 | 14 | if !mapContains(countTable,c) { | ^^^^^^^^^^^ not found in this scope error[E0308]: mismatched types --> FrequencyTableChars.rs:15:15 | 15 | countTable[c] = 1; | ^ expected `&i32`, found `i32` | help: consider borrowing here | 15 | countTable[&c] = 1; | + error[E0308]: mismatched types --> FrequencyTableChars.rs:17:15 | 17 | countTable[c] += 1; | ^ expected `&i32`, found `i32` | help: consider borrowing here | 17 | countTable[&c] += 1; | + error[E0425]: cannot find function `sortedNumericKeys` in this scope --> FrequencyTableChars.rs:23:28 | 23 | let sortedKeys:Vec<i32> = sortedNumericKeys(countTable); | ^^^^^^^^^^^^^^^^^ not found in this scope error[E0599]: no method named `at` found for struct `HashMap` in the current scope --> FrequencyTableChars.rs:25:39 | 25 | let freq:f64 = f64::from(countTable.at(x)) / f64::from(totalCount); | ^^ method not found in `HashMap<i32, isize>` error[E0277]: the trait bound `f64: From<isize>` is not satisfied --> FrequencyTableChars.rs:25:58 | 25 | let freq:f64 = f64::from(countTable.at(x)) / f64::from(totalCount); | --------- ^^^^^^^^^^ the trait `From<isize>` is not implemented for `f64` | | | required by a bound introduced by this call | = help: the following other types implement trait `From<T>`: <f64 as From<bool>> <f64 as From<f32>> <f64 as From<i16>> <f64 as From<i32>> <f64 as From<i8>> <f64 as From<u16>> <f64 as From<u32>> <f64 as From<u8>> error[E0425]: cannot find function `isctrl` in this scope --> FrequencyTableChars.rs:26:6 | 26 | if isctrl(x) { | ^^^^^^ not found in this scope error[E0599]: no method named `at` found for struct `HashMap` in the current scope --> FrequencyTableChars.rs:27:77 | 27 | println!("{}", format!("{0:05x}\t0x{0:x}\t{1:d}\t{2:.4f}", x, countTable.at(x), freq)); | ^^ method not found in `HashMap<i32, isize>` error[E0277]: `Option<char>` doesn't implement `std::fmt::Display` --> FrequencyTableChars.rs:29:64 | 29 | println!("{}", format!("{0:05x}\t{1:c}\t{2:d}\t{3:.4f}", x, char::from_u32(x), countTable.at(x), freq)); | ^^^^^^^^^^^^^^^^^ `Option<char>` cannot be formatted with the default formatter | = help: the trait `std::fmt::Display` is not implemented for `Option<char>` = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead = note: this error originates in the macro `$crate::__export::format_args` which comes from the expansion of the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0599]: no method named `at` found for struct `HashMap` in the current scope --> FrequencyTableChars.rs:29:94 | 29 | println!("{}", format!("{0:05x}\t{1:c}\t{2:d}\t{3:.4f}", x, char::from_u32(x), countTable.at(x), freq)); | ^^ method not found in `HashMap<i32, isize>` error: aborting due to 16 previous errors Some errors have detailed explanations: E0277, E0308, E0425, E0599. For more information about an error, try `rustc --explain E0277`. $ rustc FrequencyTableChars.rs error: unknown format trait `d` --> FrequencyTableChars.rs:27:49 | 27 | println!("{}", format!("{0:05x}\t0x{0:x}\t{1:d}\t{2:.4f}", x, countTable.at(x), freq)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `f` --> FrequencyTableChars.rs:27:58 | 27 | println!("{}", format!("{0:05x}\t0x{0:x}\t{1:d}\t{2:.4f}", x, countTable.at(x), freq)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `c` --> FrequencyTableChars.rs:29:40 | 29 | println!("{}", format!("{0:05x}\t{1:c}\t{2:d}\t{3:.4f}", x, char::from_u32(x), countTable.at(x), freq)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `d` --> FrequencyTableChars.rs:29:47 | 29 | println!("{}", format!("{0:05x}\t{1:c}\t{2:d}\t{3:.4f}", x, char::from_u32(x), countTable.at(x), freq)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `f` --> FrequencyTableChars.rs:29:56 | 29 | println!("{}", format!("{0:05x}\t{1:c}\t{2:d}\t{3:.4f}", x, char::from_u32(x), countTable.at(x), freq)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error[E0425]: cannot find function `getcodepoint` in this scope --> FrequencyTableChars.rs:13:8 | 13 | while getcodepoint(c) { | ^^^^^^^^^^^^ not found in this scope error[E0425]: cannot find function `mapContains` in this scope --> FrequencyTableChars.rs:14:7 | 14 | if !mapContains(countTable,c) { | ^^^^^^^^^^^ not found in this scope error[E0308]: mismatched types --> FrequencyTableChars.rs:15:15 | 15 | countTable[c] = 1; | ^ expected `&i32`, found `i32` | help: consider borrowing here | 15 | countTable[&c] = 1; | + error[E0308]: mismatched types --> FrequencyTableChars.rs:17:15 | 17 | countTable[c] += 1; | ^ expected `&i32`, found `i32` | help: consider borrowing here | 17 | countTable[&c] += 1; | + error[E0425]: cannot find function `sortedNumericKeys` in this scope --> FrequencyTableChars.rs:23:28 | 23 | let sortedKeys:Vec<i32> = sortedNumericKeys(countTable); | ^^^^^^^^^^^^^^^^^ not found in this scope error[E0599]: no method named `at` found for struct `HashMap` in the current scope --> FrequencyTableChars.rs:25:39 | 25 | let freq:f64 = f64::from(countTable.at(x)) / f64::from(totalCount); | ^^ method not found in `HashMap<i32, isize>` error[E0277]: the trait bound `f64: From<isize>` is not satisfied --> FrequencyTableChars.rs:25:58 | 25 | let freq:f64 = f64::from(countTable.at(x)) / f64::from(totalCount); | --------- ^^^^^^^^^^ the trait `From<isize>` is not implemented for `f64` | | | required by a bound introduced by this call | = help: the following other types implement trait `From<T>`: <f64 as From<bool>> <f64 as From<f32>> <f64 as From<i16>> <f64 as From<i32>> <f64 as From<i8>> <f64 as From<u16>> <f64 as From<u32>> <f64 as From<u8>> error[E0425]: cannot find function `isctrl` in this scope --> FrequencyTableChars.rs:26:6 | 26 | if isctrl(x) { | ^^^^^^ not found in this scope error[E0599]: no method named `at` found for struct `HashMap` in the current scope --> FrequencyTableChars.rs:27:77 | 27 | println!("{}", format!("{0:05x}\t0x{0:x}\t{1:d}\t{2:.4f}", x, countTable.at(x), freq)); | ^^ method not found in `HashMap<i32, isize>` error[E0277]: `Option<char>` doesn't implement `std::fmt::Display` --> FrequencyTableChars.rs:29:64 | 29 | println!("{}", format!("{0:05x}\t{1:c}\t{2:d}\t{3:.4f}", x, char::from_u32(x), countTable.at(x), freq)); | ^^^^^^^^^^^^^^^^^ `Option<char>` cannot be formatted with the default formatter | = help: the trait `std::fmt::Display` is not implemented for `Option<char>` = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead = note: this error originates in the macro `$crate::__export::format_args` which comes from the expansion of the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0599]: no method named `at` found for struct `HashMap` in the current scope --> FrequencyTableChars.rs:29:94 | 29 | println!("{}", format!("{0:05x}\t{1:c}\t{2:d}\t{3:.4f}", x, char::from_u32(x), countTable.at(x), freq)); | ^^ method not found in `HashMap<i32, isize>` error: aborting due to 16 previous errors Some errors have detailed explanations: E0277, E0308, E0425, E0599. For more information about an error, try `rustc --explain E0277`.

Solution