Advertisement
Guest User

Untitled

a guest
Jun 25th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 6.89 KB | None | 0 0
  1. package objsets
  2.  
  3. import TweetReader._
  4.  
  5. /**
  6.   * A class to represent tweets.
  7.   */
  8. class Tweet(val user: String, val text: String, val retweets: Int) {
  9.   override def toString: String =
  10.     "User: " + user + "\n" +
  11.       "Text: " + text + " [" + retweets + "]"
  12. }
  13.  
  14. /**
  15.   * This represents a set of objects of type `Tweet` in the form of a binary search
  16.   * tree. Every branch in the tree has two children (two `TweetSet`s). There is an
  17.   * invariant which always holds: for every branch `b`, all elements in the left
  18.   * subtree are smaller than the tweet at `b`. The elements in the right subtree are
  19.   * larger.
  20.   *
  21.   * Note that the above structure requires us to be able to compare two tweets (we
  22.   * need to be able to say which of two tweets is larger, or if they are equal). In
  23.   * this implementation, the equality / order of tweets is based on the tweet's text
  24.   * (see `def incl`). Hence, a `TweetSet` could not contain two tweets with the same
  25.   * text from different users.
  26.   *
  27.   *
  28.   * The advantage of representing sets as binary search trees is that the elements
  29.   * of the set can be found quickly. If you want to learn more you can take a look
  30.   * at the Wikipedia page [1], but this is not necessary in order to solve this
  31.   * assignment.
  32.   *
  33.   * [1] http://en.wikipedia.org/wiki/Binary_search_tree
  34.   */
  35. abstract class TweetSet {
  36.  
  37.   /**
  38.     * This method takes a predicate and returns a subset of all the elements
  39.     * in the original set for which the predicate is true.
  40.     *
  41.     * Question: Can we implement this method here, or should it remain abstract
  42.     * and be implemented in the subclasses?
  43.     */
  44.   def filter(p: Tweet => Boolean): TweetSet = filterAcc(p, new Empty)
  45.  
  46.   /**
  47.     * This is a helper method for `filter` that propagates the accumulated tweets.
  48.     */
  49.   def filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSet
  50.  
  51.   /**
  52.     * Returns a new `TweetSet` that is the union of `TweetSet`s `this` and `that`.
  53.     *
  54.     * Question: Should we implement this method here, or should it remain abstract
  55.     * and be implemented in the subclasses?
  56.     */
  57.   def union(that: TweetSet): TweetSet
  58.  
  59.   /**
  60.     * Returns the tweet from this set which has the greatest retweet count.
  61.     *
  62.     * Calling `mostRetweeted` on an empty set should throw an exception of
  63.     * type `java.util.NoSuchElementException`.
  64.     *
  65.     * Question: Should we implement this method here, or should it remain abstract
  66.     * and be implemented in the subclasses?
  67.     */
  68.   def mostRetweeted: Tweet = mostRetweetedHelper
  69.  
  70.   def mostRetweetedHelper: Tweet
  71.  
  72.   /**
  73.     * Returns a list containing all tweets of this set, sorted by retweet count
  74.     * in descending order. In other words, the head of the resulting list should
  75.     * have the highest retweet count.
  76.     *
  77.     * Hint: the method `remove` on TweetSet will be very useful.
  78.     * Question: Should we implment this method here, or should it remain abstract
  79.     * and be implemented in the subclasses?
  80.     */
  81.   def descendingByRetweet: TweetList = ???
  82.  
  83.   /**
  84.     * The following methods are already implemented
  85.     */
  86.  
  87.   /**
  88.     * Returns a new `TweetSet` which contains all elements of this set, and the
  89.     * the new element `tweet` in case it does not already exist in this set.
  90.     *
  91.     * If `this.contains(tweet)`, the current set is returned.
  92.     */
  93.   def incl(tweet: Tweet): TweetSet
  94.  
  95.   /**
  96.     * Returns a new `TweetSet` which excludes `tweet`.
  97.     */
  98.   def remove(tweet: Tweet): TweetSet
  99.  
  100.   /**
  101.     * Tests if `tweet` exists in this `TweetSet`.
  102.     */
  103.   def contains(tweet: Tweet): Boolean
  104.  
  105.   /**
  106.     * This method takes a function and applies it to every element in the set.
  107.     */
  108.   def foreach(f: Tweet => Unit): Unit
  109. }
  110.  
  111. class Empty extends TweetSet {
  112.   def filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSet = acc
  113.  
  114.   def union(that: TweetSet): TweetSet = that
  115.  
  116.   override def mostRetweeted: Tweet = throw new NoSuchElementException("mostRetweeted on Empty(TweetSet)")
  117.  
  118.   def mostRetweetedHelper: Tweet = new Tweet("", "", 0)
  119.  
  120.   /**
  121.     * The following methods are already implemented
  122.     */
  123.  
  124.   def contains(tweet: Tweet): Boolean = false
  125.  
  126.   def incl(tweet: Tweet): TweetSet = new NonEmpty(tweet, new Empty, new Empty)
  127.  
  128.   def remove(tweet: Tweet): TweetSet = this
  129.  
  130.   def foreach(f: Tweet => Unit): Unit = ()
  131. }
  132.  
  133. class NonEmpty(elem: Tweet, left: TweetSet, right: TweetSet) extends TweetSet {
  134.  
  135.   def filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSet = {
  136.     val filteredAcc: TweetSet = if (p(elem)) acc.incl(elem) else acc
  137.     left.filterAcc(p, filteredAcc)
  138.     right.filterAcc(p, filteredAcc)
  139.   }
  140.  
  141.   def union(that: TweetSet): TweetSet = new NonEmpty(elem, that.union(left), that.union(right))
  142.  
  143.   def mostRetweetedHelper: Tweet = {
  144.     val leftTw: Tweet = left.mostRetweetedHelper
  145.     val rigthTw: Tweet = right.mostRetweetedHelper
  146.     val leftRetweets: Int = leftTw.retweets
  147.     val rightRetweets: Int = rigthTw.retweets
  148.  
  149.     if (leftRetweets > elem.retweets && leftTw.retweets > rightRetweets) leftTw
  150.     else if (rightRetweets > elem.retweets && rigthTw.retweets > leftRetweets) rigthTw
  151.     else elem
  152.   }
  153.  
  154.   /**
  155.     * The following methods are already implemented
  156.     */
  157.  
  158.   def contains(x: Tweet): Boolean =
  159.     if (x.text < elem.text) left.contains(x)
  160.     else if (elem.text < x.text) right.contains(x)
  161.     else true
  162.  
  163.   def incl(x: Tweet): TweetSet = {
  164.     if (x.text < elem.text) new NonEmpty(elem, left.incl(x), right)
  165.     else if (elem.text < x.text) new NonEmpty(elem, left, right.incl(x))
  166.     else this
  167.   }
  168.  
  169.   def remove(tw: Tweet): TweetSet =
  170.     if (tw.text < elem.text) new NonEmpty(elem, left.remove(tw), right)
  171.     else if (elem.text < tw.text) new NonEmpty(elem, left, right.remove(tw))
  172.     else left.union(right)
  173.  
  174.   def foreach(f: Tweet => Unit): Unit = {
  175.     f(elem)
  176.     left.foreach(f)
  177.     right.foreach(f)
  178.   }
  179. }
  180.  
  181. trait TweetList {
  182.   def head: Tweet
  183.  
  184.   def tail: TweetList
  185.  
  186.   def isEmpty: Boolean
  187.  
  188.   def foreach(f: Tweet => Unit): Unit =
  189.     if (!isEmpty) {
  190.       f(head)
  191.       tail.foreach(f)
  192.     }
  193. }
  194.  
  195. object Nil extends TweetList {
  196.   def head = throw new java.util.NoSuchElementException("head of EmptyList")
  197.  
  198.   def tail = throw new java.util.NoSuchElementException("tail of EmptyList")
  199.  
  200.   def isEmpty = true
  201. }
  202.  
  203. class Cons(val head: Tweet, val tail: TweetList) extends TweetList {
  204.   def isEmpty = false
  205. }
  206.  
  207.  
  208. object GoogleVsApple {
  209.   val google = List("android", "Android", "galaxy", "Galaxy", "nexus", "Nexus")
  210.   val apple = List("ios", "iOS", "iphone", "iPhone", "ipad", "iPad")
  211.  
  212.   lazy val googleTweets: TweetSet = ???
  213.   lazy val appleTweets: TweetSet = ???
  214.  
  215.   /**
  216.     * A list of all tweets mentioning a keyword from either apple or google,
  217.     * sorted by the number of retweets.
  218.     */
  219.   lazy val trending: TweetList = ???
  220. }
  221.  
  222. object Main extends App {
  223.   // Print the trending tweets
  224.   GoogleVsApple.trending foreach println
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement