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
|
# 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
defmodule Percentage do
def freq(str, char) do
String.split(str, "", trim: true)
|> Enum.frequencies
|> Map.get(char)
end
def out(str, char) do
IO.write( str <> " " <> char <> " -> ")
case freq(str, char) do
nil -> IO.puts "0"
x -> IO.puts( x / String.length(str)*100 |> round )
end
end
end
str = "perl"; char = "e";
Percentage.out(str, char)
str = "java"; char = "a";
Percentage.out(str, char)
str = "python"; char = "m";
Percentage.out(str, char)
str = "ada"; char = "a";
Percentage.out(str, char)
str = "ballerina"; char = "l";
Percentage.out(str, char)
str = "analitik"; char = "k";
Percentage.out(str, char)
|