Skip to content

Instantly share code, notes, and snippets.

@Haaroon
Created December 29, 2020 20:53
Show Gist options
  • Save Haaroon/1558a5bb3e05145fce811288cd5902cb to your computer and use it in GitHub Desktop.
Save Haaroon/1558a5bb3e05145fce811288cd5902cb to your computer and use it in GitHub Desktop.
// ex 2
val in = new java.util.Scanner(new java.io.File("example.txt"))
val words = scala.collection.mutable.Map[String, Int]()
while (in.hasNext()) {
val word = in.next().replaceAll("\\W", "")
val c = words.getOrElse(word, 0) + 1
words(word) = c
}
for((k,v) <- words) println(k +" : "+ v)
//ex 4
change line 3 with SortedMap
// ex 6
val dayMap = Map(
"Monday" -> java.util.Calendar.MONDAY,
"Tuesday" -> java.util.Calendar.TUESDAY,
"Wednesday" -> java.util.Calendar.WEDNESDAY,
)
for((k,v) <- dayMap) println(k+" : "+v)
//ex 8
def minmax(a: Array[Int]) = {
(a.min, a.max)
}
def lteqgt(values: Array[Int], v: Int) = {
(values.filter(_<v).length,
values.filter(_==v).length,
values.filter(_>v).length)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment