Create 1.scala

This commit is contained in:
kamoshi 2019-11-27 02:33:22 +01:00 committed by GitHub
commit b6da8346a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

31
2018/Scala/1.scala Normal file
View file

@ -0,0 +1,31 @@
import scala.collection.mutable
import scala.io.Source
import scala.collection.immutable.LazyList.#::
val lines = Source.fromFile(getClass.getResource("/file.txt").getFile).getLines().toList
// Solution 1
val result1 = lines.foldLeft (0) { (acc, i) => acc + Integer.parseInt(i) }
println(result1)
// Solution 2
val hash = new mutable.HashSet[Int]()
def toLazyLoop[A](list: List[A]): LazyList[A] = {
def toLazy[A](list: List[A]): LazyList[A] = {
list match {
case List() => LazyList()
case h::t => h #:: toLazy(t)
}
}
toLazy(list) #::: toLazyLoop(list)
}
def findFirstRepeat[A](lazylist:LazyList[String], acc:Int, hash:mutable.HashSet[Int]):Int =
if (!hash.add(acc)) acc
else lazylist match {
case hd #:: tl => findFirstRepeat(tl, (acc + hd.toInt), hash)
}
val result2 = findFirstRepeat(toLazyLoop(lines), 0, hash)
println("First repeat frequency: " + result2)