commit b6da8346a121218e5feac16aeb28e6d62076b54b Author: kamoshi <18511281+kamoshi@users.noreply.github.com> Date: Wed Nov 27 02:33:22 2019 +0100 Create 1.scala diff --git a/2018/Scala/1.scala b/2018/Scala/1.scala new file mode 100644 index 0000000..4e9e6b3 --- /dev/null +++ b/2018/Scala/1.scala @@ -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)