Scala 无限循环?在什么情况下会发生这些循环?
我已经在coursera上提交了我的scala作业,但似乎有些测试失败了:Scala 无限循环?在什么情况下会发生这些循环?,scala,debugging,infinite-loop,Scala,Debugging,Infinite Loop,我已经在coursera上提交了我的scala作业,但似乎有些测试失败了: Your solution achieved a testing score of 70 out of 110. Below you can see a short feedback for every test that failed, indicating the reason for the test failure and how many points you lost for each individua
Your solution achieved a testing score of 70 out of 110.
Below you can see a short feedback for every test that failed, indicating the reason
for the test failure and how many points you lost for each individual test.
[Test Description] filter: tweet with 321 retweets
[Observed Error] Test timeout: aborted after 40 seconds; Check for infinite loops!
[exception was thrown] detailed error message in debug output section below
[Lost Points] 10
[Test Description] filter and union: tweets with 321 and 205 retweets
[Observed Error] Test timeout: aborted after 40 seconds; Check for infinite loops!
[exception was thrown] detailed error message in debug output section below
[Lost Points] 10
[Test Description] filter and trending: tweets with 321 and 205 retweets
[Observed Error] Test timeout: aborted after 40 seconds; Check for infinite loops!
[exception was thrown] detailed error message in debug output section below
[Lost Points] 10
[Test Description] trending: google and apple tweets
[Observed Error] Test timeout: aborted after 40 seconds; Check for infinite loops!
[exception was thrown] detailed error message in debug output section below
[Lost Points] 10
我的问题是,我无法想象我的功能会失败。通过作业提供的标准测试运行正常
以下是过滤器和联合功能:
def union(that: TweetSet): TweetSet = (left.union(right)).union(that).incl(elem)
val isEmpty = false
def filter(p: Tweet => Boolean): TweetSet = filterAcc(p,new Empty)
def filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSet = {
if(left.isEmpty && right.isEmpty) acc
else if(p(elem)){ left.filterAcc(p,acc.incl(elem)).union(right.filterAcc(p,acc.incl(elem)))}
else left.filterAcc(p,acc).union(right.filterAcc(p,acc))
}
如果有人需要它来更好地调试,下面是完整的代码:
package objsets
import common._
import TweetReader._
/**
* A class to represent tweets.
*/
class Tweet(val user: String, val text: String, val retweets: Int) {
override def toString: String =
"User: " + user + "\n" +
"Text: " + text + " [" + retweets + "]"
}
/**
* This represents a set of objects of type `Tweet` in the form of a binary search
* tree. Every branch in the tree has two children (two `TweetSet`s). There is an
* invariant which always holds: for every branch `b`, all elements in the left
* subtree are smaller than the tweet at `b`. The eleemnts in the right subtree are
* larger.
*
* Note that the above structure requires us to be able to compare two tweets (we
* need to be able to say which of two tweets is larger, or if they are equal). In
* this implementation, the equality / order of tweets is based on the tweet's text
* (see `def incl`). Hence, a `TweetSet` could not contain two tweets with the same
* text from different users.
*
*
* The advantage of representing sets as binary search trees is that the elements
* of the set can be found quickly. If you want to learn more you can take a look
* at the Wikipedia page [1], but this is not necessary in order to solve this
* assignment.
*
* [1] http://en.wikipedia.org/wiki/Binary_search_tree
*/
abstract class TweetSet {
def greatestCurrent(soFar: Tweet):Tweet
def iterateAndAddList(list: TweetList): TweetList
def isEmpty: Boolean
/**
* This method takes a predicate and returns a subset of all the elements
* in the original set for which the predicate is true.
*
* Question: Can we implment this method here, or should it remain abstract
* and be implemented in the subclasses?
*/
def filter(p: Tweet => Boolean): TweetSet
/**
* This is a helper method for `filter` that propagetes the accumulated tweets.
*/
def filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSet
/**
* Returns a new `TweetSet` that is the union of `TweetSet`s `this` and `that`.
*
* Question: Should we implment this method here, or should it remain abstract
* and be implemented in the subclasses?
*/
def union(that: TweetSet): TweetSet;
/**
* Returns the tweet from this set which has the greatest retweet count.
*
* Calling `mostRetweeted` on an empty set should throw an exception of
* type `java.util.NoSuchElementException`.
*
* Question: Should we implment this method here, or should it remain abstract
* and be implemented in the subclasses?
*/
def mostRetweeted: Tweet = ???
/**
* Returns a list containing all tweets of this set, sorted by retweet count
* in descending order. In other words, the head of the resulting list should
* have the highest retweet count.
*
* Hint: the method `remove` on TweetSet will be very useful.
* Question: Should we implment this method here, or should it remain abstract
* and be implemented in the subclasses?
*/
def descendingByRetweet: TweetList
/**
* The following methods are already implemented
*/
/**
* Returns a new `TweetSet` which contains all elements of this set, and the
* the new element `tweet` in case it does not already exist in this set.
*
* If `this.contains(tweet)`, the current set is returned.
*/
def incl(tweet: Tweet): TweetSet
/**
* Returns a new `TweetSet` which excludes `tweet`.
*/
def remove(tweet: Tweet): TweetSet
/**
* Tests if `tweet` exists in this `TweetSet`.
*/
def contains(tweet: Tweet): Boolean
/**
* This method takes a function and applies it to every element in the set.
*/
def foreach(f: Tweet => Unit): Unit
}
class Empty extends TweetSet {
def greatestCurrent(soFar: Tweet):Tweet = new Tweet("a","b",-1)
def iterateAndAddList(list: TweetList): TweetList = list
def descendingByRetweet() = Nil
def union(that: TweetSet): TweetSet = that
def isEmpty = true
def filter(p: Tweet=> Boolean): TweetSet = new Empty()
def filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSet = new Empty()
/**
* The following methods are already implemented
*/
def contains(tweet: Tweet): Boolean = false
def incl(tweet: Tweet): TweetSet = new NonEmpty(tweet, new Empty, new Empty)
def remove(tweet: Tweet): TweetSet = this
def foreach(f: Tweet => Unit): Unit = ()
}
class NonEmpty(elem: Tweet, left: TweetSet, right: TweetSet) extends TweetSet {
def descendingByRetweet = {
iterateAndAddList(Nil)
}
def iterateAndAddList(list: TweetList): TweetList = {
val current: Tweet = greatestCurrent(new Tweet("a","b",-1))
if(current.retweets != -1){
this.remove(current).iterateAndAddList(list.add(current))
}else list
}
def greatestCurrent(soFar: Tweet):Tweet = {
if(left.isEmpty && right.isEmpty) soFar
else{
if(elem.retweets < soFar.retweets) { mostPopular(left.greatestCurrent(soFar),right.greatestCurrent(soFar)) }
else if(elem.retweets > soFar.retweets){ mostPopular(left.greatestCurrent(elem),right.greatestCurrent(elem)) }
else { mostPopular(left.greatestCurrent(soFar),right.greatestCurrent(soFar)) }
}
}
def mostPopular(left: Tweet,right: Tweet):Tweet = {
if(left.retweets > right.retweets) left
else if(right.retweets > left.retweets) right
else left
}
def union(that: TweetSet): TweetSet = (left.union(right)).union(that).incl(elem)
val isEmpty = false
def filter(p: Tweet => Boolean): TweetSet = filterAcc(p,new Empty)
def filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSet = {
if(left.isEmpty && right.isEmpty) acc
else if(p(elem)){ left.filterAcc(p,acc.incl(elem)).union(right.filterAcc(p,acc.incl(elem)))}
else left.filterAcc(p,acc).union(right.filterAcc(p,acc))
}
/**
* The following methods are already implemented
*/
def contains(x: Tweet): Boolean =
if (x.text < elem.text) left.contains(x)
else if (elem.text < x.text) right.contains(x)
else true
def incl(x: Tweet): TweetSet = {
if (x.text < elem.text) new NonEmpty(elem, left.incl(x), right)
else if (elem.text < x.text) new NonEmpty(elem, left, right.incl(x))
else this
}
def remove(tw: Tweet): TweetSet =
if (tw.text < elem.text) new NonEmpty(elem, left.remove(tw), right)
else if (elem.text < tw.text) new NonEmpty(elem, left, right.remove(tw))
else left.union(right)
def foreach(f: Tweet => Unit): Unit = {
f(elem)
left.foreach(f)
right.foreach(f)
}
}
包对象集
进口普通_
导入TweetReader_
/**
*表示tweet的类。
*/
类Tweet(val user:String,val text:String,val retweets:Int){
重写def toString:字符串=
用户:“+User+”\n+
文本:“+Text+”[“+retweets+”]
}
/**
*这表示一组二进制搜索形式的“Tweet”类型的对象
*树。树上的每根树枝都有两个子树(两个TweetSet)。有一个
*始终保持不变:对于每个分支“b”,左侧的所有元素
*子树比“b”处的tweet小。右子树中的元素是
*更大的。
*
*注意,上面的结构要求我们能够比较两条tweet(我们
*需要能够说出两条推文中哪个推文更大,或者它们是否相等)。在里面
*在这个实现中,tweet的相等/顺序基于tweet的文本
*(请参见“def incl”)。因此,“TweetSet”不能包含两条具有相同名称的tweets
*来自不同用户的文本。
*
*
*将集合表示为二进制搜索树的优点是
*可以很快找到这一组的名称。如果你想了解更多,你可以看看
*在维基百科页面[1],但这不是解决这个问题的必要条件
*任务。
*
* [1] http://en.wikipedia.org/wiki/Binary_search_tree
*/
抽象类TweetSet{
def greatestCurrent(soFar:Tweet):Tweet
def iterateAndAddList(列表:TweetList):TweetList
def isEmpty:Boolean
/**
*此方法接受谓词并返回所有元素的子集
*在谓词为真的原始集合中。
*
*问题:我们可以在这里实现这个方法,还是应该保持抽象
*并在子类中实现?
*/
def过滤器(p:Tweet=>Boolean):TweetSet
/**
*这是'filter'的助手方法,用于传播累积的推文。
*/
def filterAcc(p:Tweet=>Boolean,acc:TweetSet):TweetSet
/**
*返回一个新的'TweetSet',它是'TweetSet'的'this'和'that'的并集。
*
*问题:我们应该在这里使用这种方法,还是应该保持抽象
*并在子类中实现?
*/
def union(即:TweetSet):TweetSet;
/**
*返回此集合中转发次数最大的tweet。
*
*在空集上调用“mostRetweeted”会引发以下异常:
*键入“java.util.NoSuchElementException”。
*
*问题:我们应该在这里使用这种方法,还是应该保持抽象
*并在子类中实现?
*/
def mostRetweeted:Tweet=???
/**
*返回包含此集合的所有推文的列表,按转发计数排序
*按降序排列。换句话说,结果列表的标题应该是
*具有最高的转发计数。
*
*提示:TweetSet上的'remove'方法将非常有用。
*问题:我们应该在这里使用这种方法,还是应该保持抽象
*并在子类中实现?
*/
def downingbyretweet:TweetList
/**
*以下方法已经实现
*/
/**
*返回一个新的'TweetSet',其中包含此集合的所有元素,以及
*新元素'tweet',以防它在此集合中不存在。
*
*如果`this.contains(tweet)`,则返回当前集。
*/
def incl(tweet:tweet):TweetSet
/**
*返回一个新的“TweetSet”,它不包括“tweet”。
*/
def移除(tweet:tweet):TweetSet
/**
*测试此“TweetSet”中是否存在“tweet”。
*/
def contains(tweet:tweet):布尔值
/**
*此方法接受一个函数并将其应用于集合中的每个元素。
*/
def foreach(f:Tweet=>单位):单位
}
类Empty扩展了TweetSet{
def greatestCurrent(soFar:Tweet):Tweet=新Tweet(“a”、“b”、-1)
def iterateAndAddList(列表:TweetList):TweetList=list
def下降ByRetweet()=零
def union(that:TweetSet):TweetSet=that
def isEmpty=true
def过滤器(p:Tweet=>Boolean):TweetSet=newempty()
def filterAcc(p:Tweet=>Boolean,acc:TweetSet):TweetSet=newempty()
/**
*以下方法已经实现
*/
def contains(tweet:tweet):布尔值=false
def incl(tweet:tweet):TweetSet=new NonEmpty(tweet,new Empty,new Empty)
def remove(tweet:tweet):TweetSet=this
def foreach(f:Tweet=>Unit):Unit=()
}
类NonEmpty(elem:Tweet,left:TweetSet,right:TweetSet)扩展了TweetSet{
def下降ByRetweet={
迭代数据列表(无)
}
def iterateAndAddList(列表:TweetList):TweetList={
当前值:Tweet=greatestCurrent(新Tweet(“a”、“b”、-1))
if(current.retweets!=-1){
this.remove(当前).iterateAndAddList(list.add(当前))
}其他列表
}
def greatestCurrent(soFar:Tweet):Tweet={
如果(left.isEmpty&&right.isEmpty)如此
否则{
if(elem.retweetssoFar.retweets){mostPopular(left.greatestCurrent(elem),right.greatestCurrent(elem))}
else{mostPopular(左.最大电流(soFar),右.最大电流(soFar))}
}
}
def mostPopular(左:推特,右:推特):推特={
如果(左.转发>右.转发)左
如果(right.retweets>left.retweets)right
剩下的
}
def union(that:TweetSet):TweetSet=(left.union(right)).union(that.incl(elem)
val isEmpty=false
def过滤器(p:Tweet=>Boolean):TweetSet=filterAcc(p,新为空)
def filterAcc(p:Tweet=>Boolean,acc:Twe