Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 创建所有子集_Scala_Set_Subset - Fatal编程技术网

Scala 创建所有子集

Scala 创建所有子集,scala,set,subset,Scala,Set,Subset,嗯。我知道。这是奥德斯基课程的练习。但这件事我已经坚持了好几天了 定义用于创建输入集所有子集的方法: def combination(occ: List[(Char,Int)]) : List[List[(Char,Int)]] 示例:对于输入列表(('a',2),('b',2))获取输出: List( List(), List(('a', 1)), List(('a', 2)), List(('b', 1)), List(('a', 1), ('b', 1)), Li

嗯。我知道。这是奥德斯基课程的练习。但这件事我已经坚持了好几天了

定义用于创建输入集所有子集的方法:

def combination(occ: List[(Char,Int)]) : List[List[(Char,Int)]]
示例:对于输入
列表(('a',2),('b',2))
获取输出:

List(
  List(),
  List(('a', 1)),
  List(('a', 2)),
  List(('b', 1)),
  List(('a', 1), ('b', 1)),
  List(('a', 2), ('b', 1)),
  List(('b', 2)),
  List(('a', 1), ('b', 2)),
  List(('a', 2), ('b', 2))
)
太好了。目前我得到了两个不同的东西:所有对(1a)和单个元素的列表(1b)

1a它可以工作。它给了我所有的夫妇(通常是元组)

输出<代码>列表(列表((a,2)),列表((a,1)),列表((b,2)),列表((b,1))

现在我完全被困在如何将两者结合起来。
您能帮我了解一下如何实现这一点吗?

不确定效率,但您可以看看这个。这是一种硬币兑换问题,即给定总金额和所有可能的硬币,有多少种方式可以组成兑换。其中一个想法(自上而下的方法)是提出一个递归函数,该函数将区分您是否将获得特定种类的硬币,这就是内部组合函数在这里所做的:

def combinations(occurrences: List[(Char,Int)]) : List[List[(Char,Int)]] = {
    // total number of coins
    val tot = occurrences.map(_._2).sum

    // add a pair to an existing combination
    def add(counter: List[(Char, Int)], element: (Char, Int)) = {
      val countMap = counter.toMap
      (countMap + (element._1 -> (countMap.getOrElse(element._1, 0) + element._2))).toList
    }

    // a recursive function to calculate all the combinations
    def combinations(occurs: List[(Char,Int)], n: Int): List[List[(Char,Int)]] = {
      if(n == 0) List(List())
      else if(occurs.isEmpty) List()
      else {
        val firstElement = if(occurs.head._2 == 1) List() else List((occurs.head._1, 1))
        // all the combinations if you take the first kind of coin
        val headComb = combinations(firstElement ++ occurs.tail, n - 1)

        // all the combinations if you don't take the first kind of coin
        val tailComb = combinations(occurs.tail, n)    

        // add the first coin pair to head combination and concatenate it with tail combination
        headComb.map(add(_, (occurs.head._1, 1))) ++ tailComb    
      }
    }

    // calculate the combinations for each amount separately
    (0 to tot).toList.flatMap(combinations(occurrences, _))
}


combinations(List())
// res49: List[List[(Char, Int)]] = List(List())

combinations(List(('a', 1)))
// res50: List[List[(Char, Int)]] = List(List(), List((a,1)))


combinations(List(('a', 1), ('b', 2)))
// res51: List[List[(Char, Int)]] = List(List(), List((a,1)), List((b,1)), List((b,1), (a,1)), List((b,2)), List((
// b,2), (a,1)))

combinations(List(('a', 2), ('b', 2)))
// res52: List[List[(Char, Int)]] = List(List(), List((a,1)), List((b,1)), List((a,2)), List((b,1), (a,1)), List((
// b,2)), List((b,1), (a,2)), List((b,2), (a,1)), List((b,2), (a,2)))

不确定效率,但你可以看看这个。这是一种硬币兑换问题,即给定总金额和所有可能的硬币,有多少种方式可以组成兑换。其中一个想法(自上而下的方法)是提出一个递归函数,该函数将区分您是否将获得特定种类的硬币,这就是内部组合函数在这里所做的:

def combinations(occurrences: List[(Char,Int)]) : List[List[(Char,Int)]] = {
    // total number of coins
    val tot = occurrences.map(_._2).sum

    // add a pair to an existing combination
    def add(counter: List[(Char, Int)], element: (Char, Int)) = {
      val countMap = counter.toMap
      (countMap + (element._1 -> (countMap.getOrElse(element._1, 0) + element._2))).toList
    }

    // a recursive function to calculate all the combinations
    def combinations(occurs: List[(Char,Int)], n: Int): List[List[(Char,Int)]] = {
      if(n == 0) List(List())
      else if(occurs.isEmpty) List()
      else {
        val firstElement = if(occurs.head._2 == 1) List() else List((occurs.head._1, 1))
        // all the combinations if you take the first kind of coin
        val headComb = combinations(firstElement ++ occurs.tail, n - 1)

        // all the combinations if you don't take the first kind of coin
        val tailComb = combinations(occurs.tail, n)    

        // add the first coin pair to head combination and concatenate it with tail combination
        headComb.map(add(_, (occurs.head._1, 1))) ++ tailComb    
      }
    }

    // calculate the combinations for each amount separately
    (0 to tot).toList.flatMap(combinations(occurrences, _))
}


combinations(List())
// res49: List[List[(Char, Int)]] = List(List())

combinations(List(('a', 1)))
// res50: List[List[(Char, Int)]] = List(List(), List((a,1)))


combinations(List(('a', 1), ('b', 2)))
// res51: List[List[(Char, Int)]] = List(List(), List((a,1)), List((b,1)), List((b,1), (a,1)), List((b,2)), List((
// b,2), (a,1)))

combinations(List(('a', 2), ('b', 2)))
// res52: List[List[(Char, Int)]] = List(List(), List((a,1)), List((b,1)), List((a,2)), List((b,1), (a,1)), List((
// b,2)), List((b,1), (a,2)), List((b,2), (a,1)), List((b,2), (a,2)))

我可以给你一个提示,但我不能给你一个解决方案,因为这将是违反coursera荣誉代码的行为

<>注意,在你的推理中,将代码< >(0)作为空集合是一件好事,所有的集合都包含空集合!你可以想出一个办法把它们过滤掉

List(('a', 2), ('b', 2)) <=> List(('a', 2), ('b', 2), ('a', 0), ('b', 0))
List(('a',2),('b',2))List(('a',2),('b',2),('a',0),('b',0))

我希望它能帮助你!祝你好运

我可以给你一个提示,但我不能给你一个解决方案,因为这将是违反coursera荣誉代码的行为

<>注意,在你的推理中,将代码< >(0)作为空集合是一件好事,所有的集合都包含空集合!你可以想出一个办法把它们过滤掉

List(('a', 2), ('b', 2)) <=> List(('a', 2), ('b', 2), ('a', 0), ('b', 0))
List(('a',2),('b',2))List(('a',2),('b',2),('a',0),('b',0))

我希望它能帮助你!祝你好运

Umh using Range(entry.\u 2到1 by-1)会自动筛选出是否有(\u,0)因为生成,映射到无并退出是,但您不想跳过带有0的集合,您想在没有itUmh的情况下添加它们。using Range(entry.\u 2到1 by-1)会自动筛选出是否有(\u,0)因为生成,映射到None and Exits是的,但你不想跳过带有0的集合,你想添加它们而不使用它好吧,我很高兴它能工作,但我不敢相信要实现这样的东西会如此困难:(无论如何,我真的很感谢你,我将自己尝试一种自上而下的不同实现方式好吧,我很高兴它能工作,但我不敢相信实现这样的东西会这么难:(无论如何,我真的很感谢你们,我要用一种自上而下的不同的实现方法来尝试,我很不愿意告诉你们这一点,因为在我上那门课的时候读到这样的东西会让我感到很不舒服,但这可以在一个语句中完成,即不
{}
=
之后需要。这是一条相当长的语句(我将其拆分为3行),涉及递归、标准库中的一些项以及对文件中另一个方法的调用(提示:这是该文件中的下一个方法)。我希望这不是有害的而是有益的。祝你好运。嗯,对于标准库的方法有什么建议吗?我使用了
flatMap()
,并使用
distinct
删除了冗余(不要告诉任何人我这么说)我有点不想告诉你这一点,因为当我上那门课的时候,读像这样的东西会把我逼疯的,但这可以在一个语句中完成,也就是说,
=
后面不需要
{}
。这是一个相当长的语句(我把它分成三行)涉及递归、标准库中的一些项,以及对文件中另一个方法的调用(提示:这是该文件中的下一个方法)。我希望这不是有害的,而是有益的。祝你好运。嗯,仅对标准库的方法有什么建议吗?我使用了
flatMap()
并使用
distinct
删除冗余(不要告诉任何人我这么说)