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

Commit 67f5839

Browse files
committed
Task two is done
1 parent 091701f commit 67f5839

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

w1/mini_max_sum.ex

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
defmodule Hackerrank.W1.MiniMaxSum do
2+
@moduledoc """
3+
Mini Max Sum – Challenge 2 – Week 1
4+
<https://www.hackerrank.com/challenges/three-month-preparation-kit-mini-max-sum/problem>
5+
"""
6+
7+
@doc """
8+
Calculates the minimum and maximum values obtainable by summing exactly
9+
four of five integers. Optionally prints the result as space-separated values.
10+
11+
## Parameters
12+
- `arr`: a list of integers
13+
14+
## Returns
15+
- a list with two integers: `[min_sum, max_sum]`
16+
17+
## Examples
18+
19+
iex> mini_max_sum([1, 2, 3, 4, 5])
20+
[10, 14]
21+
"""
22+
@spec challenge([integer()]) :: [integer()]
23+
def challenge(arr) do
24+
# sort the array
25+
arr = Enum.sort(arr)
26+
27+
# slice the mins and sum
28+
[
29+
Enum.take(arr, 4) |> Enum.sum(),
30+
Enum.drop(arr, 1) |> Enum.sum()
31+
]
32+
end
33+
34+
end

w1/mini_max_sum_test.exs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
defmodule Hackerank.W1.MiniMaxSumTests do
2+
3+
use ExUnit.Case
4+
alias Hackerrank.Test.Helper
5+
alias Hackerrank.W1.MiniMaxSum
6+
7+
@pattern "w1/tc/2_*.txt"
8+
9+
test "run the tests" do
10+
Helper.test_all(@pattern, &parse_args/1, &parse_expected/1 )
11+
|> Enum.each(fn [args, expected] ->
12+
assert MiniMaxSum.challenge(args) == expected
13+
end)
14+
end
15+
16+
def parse_args(raw) do
17+
# get the data out of a list
18+
[raw] = raw
19+
20+
# now split it
21+
String.split(raw, " ")
22+
|> Enum.map(&String.to_integer/1)
23+
end
24+
25+
def parse_expected(raw) do
26+
# in this challange the raw data is already correct - a list of floaty-strings
27+
# get the data out of a list
28+
[raw] = raw
29+
30+
# now split it
31+
String.split(raw, " ")
32+
|> Enum.map(&String.to_integer/1)
33+
end
34+
35+
end

0 commit comments

Comments
 (0)