aboutsummaryrefslogtreecommitdiff
path: root/challenge-273/spadacciniweb/perl/ch-1.pl
blob: 86d0c9ccc883e6219d1d6a35d7e7faebf1374285 (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
#!/usr/bin/env perl

# Task 1: Percentage of Character
# Submitted by: Mohammad Sajid Anwar
# 
# 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

use strict;
use warnings;

my $str = "perl"; my $char = "e";
percentage($str, $char);

$str = "java"; $char = "a";
percentage($str, $char);

$str = "python"; $char = "m";
percentage($str, $char);

$str = "ada"; $char = "a";
percentage($str, $char);

$str = "ballerina"; $char = "l";
percentage($str, $char);

$str = "analitik"; $char = "k";
percentage($str, $char);

exit 0;

sub percentage {
    my $str = shift;
    my $char = shift;

    my %freq;
    $freq{$_}++
        foreach split //, $str;
    printf "str %s char %s -> perc %.0f\n",
        $str, $char,
        $freq{$char}
            ? $freq{$char}/length($str)*100
            : 0;
}