Skip to content

Instantly share code, notes, and snippets.

View fancellu's full-sized avatar

Dino Fancellu fancellu

View GitHub Profile
@fancellu
fancellu / Fibonacci.scala
Last active April 12, 2020 11:28
Some lazy fibonacci solutions in Scala
val fibs: Stream[BigInt] = BigInt(0) #:: BigInt(1) #:: fibs.zip(fibs.tail).map { n => n._1 + n._2 }
def fibs(x: BigInt=0, y: BigInt=1): Stream[BigInt] = x #:: fibs(y, x + y)
val fibi: Iterator[BigInt] = Iterator.iterate((0,1)){case (x,y) => (y,x+y)}.map(_._1)
def fibi(x: BigInt=0, y: BigInt=1): Iterator[BigInt] = Iterator.single(x)++fibi(y, x + y)
println(fibs.take(19).toList)
println(fibs().take(19).toList)
@fancellu
fancellu / Influx1.scala
Last active June 10, 2017 02:02
Tiny bit of code emitting to InfluxDB Time Series Database in Scala
package influxdb
import org.influxdb._
import org.influxdb.dto._
import java.util.concurrent.TimeUnit
object Influx1 extends App {
val influxDB = InfluxDBFactory.connect("http://localhost:8086", "root", "root");
println(influxDB.describeDatabases())
@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)
@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 / 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 / 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 / multireplace.xslt
Created November 13, 2015 21:37
Example of recursive text replace based on lookup data
<?xml version="1.0" encoding="UTF-8"?>
<document>
<people><person name="John" id="john"/><person name="Mary" id="mary"/></people>
<text>Hello John, say hi to Mary</text>
</document>
@fancellu
fancellu / RewriteRuleExample.scala
Last active November 13, 2015 23:59
RewriteRuleExample for Scala XML
import scala.xml._
import scala.xml.transform._
object RewriteRuleExample extends App {
val xml =
<document>
<people><person name="John" id="john"/><person name="Mary" id="mary"/></people>
<text>Hello John, say hi to Mary</text>
</document>
@fancellu
fancellu / Robot.scala
Last active November 23, 2015 09:27
Simple treasure finding robot in a 3D maze
object Robot extends App {
// proper Maze implementation should be here, not my concern
object Maze{
def isDoorway(direction:Int):Boolean=true
def isTreasureRoom():Boolean=false
def move(directon:Int)={}
}
@fancellu
fancellu / Catalog.scala
Created November 23, 2015 09:19
Simple example of document store, one mutable the other immutable.
object stuff {
class Catalog(documentsIn:List[Document]=List.empty){
def isEmpty=_documents.isEmpty
def add(document:Document)= _documents::=document
private[this] var _documents=documentsIn
def documents= _documents
}
class ImCatalog(val documents:List[Document]=List.empty){