Skip to content

Instantly share code, notes, and snippets.

@voila
voila / change.js
Created July 6, 2019 23:11
Change recursive to iterative
/*
How many different ways can we make change of $ 1.00, given half-dollars, quarters, dimes, nickels, and pennies? More generally, can we write a procedure to compute the number of ways to change any given amount of money?
This problem has a simple solution as a recursive procedure.
Suppose we think of the types of coins available as arranged in some order. Then the following relation holds:
The number of ways to change amount a using n kinds of coins equals
the number of ways to change amount a using all but the first kind of coin, plus
the number of ways to change amount a - d using all n kinds of coins, where d is the denomination of the first kind of coin.
@voila
voila / fact.js
Created July 3, 2019 22:06
Factorial recursive to iterative
let fact0 = (n) => {
if (n > 0) {
return n * fact0(n - 1);
} else
return 1;
}
console.log(fact0(5, (x) => x))
let fact1 = (n, k) => {
@voila
voila / hanoi.js
Last active July 3, 2019 22:05
Hanoi Tower recursive to iterative
// recursive
const hanoi0 = (n, a, b, c) => {
if (n > 0) {
hanoi0(n - 1, a, c, b);
console.log(`${a} ==> ${c}`);
hanoi0(n - 1, b, a, c);
}
}
hanoi0(3, "a", "b", "c");
@voila
voila / erlang_shell.txt
Created July 3, 2019 22:03
Water Jugs puzzle with QuickCheck/PropEr
1> c("jug_statem.erl").
{ok,jug_statem}
2> jug_statem:test(100).
....................................................................................................
OK: Passed 100 test(s).
18% {jug_statem,fill_big,0}
17% {jug_statem,big_to_small,0}
@voila
voila / jugs.py
Created July 3, 2019 22:00
Water Jugs puzzle with Python-Z3
from z3 import *
Jug, (Big, Small) = EnumSort(
'Jug', ['Big', 'Small'])
Act, (FillBig, FillSmall, EmptyBig, EmptySmall, BigToSmall, SmallToBig) = EnumSort(
'Act', ['FillBig', 'FillSmall', 'EmptyBig', 'EmptySmall', 'BigToSmall', 'SmallToBig'])
#m = 10
for n in range(5, 10):
print("n = {}".format(n))
@voila
voila / jugs.z3
Created July 3, 2019 21:59
Water Jugs puzzle in Z3
(declare-const n Int)
(declare-datatypes () ((Jug Big Small)))
(declare-datatypes () ((Act FillBig FillSmall EmptyBig EmptySmall BigToSmall SmallToBig)))
(declare-fun vol (Jug Int) Int)
(declare-fun act (Int) Act)
(assert (= n 6))
(assert (= (act 0) EmptyBig))
(assert (= (vol Big 0) 0))
(assert (= (vol Small 0) 0))
@voila
voila / jugs.als
Created July 3, 2019 21:57
Water Jugs puzzle in Alloy
/* Impose an ordering on the State. */
open util/ordering[State]
open util/integer
/* Stores the jugs level */
sig State { small, big:Int }
/* initial state */
fact { first.small = 0 && first.big = 0 }

Keybase proof

I hereby claim:

  • I am voila on github.
  • I am th3rac25 (https://keybase.io/th3rac25) on keybase.
  • I have a public key ASCeC9hVEe0BrGoScMK4IAb0V3F8O40tgBLXc6nyxW-_hAo

To claim this, I am signing this object:

open Printf
module type Ord =
sig
type t
val lt : t -> t -> bool
val lte : t -> t -> bool
val eq : t -> t -> bool
val succ : t -> t
end
open Printf
module type Ord =
sig
type t
val lt : t -> t -> bool
val lte : t -> t -> bool
val eq : t -> t -> bool
end