aboutsummaryrefslogtreecommitdiff
path: root/challenge-273/iangoodnight/perl/Task1.pm
blob: 645852ce09bb857e1fae3882b5bc434f79cef158 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/perl

=begin comment

## Task 1: Percentage of Character

**Submitted by:** [Mohammad Sajid Anwar][1]

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
```

[1]: https://manwar.org/

=end comment
=cut

use strict;
use warnings;
use autodie;

use Exporter;
use Readonly;

our $VERSION = '1.0.0';

our @EXPORT_OK = qw(percentage_of_character);

Readonly::Scalar my $EMPTY              => q{};    # for splitting the string
Readonly::Scalar my $PERCENTAGE_FACTOR  => 100;    # for converting decimal
Readonly::Scalar my $ROUNDING_THRESHOLD => 0.5;    # for rounding the percentage

sub percentage_of_character {
    my ( $str, $char ) = @_;

    # string length ends up as the denominator of our percentage calculation
    my $string_length = length $str // 0;

    # if the string is empty, return 0 rather than dividing by zero
    if ( $string_length == 0 || !defined $char ) {
        return 0;
    }

    # grab characters as a list
    my @characters = split $EMPTY, $str;

    # filter down to the characters that match the given character
    my @matching_characters = grep { $_ eq $char } @characters;

    # count the number of matching characters
    my $matching_characters_count = scalar @matching_characters;

    # calculate the percentage as a decimal
    my $decimal_percentage = $matching_characters_count / $string_length;

    # convert the decimal to a percentage
    my $percentage = $decimal_percentage * $PERCENTAGE_FACTOR;

    # round the percentage to the nearest whole number
    my $rounded_percentage = int( $percentage + $ROUNDING_THRESHOLD );

    # return the rounded percentage as the result
    return $rounded_percentage;
}

1;

__END__

=encoding utf-8

=head1 NAME

Task 1: Percentage of Character

=head1 VERSION

This documentation refers to Task 1: Percentage of Character version 1.0.0

=head1 EXPORTS

The function C<percentage_of_character> is exported by default.

=head1 USAGE

    use strict;
    use warnings;
    use feature qw(say);
    use Task1 qw(percentage_of_character);

    my $str  = 'perl';
    my $char = 'e';
    say percentage_of_character( $str, $char ); # output: 25

=head1 DESCRIPTION

Given a string, C<$str> and a character C<$char>, the function
C<percentage_of_character> returns the percentage, rounded to the nearest whole
number, of the given character in the given string.

=head1 REQUIRED ARGUMENTS

=over 4

=item C<$str>

A string.

=item C<$char>

A character.

=back

=head1 OPTIONS

None.

=head1 DIAGNOSTICS

None.

=head1 EXIT STATUS

None.

=head1 CONFIGURATION

None.

=head1 DEPENDENCIES

=over 4

=item * Exporter

=item * Readonly

=back

=head1 INCOMPATIBILITIES

None reported.

=head1 BUGS AND LIMITATIONS

None reported.

=head1 AUTHOR

Ian Goodnight

=head1 LICENSE AND COPYRIGHT

This software is released under the terms of the GNU General Public License
Version 3.

=cut