Skip to content

Instantly share code, notes, and snippets.

View awostenberg's full-sized avatar

Alan Wostenberg awostenberg

View GitHub Profile
@awostenberg
awostenberg / Leonardo.fs
Created August 6, 2022 16:33
even fibs
module Tests
open System
open Xunit
open FsUnit.Xunit
let fib (a,b) =
let sum = a+b
Some (sum,(b,sum))
@awostenberg
awostenberg / palakata.fs
Created May 27, 2022 17:04
palindrome world
module Tests
(*
problem statement:
a palindrome is a string that reads same forward and backward,
e.g. 121 or tacocat.
A substring is a contiguous subset of characters in a string.
Given a string s, how many distinct substrings of s are palindromes?
for example: (initial test list)
@awostenberg
awostenberg / expression.fs
Created May 10, 2022 22:44
sql where expressions for Eric sql where for Eric sql where emitter for eric
module Tests
(*
Problem:
Given an expression tree, model the data and write a method that takes
the tree as an input and
generate a string that represents this expression.
@awostenberg
awostenberg / life.fsx
Last active February 19, 2021 20:17
life
// q: how would you encode Conways rule of life in f#?
// a: how about a pattern match emitting an action?
type Action = NoChange | BecomeInactive | BecomeActive
/// return what should happen to the central cell given active neighbor count
let kernel' centralCell activeNeighbors =
match centralCell, (activeNeighbors<2), (activeNeighbors>3), activeNeighbors=3 with
| true,true,_,_ -> BecomeInactive // living cell with fewer than two neighbors dies
| true,false,false,_ -> NoChange // living cell with two or three neighbors lives to next generation
| true,false,true,_ -> BecomeInactive // living cell with more than there neighbors dies
@awostenberg
awostenberg / intersection.fsx
Last active November 10, 2020 00:23
list intersection
//a: how would you compute intersection of two lists?
//b: I'd use set intersection, but if literally a list, I'd sort then merge:
let intersection a b =
let rec intersection' a b =
match a,b with
| _,[] -> []
| [],_ -> []
| x::a',y::b' when x=y -> x::intersection' a' b'
| x::a',y::_ when x<y -> intersection' a' b
@awostenberg
awostenberg / rle.fsx
Last active September 22, 2020 20:47
run length encoding with a fold
//remove consecutive duplicates
let uniq items =
let uniq accum item =
match accum with
| a::_ when a = item -> accum
| _ -> item::accum
items |> List.fold uniq [] |> List.rev
let uniq_tests =
@awostenberg
awostenberg / simant.scala
Last active September 3, 2020 18:33
scala random walk over a unit cube
// Consider an ant taking a random walk over corners of a cube.
// What are the average number of steps to arrive at the far corner?
// user interface is next line: you can change the sample size
val nSamples = 100_000
abstract class Direction
case class X() extends Direction
case class Y() extends Direction
case class Z() extends Direction
@awostenberg
awostenberg / simant.fsx
Last active September 3, 2020 18:34
random walk over a unit cube
// Consider an ant taking a random walk over corners of a cube.
// What are the average number of steps to arrive at the far corner?
// user interface is next line: you can change the sample size
let nSamples = 1_000_000
type Direction = X | Y | Z
/// walk in the given direction
let walk (x,y,z) direction =
@awostenberg
awostenberg / Montecarlo.fsx
Created September 3, 2020 16:58
Montecarlo simulation of likely points delivered over five sprint planning horizon given recent velocities
//Montecarlo simulation of likely points delivered this planning increment (5 sprints)
let copperV = [14;13;19;10;15]
let nRuns = 10_000
/// given velocity history and number of sprints in a PI, simulate the PI nRuns times
let sims velocityHistory nRuns nSprints =
let vHistory = velocityHistory |> Seq.toArray;
let rnd = System.Random();
let simPi = [for _ in 1..nRuns ->
@awostenberg
awostenberg / cpairs.fsx
Created March 22, 2017 05:59
strawman pair proposal
// pairs II
// for a lightweight IDE with F# syntax awareness and REPL try https://code.visualstudio.com
/// generate all possible pairings given a list of candidatesf
let rec genPossiblePairs list =
match list with
| [] -> []
| me::rest -> [for otherPerson in rest -> me, otherPerson] @ genPossiblePairs rest
genPossiblePairs [1..3]