Skip to content

Instantly share code, notes, and snippets.

@JRuumis
JRuumis / RecursiveTree2.scala
Created March 6, 2016 21:19
Resucrive Tree
package hackerrank
/**
* Created by Janis Rumnieks on 06/03/2016.
*/
object GrowthDirection extends Enumeration {
val LEFT, STAY, RIGHT = Value
}
import GrowthDirection._ // kāpēc bez šī nevar ????????
/**
* Created by janisrumnieks on 07/03/2016.
* Hackerrank -> Functional Programming -> Recursion -> Filter Elements (10)
* https://www.hackerrank.com/challenges/filter-elements
*/
object FilterElements /* rename this to Solution for Hackerrank */ extends App {
// helper functions
def countElementsR(occurrences: Int, list: List[Int]):List[Int] = list match {
case i::rest => if (rest.count(_ == i) >= occurrences-1)
@JRuumis
JRuumis / FilterElements4.scala
Created March 8, 2016 23:17
success at last!
object FilterElements4 /* rename this to Solution for Hackerrank */ extends App {
// helper functions
def occurringElements(occur: Int, list: List[Int]):Set[Int] = {
list.groupBy(identity).mapValues( _.size ).filter{ case(key,value) => value >= occur }.keys.toSet
}
def occurringListElements (list: List[Int], occurringElements: Set[Int]): List[Int] = {
if (list == Nil || occurringElements.isEmpty) Nil
else {
object FilterElements4 /* rename this to Solution for Hackerrank */ extends App {
// helper functions
def occurringElements(occur: Int, list: List[Int]):Set[Int] = {
list.groupBy(identity).mapValues( _.size ).filter{ case(key,value) => value >= occur }.keys.toSet
}
def filterListByOccurrences(list: List[Int], occurringElements: Set[Int]): List[Int] = {
if (list == Nil || occurringElements.isEmpty) Nil
else {
/**
* Created by janisrumnieks on 07/03/2016.
* Hackerrank -> Functional Programming -> Recursion -> Filter Elements (10)
* https://www.hackerrank.com/challenges/filter-elements
*/
object FilterElements4 /* rename this to Solution for Hackerrank */ extends App {
// helper functions
def occurringElements(occur: Int, list: List[Int]):Set[Int] = {
list.groupBy(identity).mapValues( _.size ).filter{ case(key,value) => value >= occur }.keys.toSet
@JRuumis
JRuumis / FullOfColours.scala
Created March 9, 2016 22:51
interesting that when List[Char] is replaced with String, this is dead slow
/**
* Created by janisrumnieks on 09/03/2016.
* https://www.hackerrank.com/challenges/sequence-full-of-colors
*/
case class Colours (red:Int, green:Int, yellow:Int, blue:Int) {
def add (colour:Char) = colour match {
case 'R' => Colours(red+1, green, yellow, blue)
case 'G' => Colours(red, green+1, yellow, blue)
case 'Y' => Colours(red, green, yellow+1, blue)
case 'B' => Colours(red, green, yellow, blue+1)
/**
* Created by janisrumnieks on 11/04/2016.
* https://www.hackerrank.com/challenges/super-digit
*/
object SuperDigit extends App {
def sumDigits ( i: BigInt ): BigInt = i.toString.toList.map( _.toString.toInt ).foldLeft (0) ( _+_ )
def doSuper ( i: BigInt ): BigInt = if (i>9) doSuper(sumDigits(i)) else i
val in = io.StdIn
object MagicListMerger {
def listCartesian ( list: List[List[Int]] ): List[List[Int]] = ???
val sourceList =
List(
List(1,2,3),
List(10,20),
List(100,200,300,400)
def listCartesian[T] ( list: List[List[T]] ): List[List[T]] = {
def cartesianFor2 (list1: List[List[T]], list2: List[List[T]]): List[List[T]] =
for {
l1 <- list1
l2 <- list2
} yield l1 ++ l2
list map ( sublist => sublist map ( (item: T) => List(item) ) ) reduce ( cartesianFor2 )
}
@JRuumis
JRuumis / DigitRecognizer1.scala
Created August 15, 2016 23:19
When trying to create a DataFrameL Exception in thread "main" java.lang.IllegalArgumentException: java.net.URISyntaxException: Relative path in absolute URI: file:C:/Developer/Scala%20Projects/Spark2/spark-warehouse
import org.apache.spark.ml.Pipeline
import org.apache.spark.ml.classification.RandomForestClassifier
import org.apache.spark.ml.feature.{IndexToString, VectorIndexer, StringIndexer}
import org.apache.spark.sql.SparkSession
//import spark.implicits._
/**
* Created by Janis Rumnieks on 15/08/2016.
*/