Hi guys, I started to learn Elixir at University and I need to write a function (sit!) for homework. I have been struggling with this for days now.
The description of the task:
How many ways can ‘k’ people sit down in ‘n>2’ rows, but moving from the front to the back every row has ‘d’ less people.
Specifications:
@type variant :: { h :: integer, d :: integer }
@type seatings :: { n :: integer, vs :: [ variant ] }
@spec sit!( k :: integer ) :: r :: { m :: integer, ss :: [ seatings ] }
k: number of the people
m: maximum variation of the seating
ss: list of {n, vs} pairs
n: number of rows of the seating
h: the shortest row (back row/first row)
d: the difference between every neighboring rows
Here is an example:
10 - >
XXXX
XXX
XX
X
iex> Khf1.sit! 10
{1, [{4, [{1, 1}]}]}
In this case k=10, m=1 because there is only one variation, n=4 because there is 4 rows, h=1 because the shortest row is 1, d=1 because the difference between rows is 1.
I need to solve this problem with recursion and the output must look like the specification.
More examples:
iex> Khf1.sit! 101
{0, []}
iex> Khf1.sit! 15
{5, [{3, [{1, 4}, {2, 3}, {3, 2}, {4, 1}]}, {5, [{1, 1}]}]}
iex> Khf1.sit! 14
{1, [{4, [{2, 1}]}]}
iex> Khf1.sit! 10
{1, [{4, [{1, 1}]}]}
iex> Khf1.sit! 9
{2, [{3, [{1, 2}, {2, 1}]}]}
iex> Khf1.sit! 5
{0, []}
If someone could give me some tips how to do this that would be very helpful.
Thank you.