We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d6d9fc9 commit 32c9dd8Copy full SHA for 32c9dd8
CountLeaves.exs
@@ -0,0 +1,25 @@
1
+defmodule CountLeaves do
2
+ def of([]), do: 0
3
+ def of([head | tail]), do: of(head) + of(tail)
4
+ def of(_), do: 1
5
+end
6
+
7
+ExUnit.start
8
9
+defmodule CountLeavesTest do
10
+ use ExUnit.Case, async: true
11
12
+ test "it counts leaves in an empty list" do
13
+ assert CountLeaves.of([]) == 0
14
+ end
15
16
+ test "it count leaves in a flat list" do
17
+ assert CountLeaves.of([1,2,3]) == 3
18
19
20
+ test "it count leaves in nested lists" do
21
+ assert CountLeaves.of([[1, 2], 3]) == 3
22
+ assert CountLeaves.of([1, [2, 3]]) == 3
23
+ assert CountLeaves.of([[1, 2], [3, 4]]) == 4
24
25
0 commit comments