scala-problem26-30

来源:互联网 发布:java基础教程 免费下载 编辑:程序博客网 时间:2024/04/30 19:57

  • P26 Generate the combinations of K distinct objects chosen from the N elements of a list
    • 要求
    • 方案
  • P27 Group the elements of a set into disjoint subsets
    • 要求
    • 方案
  • P28 Sorting a list of lists according to length of sublists
    • 要求一
    • 方案
    • 要求二
    • 方案

* 声明*
该系列文章来自:http://aperiodic.net/phil/scala/s-99/
大部分内容和原文相同,加入了部分自己的代码。
如有侵权,请及时联系本人。本人将立即删除相关内容。

P26 (**) Generate the combinations of K distinct objects chosen from the N elements of a list.

要求

In how many ways can a committee of 3 be chosen from a group of 12 people? We all know that there are C(12,3) = 220 possibilities (C(N,K) denotes the well-known binomial coefficient). For pure mathematicians, this result may be great. But we want to really generate all the possibilities.

Example:

scala> combinations(3, List('a, 'b, 'c, 'd, 'e, 'f))res0: List[List[Symbol]] = List(List('a, 'b, 'c), List('a, 'b, 'd), List('a, 'b, 'e), ...

方案

  • (1) 递归
def flatMapSublists[A, B](ls: List[A])(f: (List[A]) => List[B]): List[B] = {    ls match {        case Nil                   => Nil        case sublist @ (_ :: tail) => f(sublist) ::: flatMapSublists(tail)(f)    }}def combinations[A](n: Int, ls: List[A]): List[List[A]] = {    if (n == 0)        return List(Nil)    else {        return flatMapSublists(ls)(sl => {            combinations(n - 1, sl.tail) map { sl.head :: _ }        });    }}

P27 (**) Group the elements of a set into disjoint subsets.

要求

a) In how many ways can a group of 9 people work in 3 disjoint subgroups of 2, 3 and 4 persons? Write a function that generates all the possibilities.
Example:

scala> group3(List(“Aldo”, “Beat”, “Carla”, “David”, “Evi”, “Flip”, “Gary”, “Hugo”, “Ida”))
res0: List[List[List[String]]] = List(List(List(Aldo, Beat), List(Carla, David, Evi), List(Flip, Gary, Hugo, Ida)), …
b) Generalize the above predicate in a way that we can specify a list of group sizes and the predicate will return a list of groups.

Example:

scala> group(List(2, 2, 5), List("Aldo", "Beat", "Carla", "David", "Evi", "Flip", "Gary", "Hugo", "Ida"))res0: List[List[List[String]]] = List(List(List(Aldo, Beat), List(Carla, David), List(Evi, Flip, Gary, Hugo, Ida)), ...

Note that we do not want permutations of the group members; i.e. ((Aldo, Beat), …) is the same solution as ((Beat, Aldo), …). However, we make a difference between ((Aldo, Beat), (Carla, David), …) and ((Carla, David), (Aldo, Beat), …).

You may find more about this combinatorial problem in a good book on discrete mathematics under the term “multinomial coefficients”.

方案

P28 (**) Sorting a list of lists according to length of sublists.

要求一

a) We suppose that a list contains elements that are lists themselves. The objective is to sort the elements of the list according to their length. E.g. short lists first, longer lists later, or vice versa.

Example:

scala> lsort(List(List('a, 'b, 'c), List('d, 'e), List('f, 'g, 'h), List('d, 'e), List('i, 'j, 'k, 'l), List('m, 'n), List('o)))res0: List[List[Symbol]] = List(List('o), List('d, 'e), List('d, 'e), List('m, 'n), List('a, 'b, 'c), List('f, 'g, 'h), List('i, 'j, 'k, 'l))

方案

  • (1) 使用List自带的sortWith方法按元素(这里元素类型是List)长度排序即可
def lsort[T](list: List[List[T]]): List[List[T]] =    list.sortWith((l, r) => l.length < r.length)

要求二

b) Again, we suppose that a list contains elements that are lists themselves. But this time the objective is to sort the elements according to their length frequency; i.e. in the default, sorting is done ascendingly, lists with rare lengths are placed, others with a more frequent length come later.

Example:

scala> lsortFreq(List(List('a, 'b, 'c), List('d, 'e), List('f, 'g, 'h), List('d, 'e), List('i, 'j, 'k, 'l), List('m, 'n), List('o)))res1: List[List[Symbol]] = List(List('i, 'j, 'k, 'l), List('o), List('a, 'b, 'c), List('f, 'g, 'h), List('d, 'e), List('d, 'e), List('m, 'n))

Note that in the above example, the first two lists in the result have length 4 and 1 and both lengths appear just once. The third and fourth lists have length 3 and there are two list of this length. Finally, the last three lists have length 2. This is the most frequent length.

方案

0 0
原创粉丝点击