author | Christian Urban <christian.urban@kcl.ac.uk> |
Mon, 10 Oct 2022 15:07:31 +0100 | |
changeset 887 | d9d52f3e7935 |
parent 882 | 5fcad75ade92 |
child 965 | 94f5cce73a4f |
permissions | -rw-r--r-- |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
def concat(A: Set[String], B: Set[String]) : Set[String] = |
564 | 2 |
for (x <- A ; y <- B) yield x ++ y |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
3 |
|
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
4 |
def pow(A: Set[String], n: Int) : Set[String] = n match { |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
case 0 => Set("") |
882 | 6 |
case n => concat(A, pow(A, n - 1)) |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
|
882 | 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 |
||
15 |
||
801 | 16 |
val A = Set("a", "b", "c", "d", "e") |
17 |
val B = Set("a", "b", "c", "d", "") |
|
18 |
pow(A, 4).size |
|
19 |
pow(B, 4).size |
|
882 | 20 |
powT(A, 4).size |
21 |
powT(B, 4).size |
|
801 | 22 |
|
23 |
||
882 | 24 |
val C = Set("a", "b") |
25 |
||
26 |
pow(C, 100).size |
|
27 |
powT(C, 100000) |
|
28 |
||
676 | 29 |
val B = Set("aaa", "aaaa") |
30 |
concat(A, B).size // -> 3 |
|
669 | 31 |
|
32 |
||
33 |
||
650 | 34 |
val A = Set("1", "2", "3", "") |
35 |
val B = Set("1", "2", "3", "4", "5", "6", "") |
|
36 |
concat(A, B).size // -> 28 |
|
37 |
pow(B, 3).size |
|
38 |
||
39 |
||
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
40 |
val A = Set("a", "b", "c", "d") |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
41 |
pow(A, 4).size // -> 256 |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
|
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
43 |
val B = Set("a", "b", "c", "") |
572 | 44 |
pow(B, 4) |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
pow(B, 4).size // -> 121 |
638 | 46 |
pow(B, 3).size |
471 | 47 |
|
48 |
||
397
cf3ca219c727
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
49 |
val B2 = Set("a", "b", "c", "") |
cf3ca219c727
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
50 |
pow(B2, 3).size // -> 40 |
cf3ca219c727
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
51 |
|
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
52 |
val C = Set("a", "b", "") |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
53 |
pow(C, 2) |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
54 |
pow(C, 2).size // -> 7 |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
55 |
|
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
56 |
pow(C, 3) |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
57 |
pow(C, 3).size // -> 15 |
525 | 58 |
|
59 |
||
572 | 60 |
//val A = Set("a", "b", "c", "d") |
61 |
//pow(A, 4).size |
|
525 | 62 |
|
572 | 63 |
//val A = Set("a", "b", "c") |
64 |
//pow(A, 5).size |
|
525 | 65 |
|
572 | 66 |
//val A = Set("a", "b", "") |
67 |
//pow(A, 5).size |
|
564 | 68 |
|
69 |
||
572 | 70 |
for (n <- (0 to 6).toList) |
71 |
yield pow(B, n).size |
|
577 | 72 |
|
73 |
||
74 |
||
75 |
||
76 |
||
77 |
val A = Set("a", "b", "c") |
|
78 |
pow(A, 3) |
|
618 | 79 |
pow(A, 3).size |
577 | 80 |
|
81 |
val B = Set("a", "b", "") |
|
82 |
pow(B, 4) |
|
618 | 83 |
pow(B, 4).size |
765 | 84 |
|
768 | 85 |
val C = Set("a", "") |
86 |
pow(C, 4) |
|
87 |
pow(C, 4).size |
|
765 | 88 |
|
89 |
val SA = Set("a", "b", "c", "d") |
|
90 |
val SB = Set("0", "1", "2", "3", "4", "5", "6") |
|
91 |
||
92 |
concat(SA, SB).size |
|
93 |
||
94 |
||
95 |
val SA2 = Set("ab", "a", "c", "d") |
|
96 |
val SB2 = Set("bc", "c", "2", "3", "4", "5", "6") |
|
97 |
||
98 |
concat(SA2, SB2).size |
|
99 |
||
100 |
||
101 |
/* |
|
102 |
((a b) c) d |
|
103 |
a (b (c d)) |
|
104 |
(a b) (c d) |
|
105 |
(a (b c)) d |
|
106 |
a ((b c) d) |
|
107 |
*/ |