Skip to content

Commit 39b0bc5

Browse files
committed
Add hexadecimal exercise
1 parent 6ef1878 commit 39b0bc5

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed

config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"queen-attack",
3030
"saddle-points",
3131
"triangle",
32+
"hexadecimal",
3233
"scrabble-score",
3334
"crypto-square",
3435
"diamond",

exercises/hexadecimal/example.exs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
defmodule Hexadecimal do
2+
@base 16
3+
@hex_table String.split("0123456789abcdef", "", trim: true)
4+
|> Enum.with_index
5+
|> Enum.into(%{})
6+
7+
@doc """
8+
Accept a string representing a hexadecimal value and return the
9+
corresponding decimal value.
10+
It returns the integer 0 if the hexadecimal is invalid.
11+
Otherwise returns an integer representing the decimal value.
12+
13+
## Examples
14+
15+
iex> Hexadecimal.to_decimal("invalid")
16+
0
17+
18+
iex> Hexadecimal.to_decimal("af")
19+
175
20+
21+
"""
22+
23+
@spec to_decimal(binary) :: integer
24+
def to_decimal(hex) do
25+
if invalid?(hex) do
26+
0
27+
else
28+
hex
29+
|> String.downcase
30+
|> String.reverse
31+
|> String.split("", trim: true)
32+
|> hex_to_int(0, 0)
33+
end
34+
end
35+
36+
defp invalid?(hex) do
37+
String.match?(hex, ~r/[^0-9a-fA-F]/)
38+
end
39+
40+
defp hex_to_int([], acc, _), do: round(acc)
41+
defp hex_to_int([char|hex], acc, power) do
42+
acc = acc + Map.get(@hex_table, char) * :math.pow(@base, power)
43+
hex_to_int(hex, acc, power + 1)
44+
end
45+
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
defmodule Hexadecimal do
2+
@doc """
3+
Accept a string representing a hexadecimal value and returns the
4+
corresponding decimal value.
5+
It returns the integer 0 if the hexadecimal is invalid.
6+
Otherwise returns an integer representing the decimal value.
7+
8+
## Examples
9+
10+
iex> Hexadecimal.to_decimal("invalid")
11+
0
12+
13+
iex> Hexadecimal.to_decimal("af")
14+
175
15+
16+
"""
17+
18+
@spec to_decimal(binary) :: integer
19+
def to_decimal(hex) do
20+
21+
end
22+
end
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
if !System.get_env("EXERCISM_TEST_EXAMPLES") do
2+
Code.load_file("hexadecimal.exs")
3+
end
4+
5+
ExUnit.start
6+
ExUnit.configure exclude: :pending, trace: true
7+
8+
defmodule ChangeTest do
9+
use ExUnit.Case
10+
11+
test "returns 1 when hex is 1" do
12+
assert Hexadecimal.to_decimal("1") == 1
13+
end
14+
15+
@tag :pending
16+
test "returns 12 when hex is c" do
17+
assert Hexadecimal.to_decimal("c") == 12
18+
end
19+
20+
@tag :pending
21+
test "hexadecimal is case insensitive" do
22+
assert Hexadecimal.to_decimal("C") == 12
23+
end
24+
25+
@tag :pending
26+
test "returns 16 when hex is 10" do
27+
assert Hexadecimal.to_decimal("10") == 16
28+
end
29+
30+
@tag :pending
31+
test "returns 175 when hex is af" do
32+
assert Hexadecimal.to_decimal("af") == 175
33+
end
34+
35+
@tag :pending
36+
test "returns 256 when hex is 100" do
37+
assert Hexadecimal.to_decimal("100") == 256
38+
end
39+
40+
@tag :pending
41+
test "returns 105_166 when hex is 19ace" do
42+
assert Hexadecimal.to_decimal("19ace") == 105_166
43+
end
44+
45+
@tag :pending
46+
test "returns 0 when hex is invalid" do
47+
assert Hexadecimal.to_decimal("carrot") == 0
48+
end
49+
50+
@tag :pending
51+
test "returns 0 when hex represents black" do
52+
assert Hexadecimal.to_decimal("000000") == 0
53+
end
54+
55+
@tag :pending
56+
test "returns 16_777_215 when hex represents white" do
57+
assert Hexadecimal.to_decimal("ffffff") == 16_777_215
58+
end
59+
60+
@tag :pending
61+
test "returns 16_776_960 when hex represents yellow" do
62+
assert Hexadecimal.to_decimal("ffff00") == 16_776_960
63+
end
64+
end

0 commit comments

Comments
 (0)