Skip to content

Instantly share code, notes, and snippets.

View trevorsibanda's full-sized avatar
🏠
Working from home

Trevor Sibanda trevorsibanda

🏠
Working from home
View GitHub Profile
@trevorsibanda
trevorsibanda / json-parser.hs
Created July 5, 2021 14:39
haskell json parser written in haskell
-- Json parser written in haskell
-- Credit: https://www.youtube.com/watch?v=N9RUqGYuGfw
{-# Language OverloadedStrings #-}
{-# LANGUAGE ViewPatterns #-}
module Parser (
JValue(..)
,parseJson
,ParseResult
,Parse
@trevorsibanda
trevorsibanda / gsoc-report-2017.md
Last active August 29, 2017 12:33
Google Summer of Code 2017 Report

Google Summer of Code

Google Summer of Code 2017

SLICK bug and feature hunt

def ?(r: Int, ops: Seq[Char], nums: Seq[Int]):Int=if(ops.isEmpty)r else ?(ops.head match{case '+' => r+nums.head; case '-' => r-nums.head}, ops.tail, nums.tail); (1 to 9).map{i=>"123456789".grouped(i).toList}.map{_.map{_.toInt}}match{ case l => l.zip((1 to l.length).permutations.toList.map{_.map{i => if(i%2 == 0) '+' else '-'})}.map{x=> (?(0, x._1, x._2), x)}}.filter(_ != 100)
class A{
public:
A():v(0){}
A* incr(){
v++;
return this;
}
private:
int v;
};
def I(s):
val = 0
for i in range(len(s)):
digit = ord(s[len(s) - i - 1])
val <<= 8
val |= digit
return val
def Sn(i, length):
s = ''
@trevorsibanda
trevorsibanda / db.cpp
Created April 29, 2017 14:25
C++ define sql tables as classes. typesafe.
//g++ db.cpp -o db.exe 2>&1 | more
#include<algorithm>
#include <string>
#include <vector>
#include<iostream>
#include <typeinfo>
#include <map>
using namespace std;
//simple algorithm to calculate entropy of a file in Scala
package zw.trevor.disorder
class Entropy(bytes: Seq[Byte]){
val max_bytes = 256
def log2(n: Double) = math.log(n)/math.log(2)
lazy val token_freqs = {
var freqs: Array[Int] = (0 until max_bytes).map{x=>0}.toArray
bytes.map{b =>
@trevorsibanda
trevorsibanda / UnLuckyNumbers.scala
Created September 29, 2016 10:08
Lucky numbers
//Lucky numbers challenge
//http://codegolf.stackexchange.com/questions/94695/unlucky-numbers
case class LuckyNumbers(val n: Double, seq: Seq[Double] )
case class Test(val m: Int, val n: Int, val expected: Double)
object LuckyNumbers{
def natural_numbers(max: Double) = {
def next(n: Double, seq: Seq[Double]): Seq[Double] = if(n >= max) seq ++ Seq(n) else next(n+1, seq ++ Seq(n) )
next(1, Seq())
}
@trevorsibanda
trevorsibanda / OddOneOut.scala
Created September 28, 2016 09:26
Consider the function F(N) = 2^N + 1 where N is a positive integer less than 31. Given such a list of 5 integers, find the one that was swapped in and is therefore not part of the original 5 contiguous integers.
//http://codegolf.stackexchange.com/questions/94679/find-the-odd-one-out-in-a-sequence
import math._
object OddOneOut{
case class Test(val l: Seq[Double],val expected: Double){
def apply( _l: Seq[Double], _e: Double ) = new Test( _l, _e )
}
def log2(x: Double) = math.log(x)/math.log(2)
@trevorsibanda
trevorsibanda / DnaEncoderDecoder.scala
Last active September 28, 2016 07:59
an encoder (and a separate decoder) which takes a string as input and outputs the string encoded in the style of a strand of DNA. Can be improved
//Simple DNA encoder
//http://codegolf.stackexchange.com/questions/91645/dna-encode-a-string/91850
import java.io.{ByteArrayOutputStream, ByteArrayInputStream}
import java.util.Base64
import java.util.zip.{GZIPOutputStream, GZIPInputStream}
import scala.util.Try
object Nucleotide{
abstract class Nucleotide(val symbol: String){