-
-
Save fancellu/8931584 to your computer and use it in GitHub Desktop.
Scala coding test (note error in the Spec I was sent, I'll let you work out what it was)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Exercise | |
Implement a console-based social networking application (similar to Twitter) satisfying the scenarios below. | |
Scenarios | |
Posting: Alice can publish messages to a personal timeline | |
> Alice -> I love the weather today | |
> Bob -> Oh, we lost! | |
> Bob -> at least it's sunny | |
Reading: Bob can view Alice’s timeline | |
> Alice | |
I love the weather today (5 minutes ago) | |
> Bob | |
Oh, we lost! (1 minute ago) | |
at least it's sunny (2 minutes ago) | |
Following: Charlie can subscribe to Alice’s and Bob’s timelines, and view an aggregated list of all subscriptions | |
> Charlie -> I'm in New York today! Anyone wants to have a coffee? | |
> Charlie follows Alice | |
> Charlie wall | |
Charlie - I'm in New York today! Anyone wants to have a coffee? (2 seconds ago) | |
Alice - I love the weather today (5 minutes ago) | |
> Charlie follows Bob | |
> Charlie wall | |
Charlie - I'm in New York today! Anyone wants to have a coffee? (15 seconds ago) | |
Bob - Oh, we lost! (1 minute ago) | |
Bob - at least it's sunny (2 minutes ago) | |
Alice - I love the weather today (5 minutes ago) | |
General requirements | |
- Application must use the console for input and output; | |
- User submits commands to the application: | |
posting: <user name> -> <message> | |
reading: <user name> | |
following: <user name> follows <another user> | |
wall: <user name> wall | |
- Don't worry about handling any exceptions or invalid commands. Assume that the user will always type the correct commands. Just focus on the sunny day scenarios. | |
- Use whatever language and frameworks you want. (provide instructions how to run the application) | |
IMPORTANT: Implement the requirements focusing on writing the best code you can produce. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package twitle | |
object Twitle extends App { | |
import scala.collection.mutable.ListBuffer | |
import scala.collection.mutable.Set | |
implicit def toPerson(name:String)=Person(name) | |
class Person private(val name:String, val timeline:ListBuffer[Message]=ListBuffer.empty, | |
val following:Set[Person]=Set.empty){ | |
def post(text:String):Unit=timeline += Message(text,name) | |
def follow(otherPerson:Person):Unit=following+= otherPerson | |
def read:Unit=timeline.foreach(println) | |
def wall:Unit={ | |
val people=following + this | |
val messages=people.toList.flatMap(_.timeline).sorted | |
messages.foreach(m=>println(m.sender.name+" - "+m.toString)) | |
} | |
} | |
object Person { | |
val persons = scala.collection.mutable.Map[String,Person]() | |
def apply(name: String): Person= persons.getOrElseUpdate(name, new Person(name)) | |
} | |
case class Message(text:String, sender:Person,time:java.util.Date=new java.util.Date) extends Ordered[Message]{ | |
def compare(that: Message): Int = -(time compareTo that.time) | |
override def toString():String=s"""$text ($timeSpan ago)""" | |
def timeSpan:String={ | |
val seconds=(System.currentTimeMillis()-time.getTime())/1000 | |
if (seconds<60) s"$seconds seconds" else s"${seconds/60} minutes" | |
} | |
} | |
println(""" | |
Welcome to Twitle: | |
To post: | |
username -> message | |
To read someone's messages: | |
username | |
To make someone follow someone else: | |
username follows other-username | |
To read someone's wall: | |
username wall | |
To quit: | |
quit | |
""") | |
val post = """\s?(\S*) -> (.*)""".r | |
val username = """\s?(\S*)\s?""".r | |
val follows = """\s?(\S*) follows (\S*)\s?""".r | |
val wall = """\s?(\S*) wall""".r | |
while (true){ | |
print("> ") | |
val input=readLine | |
input match{ | |
case "quit" =>System.exit(0) | |
case post(person,text)=> person.post(text) | |
case follows(person,otherPerson)=>person.follow(otherPerson) | |
case wall(person)=>person.wall | |
case username(person)=>person.read | |
case _ =>println("Sorry, did not understand") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment