Skip to content
This repository was archived by the owner on Mar 1, 2026. It is now read-only.

Commit a89319a

Browse files
authored
W1/3 - Time Conversion (elixir) (#141)
The third challenge of week one. Another one off #117 Closes #124
1 parent b02cb9b commit a89319a

9 files changed

Lines changed: 75 additions & 11 deletions

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule Hackerank.MixProject do
1+
defmodule HackerRank.MixProject do
22
use Mix.Project
33

44
@doc """

test/test_helper.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule Hackerrank.Test.Helper do
1+
defmodule HackerRank.Test.Helper do
22
@moduledoc """
33
A set of utilities for testing the Elixir HackerRank challenges
44
"""

w1/mini_max_sum.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule Hackerrank.W1.MiniMaxSum do
1+
defmodule HackerRank.W1.MiniMaxSum do
22
@moduledoc """
33
Mini Max Sum – Challenge 2 – Week 1
44
<https://www.hackerrank.com/challenges/three-month-preparation-kit-mini-max-sum/problem>

w1/mini_max_sum_test.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
defmodule Hackerank.W1.MiniMaxSumTests do
1+
defmodule HackerRank.W1.MiniMaxSumTests do
22

33
use ExUnit.Case
4-
alias Hackerrank.Test.Helper
5-
alias Hackerrank.W1.MiniMaxSum
4+
alias HackerRank.Test.Helper
5+
alias HackerRank.W1.MiniMaxSum
66

77
@pattern "w1/tc/2_*.txt"
88

w1/plus_minus.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule Hackerrank.W1.PlusMinus do
1+
defmodule HackerRank.W1.PlusMinus do
22
@doc """
33
The challenge function.
44
"""

w1/plus_minus.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func PlusMinus(arr []int32) [3]string {
4444
solution[1] = fmt.Sprintf("%.6f", n_ratio)
4545
solution[2] = fmt.Sprintf("%.6f", z_ratio)
4646

47-
// Hackerank wants you to print them
47+
// HackerRank wants you to print them
4848
for _, value := range solution {
4949
fmt.Println(value)
5050
}

w1/plus_minus_test.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
defmodule Hackerank.W1.PlusMinusTest do
1+
defmodule HackerRank.W1.PlusMinusTest do
22

33
use ExUnit.Case
4-
alias Hackerrank.Test.Helper
5-
alias Hackerrank.W1.PlusMinus
4+
alias HackerRank.Test.Helper
5+
alias HackerRank.W1.PlusMinus
66

77
@pattern "w1/tc/1_*.txt"
88

w1/time_conversion.ex

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
defmodule HackerRank.W1.TimeConversion do
2+
@moduledoc """
3+
Time Conversion – Challenge 3 – Week 1
4+
<https://www.hackerrank.com/challenges/three-month-preparation-kit-time-conversion/problem>
5+
"""
6+
7+
@doc """
8+
Converts 12-hour AM/PM time format to 24-hour military time.
9+
10+
## Examples
11+
12+
iex> challenge("07:05:45PM")
13+
"19:05:45"
14+
15+
## Constraints
16+
17+
- Input format: `"hh:mm:ssAM"` or `"hh:mm:ssPM"`
18+
- Always returns a string in 24-hour format
19+
"""
20+
@spec challenge(String.t()) :: String.t()
21+
def challenge(s) do
22+
# Dates / Times are not Elixis strong suit
23+
# However, binary string pattern matching is so let's do that then
24+
[hh, mm, ss_am_pm] = String.split(s, ":")
25+
26+
# now we need am / pm
27+
<<ss::binary-size(2), period::binary>> = ss_am_pm
28+
29+
# now parse into the correct types
30+
hour = parse_hour(hh, period)
31+
minute = String.to_integer(mm)
32+
second = String.to_integer(ss)
33+
34+
# run everything through pad and return with Enum.map_join
35+
Enum.map_join([hour, minute, second], ":", &pad2/1)
36+
end
37+
38+
defp parse_hour("12", "AM"), do: 0
39+
defp parse_hour(h, "AM"), do: String.to_integer(h)
40+
defp parse_hour("12", "PM"), do: 12
41+
defp parse_hour(h, "PM"), do: String.to_integer(h) + 12
42+
43+
defp pad2(number) do
44+
:io_lib.format("~2..0B", [number]) |> to_string()
45+
end
46+
end

w1/time_conversion_test.exs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
defmodule HackerRank.W1.TimeConversionTests do
2+
use ExUnit.Case
3+
alias HackerRank.Test.Helper
4+
alias HackerRank.W1.TimeConversion
5+
6+
@pattern ~w"w1/tc/3_*.txt"
7+
8+
test "run the tests" do
9+
Helper.test_all(@pattern, &parse_args/1, &parse_expected/1 )
10+
|> Enum.each(fn [args, expected] ->
11+
assert TimeConversion.challenge(args) == expected
12+
end)
13+
end
14+
15+
def parse_args([raw]), do: raw
16+
def parse_expected([raw]), do: raw
17+
18+
end

0 commit comments

Comments
 (0)