1 def concat(A: Set[String], B: Set[String]) : Set[String] = |
1 def concat(A: Set[String], B: Set[String]) : Set[String] = |
2 for (x <- A ; y <- B) yield x ++ y |
2 for (x <- A ; y <- B) yield x ++ y |
3 |
3 |
4 def pow(A: Set[String], n: Int) : Set[String] = n match { |
4 def pow(A: Set[String], n: Int) : Set[String] = n match { |
5 case 0 => Set("") |
5 case 0 => Set("") |
6 case n => concat(A, pow(A, n- 1)) |
6 case n => concat(A, pow(A, n - 1)) |
7 } |
7 } |
|
8 |
|
9 def powT(A: Set[String], n: Int, acc: Set[String] = Set("")) : Set[String] = |
|
10 n match { |
|
11 case 0 => acc |
|
12 case n => powT(A, n - 1, concat(acc, A)) |
|
13 } |
|
14 |
8 |
15 |
9 val A = Set("a", "b", "c", "d", "e") |
16 val A = Set("a", "b", "c", "d", "e") |
10 val B = Set("a", "b", "c", "d", "") |
17 val B = Set("a", "b", "c", "d", "") |
11 pow(A, 4).size |
18 pow(A, 4).size |
12 pow(B, 4).size |
19 pow(B, 4).size |
|
20 powT(A, 4).size |
|
21 powT(B, 4).size |
13 |
22 |
14 |
23 |
15 val A = Set("aa", "a") |
24 val C = Set("a", "b") |
|
25 |
|
26 pow(C, 100).size |
|
27 powT(C, 100000) |
|
28 |
16 val B = Set("aaa", "aaaa") |
29 val B = Set("aaa", "aaaa") |
17 concat(A, B).size // -> 3 |
30 concat(A, B).size // -> 3 |
18 |
31 |
19 |
32 |
20 |
33 |