<commit_before>use v6;

my $total_num = 0;
my $sum = 0;

# rec is short for "recurse".
sub rec($factor, @counts, $result)
{
    say @counts.join(",");
    my $cnt = [+] @counts;
    if $cnt == 0
    {
        $sum += $factor * $result;
        $total_num += $factor;
    }
    else
    {
        for (@counts.kv) -> $size, $f
        {
            if $f > 0
            {
                my @c = @counts;
                @c[$size]--;
                for $size ^.. 4 -> $s
                {
                    @c[$s]++;
                }
                rec( $factor*$f/$cnt, @c, $result + ($cnt == 1) );
            }
        }
    }
    return;
}

rec(1,[1],0);

say "Result == ", ($sum / $total_num - 2).fmt("%.6f");
<commit_msg>Optimisation. Get rid of $total_num .<commit_after>use v6;

my $sum = 0;

# rec is short for "recurse".
sub rec($factor, @counts, $result)
{
    say @counts.join(",");
    my $cnt = [+] @counts;
    if $cnt == 0
    {
        $sum += $factor * $result;
    }
    else
    {
        for (@counts.kv) -> $size, $f
        {
            if $f > 0
            {
                my @c = @counts;
                @c[$size]--;
                for $size ^.. 4 -> $s
                {
                    @c[$s]++;
                }
                rec( $factor*$f/$cnt, @c, $result + ($cnt == 1) );
            }
        }
    }
    return;
}

rec(1,[1],0);

say "Result == ", ($sum - 2).fmt("%.6f");

{'fraction_non_alphanumeric': 0.19719298245614036, 'fraction_numerical': 0.016140350877192983, 'mean_word_length': 1.1347305389221556, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3204890', 'n_tokens_mistral': 543, 'n_tokens_neox': 515, 'n_words': 119}