为什么订单在发生时很重要? Coursera – 斯卡拉
发布时间:2020-12-16 18:07:07 所属栏目:安全 来源:网络整理
导读:我在Coursera上问了这个问题,但没有人回复,所以我来到这里. 这是关于 Scala中功能编程原理课程的最后一个任务(Anagrams). 如果函数减法返回无序的次数,AnagramsSuite中的最后一次测试将失败. 此外,还需要函数wordOccurrences返回已排序的Occurrences. 那么,
我在Coursera上问了这个问题,但没有人回复,所以我来到这里.
这是关于 Scala中功能编程原理课程的最后一个任务(Anagrams). 如果函数减法返回无序的次数,AnagramsSuite中的最后一次测试将失败. 此外,还需要函数wordOccurrences返回已排序的Occurrences. 那么,为什么发生的顺序很重要? // sentenceAnagrams passes the Test def subtract(x: Occurrences,y: Occurrences): Occurrences = ((y foldLeft x.toMap)((result,current) => { result.updated(current._1,result(current._1)-current._2) }) filter (_._2>0)).toList.sortWith(_._1<_._1) // Without sortWith,the sentenceAnagrams will fail to get the right answer def subtract(x: Occurrences,result(current._1)-current._2) }) filter (_._2>0)).toList 解决方法
因为它是
part of the definition:
/** `Occurrences` is a `List` of pairs of characters and positive integers saying * how often the character appears. * This list is sorted alphabetically w.r.t. to the character in each pair. * All characters in the occurrence list are lowercase. * * Any list of pairs of lowercase characters and their frequency which is not sorted * is **not** an occurrence list. * * Note: If the frequency of some character is zero,then that character should not be * in the list. */ type Occurrences = List[(Char,Int)] List类型是有序的.如果相反他们使用Map(就像它本来的那样),那么这不会是一个问题. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 在Bash中的运算符“=”和“==”有什么区别?
- nvd3 Angular2动态更改图表选项
- 【转贴】简述WebS简述WebService与.NET Remoting的区别及适
- angularjs – UI Bootstrap下拉指令导致multidir问题
- Angular 2 * ngFor仅支持在内联模板中一次添加10个元素
- angular – Parser Error:得到插值({{}}),其中表达式是预期
- scala – 包含NaN的收藏的最小/最大(处理订购中的不兼容性)
- bash – 根据列中出现的数字将CSV文件从最大到最小排序
- [WebService Test]: 4. WSDL
- 在pcf中部署angular2应用程序