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

Commit efa0dc2

Browse files
committed
W1/4 Breaking Records completed in elixir
1 parent 34cc652 commit efa0dc2

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

w1/breaking_records.ex

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,38 @@ defmodule HackerRank.W1.BreakingRecords do
2121
iex> breaking_records([3, 4, 21, 36, 10, 28, 35, 5, 24, 42])
2222
[4, 0]
2323
"""
24-
@spec breaking_records([integer()]) :: [integer()]
24+
@spec challenge([integer()]) :: [integer()]
2525
def challenge(scores) do
26-
[1,2]
26+
# calculate the minimum and maximum records
27+
process_scores(scores, [], [], [])
2728
end
2829

30+
def process_scores([current | rest], processed, min_b, max_b) do
31+
32+
# get the max value in processed, if there is no value
33+
# add the current to procesed and recurse.
34+
mx = Enum.max(processed, fn -> :nil end)
35+
if mx == :nil do
36+
process_scores(rest, [current | processed], min_b, max_b)
37+
end
38+
39+
# get the minimum value in processed, and fallback to zero
40+
mn = Enum.min(processed, fn -> 0 end)
41+
42+
# now process everything
43+
cond do
44+
current > mx ->
45+
process_scores(rest, [current| processed] , min_b, [current| max_b])
46+
47+
current < mn ->
48+
process_scores(rest, [current| processed], [ current | min_b] , max_b)
49+
50+
true ->
51+
process_scores(rest, [current | processed] , min_b, max_b)
52+
end
53+
end
54+
55+
def process_scores([], _processed, min_b, max_b) do
56+
[length(max_b), length(min_b)]
57+
end
2958
end

w1/breaking_records_test.exs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ defmodule HackerRank.W1.BreakingRecordsTests do
88
test "run the tests" do
99
Helper.test_all(@pattern, &parse_args/1, &parse_expected/1 )
1010
|> Enum.each(fn [args, expected] ->
11-
IO.inspect(args)
1211
assert BreakingRecords.challenge(args) == expected
1312
end)
1413
end
@@ -17,6 +16,8 @@ defmodule HackerRank.W1.BreakingRecordsTests do
1716
# now turn in to Integers
1817
raw |> String.split(" ") |> Enum.map(&String.to_integer/1)
1918
end
20-
def parse_expected([raw]), do: raw
19+
def parse_expected([raw]) do
20+
raw |> String.split(" ") |> Enum.map(&String.to_integer/1)
21+
end
2122

2223
end

0 commit comments

Comments
 (0)