Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.29 KB | None | 0 0
  1. object TweetStorm {
  2.  
  3.   def main(args: Array[String]): Unit = {
  4.  
  5.     args match {
  6.       case Array(text) if text.length < 140 => println(text)
  7.       case Array(text) if text.length > 140 => stormfy(text).foreach(println)
  8.       case Array() => println("There is no arguments")
  9.     }
  10.   }
  11.  
  12.   def stormfy(text: String): List[String] = {
  13.     val orderPlaceHolderSize = 4
  14.     val tweetMaxSize = 140 - orderPlaceHolderSize
  15.  
  16.     val result = parseTweets(text.split(" ").toList, List(), "", tweetMaxSize)
  17.  
  18.     addOrder(result)
  19.   }
  20.  
  21.   def parseTweets(texts: List[String], tweetList: List[String], tweet: String, tweetMaxSize: Int): List[String] = {
  22.     texts match {
  23.       case word :: Nil => parseTweets(List(), tweetList :+ s"$tweet $word", "", tweetMaxSize)
  24.       case word :: rest =>
  25.         val tweetSize = tweet.length
  26.         val wordSize = word.length
  27.  
  28.         if (tweetSize + wordSize > tweetMaxSize) {
  29.           parseTweets(rest, tweetList :+ tweet, s" $word", tweetMaxSize)
  30.  
  31.         } else {
  32.           parseTweets(rest, tweetList, s"$tweet $word", tweetMaxSize)
  33.         }
  34.  
  35.       case Nil => tweetList
  36.  
  37.     }
  38.  
  39.   }
  40.  
  41.   def addOrder(tweets: List[String]): List[String] = {
  42.     tweets.zipWithIndex.map {
  43.       case (tweet, index) => s"${index + 1}/${tweets.length}$tweet"
  44.     }
  45.   }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement