Skip to content

Instantly share code, notes, and snippets.

View grufino's full-sized avatar

Gabriel Florêncio Rufino grufino

View GitHub Profile
test "lists of different size as input result in error" do
stats = [[0, 10, 20], [20, 10, 0], [10, 10]]
assert MissingData.average_list(stats) == :error
end
test "generate list of numbers given same size input list" do
stats = [[0, 10, 20], [20, 10, 0], [10, 10, 10]]
assert MissingData.average_list(stats) == [15, 10, 15]
end
{"ac_ratio":
{"accelerations":
[100.0,166.0,0,119.0,147.0,92.0,0,0,118.0,0,127.0,133.0,
192.0,0,103.0,133.0,0,142.0,149.0,181.0,0,140.0,0,0,0,0,0,0],
"sprint_distances":
[153.002649391,231.992722498,0,272.078987806,167.660927286,198.818410826,0,0,376.611274507,
0,320.307097832,255.344880942,743.19938242,0,603.015597359,40.2846969563,0,789.82629535,
130.172380645,807.139266604,0,653.654959634,0,0,0,0,0,0]
}
}
property "index number is always it's average in all indexes" do
check all size <- integer(1..100),
number_lists <- list_of(number_list(size), min_length: 1, max_length: 100) do
# since this test is based on index it also tests that ordering is maintained
Enum.map(1..(size - 1), fn index ->
index_avg =
Enum.map(number_lists, fn number_list -> Enum.at(number_list, index) end)
|> Enum.filter(fn value -> value != 0 && value != nil end)
|> Math.Enum.mean()
property "always returns list of the same size" do
check all size <- integer(0..100),
number_lists <- list_of(number_list(size), min_length: 1, max_length: 100) do
assert length(MissingData.average_list(number_lists)) == length(Enum.random(number_lists))
end
end
defp number_list(size) do
gen all number_list <- list_of(integer(0..99999), length: size) do
number_list
end
end
@grufino
grufino / missing_data.ex
Last active August 20, 2019 21:01
Module to give averages based on a list of lists of numbers.
defmodule MissingData do
@doc """
Example input:
> MissingData.average_list([[0, 10, 20], [20, 10, 0], [10, 10, 10]])
Example output:
> [15, 10, 15]
"""
def average_list(list_of_lists) do
if have_same_sizes?(list_of_lists) do