loading...

re: Daily Challenge #248 - Chinese Numerals VIEW POST

FULL DISCUSSION
 

First time doing one of these. Figured I'd throw out one of those weird languages (Raku):

use v6;

sub toChineseNumeral(Int $number = 0) {
    my $numerals = <         >;
    my $places = $number.polymod(10, 10, 10, 10).reverse;
    my Str $numbers = join "", do for ^4 {
        my Int $number = $places[$_];
        next if $number == 0;
        $numerals[$number] ~ <   >[$_];
    }

    given $number {
        when 0..9 { $numerals[$places.tail] }
        when 10 { $numbers.substr(1) }
        when 11..19 { $numbers.substr(1) ~ $numerals[$places.tail] }
        default { $numbers ~ $numerals[$places.tail] }
    }
}

sub MAIN() {
    for [9, 10, 11, 18, 21, 110, 111, 123, 1000, 10000, 24681] {
        say $_ ~ ": " ~ toChineseNumeral($_)
    }
}
code of conduct - report abuse