progs/pow.scala
changeset 965 94f5cce73a4f
parent 882 5fcad75ade92
equal deleted inserted replaced
964:da1f8c033b8e 965:94f5cce73a4f
       
     1 
     1 def concat(A: Set[String], B: Set[String]) : Set[String] =
     2 def concat(A: Set[String], B: Set[String]) : Set[String] =
     2   for (x <- A ; y <- B) yield x ++ y
     3   for (x <- A ; y <- B) yield x ++ y
     3 
     4 
     4 def pow(A: Set[String], n: Int) : Set[String] = n match {
     5 def pow(A: Set[String], n: Int) : Set[String] = n match {
     5   case 0 => Set("")
     6   case 0 => Set("")
     6   case n => concat(A, pow(A, n - 1))
     7   case n => concat(A, pow(A, n - 1))
     7 }
     8 }
     8 
     9 
     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 
       
    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
       
    20 powT(A, 4).size
       
    21 powT(B, 4).size
       
    22 
       
    23 
       
    24 val C = Set("a", "b")
       
    25 
       
    26 pow(C, 100).size
       
    27 powT(C, 100000)  
       
    28 
       
    29 val B = Set("aaa", "aaaa")
       
    30 concat(A, B).size                     // -> 3 
       
    31 
       
    32 
       
    33 
       
    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 
    10 
    39 
    11 
    40 val A = Set("a", "b", "c", "d")
    12 val A = Set("a", "b", "c", "d")
    41 pow(A, 4).size                            // -> 256
       
    42 
       
    43 val B = Set("a", "b", "c", "")
    13 val B = Set("a", "b", "c", "")
    44 pow(B, 4)
    14 pow(A, 4).size
    45 pow(B, 4).size                            // -> 121
    15 pow(B, 4).size
    46 pow(B, 3).size 
       
    47 
    16 
    48 
    17 
    49 val B2 = Set("a", "b", "c", "")
       
    50 pow(B2, 3).size                           // -> 40
       
    51 
       
    52 val C = Set("a", "b", "")
       
    53 pow(C, 2)
       
    54 pow(C, 2).size                            // -> 7
       
    55 
       
    56 pow(C, 3)
       
    57 pow(C, 3).size                            // -> 15
       
    58 
       
    59 
       
    60 //val A = Set("a", "b", "c", "d")
       
    61 //pow(A, 4).size   
       
    62 
       
    63 //val A = Set("a", "b", "c")
       
    64 //pow(A, 5).size   
       
    65 
       
    66 //val A = Set("a", "b", "")
       
    67 //pow(A, 5).size   
       
    68 
       
    69 
       
    70 for (n <- (0 to 6).toList) 
       
    71   yield pow(B, n).size
       
    72 
       
    73 
       
    74 
       
    75 
       
    76 
       
    77 val A = Set("a", "b", "c")
       
    78 pow(A, 3)
       
    79 pow(A, 3).size
       
    80 
       
    81 val B = Set("a", "b", "")
       
    82 pow(B, 4)
       
    83 pow(B, 4).size
       
    84 
       
    85 val C = Set("a", "")
       
    86 pow(C, 4)
       
    87 pow(C, 4).size
       
    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 */