Perl Weekly Challenge 273: Percentage of Character

These are some answers to the Week 273, Task 1, of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Spoiler Alert: This weekly challenge deadline is due in a few days from now (on June 16, 2024 at 23:59). This blog post provides some solutions to this challenge. Please don’t read on if you intend to complete the challenge on your own.

Task 1: Percentage of Character

You are given a string, $str and a character $char.

Write a script to return the percentage, nearest whole, of given character in the given string.

Example 1

Input: $str = "perl", $char = "e"
Output: 25

Example 2

Input: $str = "java", $char = "a"
Output: 50

Example 3

Input: $str = "python", $char = "m"
Output: 0

Example 4

Input: $str = "ada", $char = "a"
Output: 67

Example 5

Input: $str = "ballerina", $char = "l"
Output: 22

Example 6

Input: $str = "analitik", $char = "k"
Output: 13

Percentage of Character in Raku

This is fairly easy. We just traverse the input string and count the number of occurrences of the given character. Finally, we compute the percentage to the string length and round it to the nearest integer with the round method.

sub percent ($str, $char) {
    my $count = 0;
    for $str.comb -> $ch {
        $count++ if $ch eq $char;
    }
    return (($count * 100) / $str.chars).round;
}

my @tests = <perl e>, <java a>, <python m>,
            <ada a>, <ballerina l>, <analitik k>;
for @tests -> @test {
    printf "%-10s - %-2s => ", @test;
    say percent @test[0], @test[1];
}

This program displays the following output.

$ raku ./percentage-of-character.raku
perl       - e  => 25
java       - a  => 50
python     - m  => 0
ada        - a  => 67
ballerina  - l  => 22
analitik   - k  => 13

Percentage of Character in Perl

This is a port to Perl of the above Raku program. Perl doesn't have a built-in round function, but it's easy to simulate it with the int function.

use strict;
use warnings;
use feature 'say';

sub percent {
    my ($str, $char) = @_;
    my $count = 0;
    for my $ch (split //, $str) {
        $count++ if $ch eq $char;
    }
    return int (0.5 + ($count * 100) / (length $str));
}

my @tests = ( [<perl e>], [<java a>], [<python m>],
            [<ada a>], [<ballerina l>], [<analitik k>] );
for my $test (@tests) {
    printf "%-10s - %-2s => ", $test->[0], $test->[1];
    say percent @$test;
}

This program displays the following output.

$ perl  ./percentage-of-character.pl
perl       - e  => 25
java       - a  => 50
python     - m  => 0
ada        - a  => 67
ballerina  - l  => 22
analitik   - k  => 13

Wrapping up

The next week Perl Weekly Challenge will start soon. If you want to participate in this challenge, please check https://perlweeklychallenge.org/ and make sure you answer the challenge before 23:59 BST (British summer time) on June 23, 2024. And, please, also spread the word about the Perl Weekly Challenge if you can.

Leave a comment

About laurent_r

user-pic I am the author of the "Think Perl 6" book (O'Reilly, 2017) and I blog about the Perl 5 and Raku programming languages.