author | Christian Urban <christian.urban@kcl.ac.uk> |
Fri, 24 Sep 2021 23:56:31 +0100 | |
changeset 839 | 7af70f9b30ab |
parent 801 | 7aab258bf72a |
child 882 | 5fcad75ade92 |
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("") |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
case n => concat(A, pow(A, n- 1)) |
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 |
|
801 | 9 |
val A = Set("a", "b", "c", "d", "e") |
10 |
val B = Set("a", "b", "c", "d", "") |
|
11 |
pow(A, 4).size |
|
12 |
pow(B, 4).size |
|
13 |
||
14 |
||
676 | 15 |
val A = Set("aa", "a") |
16 |
val B = Set("aaa", "aaaa") |
|
17 |
concat(A, B).size // -> 3 |
|
669 | 18 |
|
19 |
||
20 |
||
650 | 21 |
val A = Set("1", "2", "3", "") |
22 |
val B = Set("1", "2", "3", "4", "5", "6", "") |
|
23 |
concat(A, B).size // -> 28 |
|
24 |
pow(B, 3).size |
|
25 |
||
26 |
||
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
27 |
val A = Set("a", "b", "c", "d") |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
28 |
pow(A, 4).size // -> 256 |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
29 |
|
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
30 |
val B = Set("a", "b", "c", "") |
572 | 31 |
pow(B, 4) |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
32 |
pow(B, 4).size // -> 121 |
638 | 33 |
pow(B, 3).size |
471 | 34 |
|
35 |
||
397
cf3ca219c727
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
36 |
val B2 = Set("a", "b", "c", "") |
cf3ca219c727
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
37 |
pow(B2, 3).size // -> 40 |
cf3ca219c727
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
38 |
|
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
39 |
val C = Set("a", "b", "") |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
40 |
pow(C, 2) |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
41 |
pow(C, 2).size // -> 7 |
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 |
pow(C, 3) |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
44 |
pow(C, 3).size // -> 15 |
525 | 45 |
|
46 |
||
572 | 47 |
//val A = Set("a", "b", "c", "d") |
48 |
//pow(A, 4).size |
|
525 | 49 |
|
572 | 50 |
//val A = Set("a", "b", "c") |
51 |
//pow(A, 5).size |
|
525 | 52 |
|
572 | 53 |
//val A = Set("a", "b", "") |
54 |
//pow(A, 5).size |
|
564 | 55 |
|
56 |
||
572 | 57 |
for (n <- (0 to 6).toList) |
58 |
yield pow(B, n).size |
|
577 | 59 |
|
60 |
||
61 |
||
62 |
||
63 |
||
64 |
val A = Set("a", "b", "c") |
|
65 |
pow(A, 3) |
|
618 | 66 |
pow(A, 3).size |
577 | 67 |
|
68 |
val B = Set("a", "b", "") |
|
69 |
pow(B, 4) |
|
618 | 70 |
pow(B, 4).size |
765 | 71 |
|
768 | 72 |
val C = Set("a", "") |
73 |
pow(C, 4) |
|
74 |
pow(C, 4).size |
|
765 | 75 |
|
76 |
val SA = Set("a", "b", "c", "d") |
|
77 |
val SB = Set("0", "1", "2", "3", "4", "5", "6") |
|
78 |
||
79 |
concat(SA, SB).size |
|
80 |
||
81 |
||
82 |
val SA2 = Set("ab", "a", "c", "d") |
|
83 |
val SB2 = Set("bc", "c", "2", "3", "4", "5", "6") |
|
84 |
||
85 |
concat(SA2, SB2).size |
|
86 |
||
87 |
||
88 |
/* |
|
89 |
((a b) c) d |
|
90 |
a (b (c d)) |
|
91 |
(a b) (c d) |
|
92 |
(a (b c)) d |
|
93 |
a ((b c) d) |
|
94 |
*/ |