Skip to content

Instantly share code, notes, and snippets.

View fancellu's full-sized avatar

Dino Fancellu fancellu

View GitHub Profile
@fancellu
fancellu / FirstToFinishMost.scala
Last active August 29, 2015 14:17
Quick coding test. Given tasks to people, find person with most tasks, that finished first
// Broken apart to ease understanding.
val s1=Seq(1,1,1,2,2,2,3,3,3,3,4,4,4,4,1)
val groups=s1.groupBy { identity }
val sizes=groups.mapValues { _.size}
val maxSize=sizes.values.max
val maxUsers=sizes.filter(_._2==maxSize).keys.toSet
val pop=s1.filter(maxUsers).zipWithIndex
val lastPlaces=pop.groupBy(_._1).mapValues(_.last)
@fancellu
fancellu / DropNth.scala
Created July 30, 2015 18:46
Drop Nth element from List alternative soltuion
http://aperiodic.net/phil/scala/s-99/p16.scala
Mine
def dropNth[A](n: Int, ls: List[A])=
ls.grouped(n).flatMap(_.take(n-1)).toList
---------
val li=List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k)
@fancellu
fancellu / RotateN.scala
Last active August 29, 2015 14:26
RotateN to left : Alternate solution for http://aperiodic.net/phil/scala/s-99/p19.scala
Alternate solution for
http://aperiodic.net/phil/scala/s-99/p19.scala
def rotate[A](n: Int, ls: List[A])=
if (ls.isEmpty) ls
else{
val (first,last)=ls.splitAt((if (n>=0) n else ls.length+n)%ls.length)
last:::first
}
@fancellu
fancellu / T1.scala
Last active September 10, 2015 15:27
ScalaJS ractive example - Live demo http://dinofancellu.com/demo/scalajsRactive/
package example
import org.scalajs.dom
import scalajs.js.annotation.JSExport
import scala.scalajs.concurrent.JSExecutionContext.Implicits.runNow
import dom.ext.Ajax
import scalajs.js
import js.Dynamic.literal
import com.felstar.scalajs.ractive._
@JSExport
@fancellu
fancellu / Checkout.scala
Last active October 9, 2015 11:19
Checkout Kata CodeTest in Scala
object Checkout extends App {
val rulesString = """
A,50,3 for 130
B,30,2 for 45
C,20
D,15
"""
import CheckoutRuleEngine._
@fancellu
fancellu / Spec
Created February 11, 2014 09:12
Scala coding test (note error in the Spec I was sent, I'll let you work out what it was)
Exercise
Implement a console-based social networking application (similar to Twitter) satisfying the scenarios below.
Scenarios
Posting: Alice can publish messages to a personal timeline
> Alice -> I love the weather today
> Bob -> Oh, we lost!
> Bob -> at least it's sunny
@fancellu
fancellu / Compression.scala
Last active November 10, 2015 16:41
Compression for HackerRank
def pack(s:String):List[String]=
if (s.isEmpty()) Nil
else{
val (packed, rest) = s span { _ == s.head }
packed::pack(rest)
}
def rle(s:String)= pack(s).map(e=>(e.head,e.length))
def compress(s:String):String=
@fancellu
fancellu / Subsequences.scala
Last active November 10, 2015 16:42
k-subSequence for HackerRank
def kSub(k:Int,nums:Array[Int]):Long={
val count=for {
i<-0 to nums.size
j<-i to nums.size
slice=nums.slice(i, j)
sum=slice.sum
} yield if (sum!=0 && (sum%k)==0) 1 else 0
count.sum
}
@fancellu
fancellu / Pangrams.scala
Created November 3, 2015 15:18
Pangrams for HackerRank
def isPangram(strings: Array[String]): String = {
val values=for{
string<-strings
set=string.toLowerCase().toSet
isPangram=('a' to 'z').forall(set.contains)
} yield if (isPangram) 1 else 0
values.mkString
}
@fancellu
fancellu / Closest.scala
Created November 3, 2015 15:13
Closest Numbers for HackerRank
def closestNumbers(s:String):String={
val sorted=s.split(" ").map(_.toInt).toList.sorted
val zip=sorted.zip(sorted.tail)
val triples=for {
(x,y)<-zip
} yield (x,y,Math.abs(x-y))
val (_,_,minDelta)=triples.minBy(_._3)