Skip to content

Instantly share code, notes, and snippets.

View OlegEfrem's full-sized avatar

Oleg Efrem OlegEfrem

View GitHub Profile
@OlegEfrem
OlegEfrem / SecondMinimum.scala
Last active November 21, 2019 11:46
Find second minimum element from a list of integers in scala, complexity should be n (hence list can't be sorted).
package com.recbuc.receipts
import scala.annotation.tailrec
object SecondMinimum {
def main(args: Array[String]): Unit = {
val regular = List(5, 3, 2, 5, 1, 3, 1, -1, 2, -1, 5) // expected -1
val sorted = regular.sorted // expected -1
val distinct = sorted.distinct //expected 1
val reverse = distinct.reverse //expected 1
@OlegEfrem
OlegEfrem / RandomQuote
Created February 15, 2015 18:28
Random quote from URL file
import scala.collection.mutable.ListBuffer
import scala.io.Source
import scala.util.Random
val sourceLines = Source.fromURL("http://www.coverfire.com/files/quotes.txt","UTF-8").getLines()
val quotes = new ListBuffer[String]
sourceLines.reduceLeft((a,b) => if (b.nonEmpty) a + " " + b else {quotes.+=(a.trim); ""})
val randomQuote = quotes.apply(Random.nextInt(quotes.size))