dlang-book/01-знакомство-с-языком-d/src/chapter-1-4-3/app.d

29 lines
721 B
D
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import std.algorithm, std.stdio, std.string;
void main()
{
// Рас­счи­тать таб­ли­цу час­тот
uint[string] freqs;
foreach (line; stdin.byLine())
{
foreach (word; line.strip.splitter)
{
++freqs[word.idup];
}
}
// На­пе­ча­тать таб­ли­цу час­тот
foreach (key, value; freqs)
{
writefln("%6u\t%s", value, key);
}
// На­пе­ча­тать таб­ли­цу час­тот с сортировкой
string[] words = freqs.keys;
sort!((a, b) { return freqs[a] > freqs[b]; })(words);
foreach (word; words)
{
writefln("%6u\t%s", freqs[word], word);
}
}