progs/pow.scala
author Christian Urban <urbanc@in.tum.de>
Sun, 13 Oct 2019 00:53:20 +0100
changeset 654 fb6192488b91
parent 650 3031e3379ea3
child 669 2f5a4d76756d
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 525
diff changeset
     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
650
3031e3379ea3 updated
Christian Urban <urbanc@in.tum.de>
parents: 638
diff changeset
     9
val A = Set("1", "2", "3", "")
3031e3379ea3 updated
Christian Urban <urbanc@in.tum.de>
parents: 638
diff changeset
    10
val B = Set("1", "2", "3", "4", "5", "6", "")
3031e3379ea3 updated
Christian Urban <urbanc@in.tum.de>
parents: 638
diff changeset
    11
concat(A, B).size                     // -> 28 
3031e3379ea3 updated
Christian Urban <urbanc@in.tum.de>
parents: 638
diff changeset
    12
pow(B, 3).size 
3031e3379ea3 updated
Christian Urban <urbanc@in.tum.de>
parents: 638
diff changeset
    13
3031e3379ea3 updated
Christian Urban <urbanc@in.tum.de>
parents: 638
diff changeset
    14
365
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
val A = Set("a", "b", "c", "d")
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
pow(A, 4).size                            // -> 256
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
val B = Set("a", "b", "c", "")
572
4a1739f256fd updated
Christian Urban <urbanc@in.tum.de>
parents: 564
diff changeset
    19
pow(B, 4)
365
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
pow(B, 4).size                            // -> 121
638
0367aa7c764b updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
    21
pow(B, 3).size 
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 397
diff changeset
    22
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 397
diff changeset
    23
397
cf3ca219c727 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 365
diff changeset
    24
val B2 = Set("a", "b", "c", "")
cf3ca219c727 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 365
diff changeset
    25
pow(B2, 3).size                           // -> 40
cf3ca219c727 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 365
diff changeset
    26
365
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
val C = Set("a", "b", "")
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
pow(C, 2)
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
pow(C, 2).size                            // -> 7
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
pow(C, 3)
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
pow(C, 3).size                            // -> 15
525
a2ee4b11c976 updated
cu
parents: 471
diff changeset
    33
a2ee4b11c976 updated
cu
parents: 471
diff changeset
    34
572
4a1739f256fd updated
Christian Urban <urbanc@in.tum.de>
parents: 564
diff changeset
    35
//val A = Set("a", "b", "c", "d")
4a1739f256fd updated
Christian Urban <urbanc@in.tum.de>
parents: 564
diff changeset
    36
//pow(A, 4).size   
525
a2ee4b11c976 updated
cu
parents: 471
diff changeset
    37
572
4a1739f256fd updated
Christian Urban <urbanc@in.tum.de>
parents: 564
diff changeset
    38
//val A = Set("a", "b", "c")
4a1739f256fd updated
Christian Urban <urbanc@in.tum.de>
parents: 564
diff changeset
    39
//pow(A, 5).size   
525
a2ee4b11c976 updated
cu
parents: 471
diff changeset
    40
572
4a1739f256fd updated
Christian Urban <urbanc@in.tum.de>
parents: 564
diff changeset
    41
//val A = Set("a", "b", "")
4a1739f256fd updated
Christian Urban <urbanc@in.tum.de>
parents: 564
diff changeset
    42
//pow(A, 5).size   
564
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 525
diff changeset
    43
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 525
diff changeset
    44
572
4a1739f256fd updated
Christian Urban <urbanc@in.tum.de>
parents: 564
diff changeset
    45
for (n <- (0 to 6).toList) 
4a1739f256fd updated
Christian Urban <urbanc@in.tum.de>
parents: 564
diff changeset
    46
  yield pow(B, n).size
577
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 572
diff changeset
    47
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 572
diff changeset
    48
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 572
diff changeset
    49
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 572
diff changeset
    50
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 572
diff changeset
    51
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 572
diff changeset
    52
val A = Set("a", "b", "c")
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 572
diff changeset
    53
pow(A, 3)
618
f4818c95a32e updated
Christian Urban <urbanc@in.tum.de>
parents: 577
diff changeset
    54
pow(A, 3).size
577
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 572
diff changeset
    55
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 572
diff changeset
    56
val B = Set("a", "b", "")
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 572
diff changeset
    57
pow(B, 4)
618
f4818c95a32e updated
Christian Urban <urbanc@in.tum.de>
parents: 577
diff changeset
    58
pow(B, 4).size