summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2013-04-14 18:12:34 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2016-11-10 18:03:23 +0100
commit022c591d1ff008041f712e65149670f568404c1a (patch)
treeab42f7087900b55640a56524d1bd07f3f7bbb6b5
parentfe6c2fe7035df29901899d7acbf1425ac350f4a2 (diff)
downloadcoursera-022c591d1ff008041f712e65149670f568404c1a.zip
coursera-022c591d1ff008041f712e65149670f568404c1a.tar.gz
Scala : objsets: implements TweetSet
-rw-r--r--Scala/objsets/src/main/scala/objsets/TweetSet.scala39
1 files changed, 33 insertions, 6 deletions
diff --git a/Scala/objsets/src/main/scala/objsets/TweetSet.scala b/Scala/objsets/src/main/scala/objsets/TweetSet.scala
index 9200c50..f558e48 100644
--- a/Scala/objsets/src/main/scala/objsets/TweetSet.scala
+++ b/Scala/objsets/src/main/scala/objsets/TweetSet.scala
@@ -42,7 +42,7 @@ abstract class TweetSet {
* Question: Can we implment this method here, or should it remain abstract
* and be implemented in the subclasses?
*/
- def filter(p: Tweet => Boolean): TweetSet = ???
+ def filter(p: Tweet => Boolean): TweetSet = filterAcc(p, new Empty)
/**
* This is a helper method for `filter` that propagetes the accumulated tweets.
@@ -55,7 +55,7 @@ abstract class TweetSet {
* Question: Should we implment this method here, or should it remain abstract
* and be implemented in the subclasses?
*/
- def union(that: TweetSet): TweetSet = ???
+ def union(that: TweetSet): TweetSet
/**
* Returns the tweet from this set which has the greatest retweet count.
@@ -66,7 +66,12 @@ abstract class TweetSet {
* Question: Should we implment this method here, or should it remain abstract
* and be implemented in the subclasses?
*/
- def mostRetweeted: Tweet = ???
+ def mostRetweeted: Tweet
+
+ /**
+ * This is a helper method for `mostRetweeted` search the set transporting the challenger
+ */
+ def mostRetweetedElect(tweet: Tweet): Tweet
/**
* Returns a list containing all tweets of this set, sorted by retweet count
@@ -77,7 +82,7 @@ abstract class TweetSet {
* Question: Should we implment this method here, or should it remain abstract
* and be implemented in the subclasses?
*/
- def descendingByRetweet: TweetList = ???
+ def descendingByRetweet: TweetList
/**
@@ -110,7 +115,15 @@ abstract class TweetSet {
class Empty extends TweetSet {
- def filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSet = ???
+ def filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSet = acc
+
+ def union(that: TweetSet): TweetSet = that
+
+ def mostRetweeted: Tweet = throw new NoSuchElementException
+
+ def mostRetweetedElect(challenger: Tweet): Tweet = challenger
+
+ def descendingByRetweet: TweetList = Nil
/**
@@ -128,7 +141,21 @@ class Empty extends TweetSet {
class NonEmpty(elem: Tweet, left: TweetSet, right: TweetSet) extends TweetSet {
- def filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSet = ???
+ def filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSet =
+ if (p(elem)) right filterAcc(p, (left filterAcc(p, acc) incl elem))
+ else right filterAcc(p, left filterAcc(p, acc))
+
+ def union(that: TweetSet): TweetSet = (right union (left union that)) incl elem
+
+ def mostRetweeted: Tweet = mostRetweetedElect(elem)
+
+ def mostRetweetedElect(challenger: Tweet): Tweet =
+ right mostRetweetedElect (left mostRetweetedElect (if (elem.retweets > challenger.retweets) elem else challenger))
+
+ def descendingByRetweet: TweetList = {
+ val most = mostRetweetedElect(elem)
+ new Cons(most, remove(most).descendingByRetweet)
+ }
/**