Quick actions

cmd+k|ctrl+k

Navigation

Languages

Untitled

Snippet info

Language

Raku

Visibility

unlisted

Author

legacy-anonymous

Created

2023-01-21T16:19:51.245146Z

Updated

2023-01-21T16:19:51.245146Z

sub char_count($str) {
    my %res;
    my @chars = $str.comb;
    for @chars -> $c {
        %res{$c}++;
    }
    %res;
}

my $str = 'Article 1. All human beings are born free and equal in dignity and rights. They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood.';

my %char_count = char_count($str);
my @char_count_sorted = %char_count
                            .pairs
                            .sort(sub ($e) {
                                $e.value
                            })
                            .reverse;

say "String: $str";
say '';
say 'Character occurence (most frequent to least frequent):';
for @char_count_sorted -> $pair {
    my $char = $pair.key;
    my $count = $pair.value;
    my $display_char;
    if ($char eq ' ') {
        $display_char = '<space>';
    } else {
        $display_char = $char;
    }
    say "$display_char: $count";
}
INFO