Skip to content

Instantly share code, notes, and snippets.

//forked from https://gist.github.com/jkdeveyra/4030815
def sentenceAnagrams(sentence: Sentence): List[Sentence] = {
def subSentence(occ: Occurrences): List[Sentence] = {
if (occ.isEmpty) List(List())
else
for {
x <- combinations(occ)
y <- dictionaryByOccurrences.getOrElse(x, List())
@valtih1978
valtih1978 / Permutations.sc
Last active August 29, 2015 14:03
Permutations with n replacements, which generalizes permutations with replacements (replace infinitely) and without premutations (every item is available only once)
// Inspired by Oderski's anagram and the idea that SICP count change algorithm can be used
// for anagram lookup and more efficiently than odersky wanted. Anagrams are however permu
// tated "changes" and, thus, we need an algorithm for permutation. There is none in stack
// overflow, at least I could not find out whether scala and other programmers provide per
// mutations with replacements or not whereas anagrams requre special kind of permutations
// It should be that "aab", "aba" and "baa" would be permutations. Note multiplicity 2 of
// 'a'. With replacements produces aaa since a is not exhausted by two repetitions wheras
// without repetitions cannot produce sequences longer than 2 since picked two items we a
// re out of items in the dictionary. If we put two a items in the dictionary, with/wo rep
// etitions will produce idential sequences, aab and aab can be produced two times.
object GenericChange {
// Part I: generic coin change algorithm
// SICP count change algorithm, http://mitpress.mit.edu/sicp/full-text/sicp/book/node16.html
def coin_change(sum: Int, coins: List[Int]): Int = {
if (sum == 0) 1 else
if (coins.isEmpty || sum < 0) 0 else
coin_change(sum - coins.head, coins) + coin_change(sum, coins.tail)
class Args (val args: Array[String]) extends Enumeration {
class OptVal extends Val {
override def toString = "-" + super.toString
}
val nopar, silent, reportSamples = new OptVal() { // boolean options
def apply(): Boolean = args.contains(toString)
}
@valtih1978
valtih1978 / AnovaVsStudentTest.sc
Created February 6, 2015 19:13
Comparison of ANOVA F-Test with Student's T-Test
// Rumor says that they must match. They do indeed (I believe less than 1% mistmach is because
// tables are not 100% complete rather than invalid input data)
import math.pow
object student_vs_anova {
def log(msg: String) {
//println(msg)
} //> log: (msg: String)Unit
def fTest(alpha: Double, var1: Double, freedoms1: Int, var2: Double, freedoms2: Int) = {
@valtih1978
valtih1978 / Machine.sc
Last active August 29, 2015 14:16
Indeterministic/Propabilistic Machine
// Indeterministic chooses all possible states, maintaining probabilities
// Probabilistic chooses one of possible states on every transition.
object Probabilistic {
class Machine[T : Ordering] (transitions: Map[T, Set[T]]) { // TODO: probabilistic transitions
def step(history: List[T]) = {
val p2 = transitions(history.head)
val picked = (Math.random() * p2.size).toInt
p2.toList(picked) :: history
@valtih1978
valtih1978 / Scala+gcc.sc
Last active August 29, 2015 14:17
Remove executable fails after gcc > execute > kill > remove executable
File deletion does not have immediate effect at least process image file, at least when
process was killed ungracefully prematurely. I have to retry the delete in my app.
Also, EVEN IF FILE DELETED, IT IS REPORTED EXISTING and complire cannot reporduce the
file. I have to retry the compilation also.
object prog_gen extends App {
import scala.sys.process._, scala.concurrent._, ExecutionContext.Implicits.global, Paths.get
implicit def toPath(filename: String) = get(filename)
@valtih1978
valtih1978 / 1.generators.sc
Last active August 29, 2015 14:22
Scala reactive programming FRP
object generator {
// Coursera Reactive programming
trait Generator[T] { self =>
def generate: T
def map[K](f: T => K) = new Generator[K] {
// {val sample = generate ; println("map creates a generator [" + sample.getClass().getSimpleName + "]") }
def generate: K = f(self.generate)
}
@valtih1978
valtih1978 / 1. asexual_oversimplication.scala
Last active August 29, 2015 14:25
4% degradation with every generation
// This talk https://www.youtube.com/watch?v=MGfr_col5PI claims that if 4% of healty people degenerate with every
// and if 50% recover from defective genetics then we reach 8% failed genetics. Here is the simulation that confirms this.
def sim1(healthy: Double, generation: Int = 1): Unit = if (generation < 100) {
val degenerates = 1 - healthy; println((degenerates * 100) + "% down" )
sim1(healthy * 0.96 + degenerates * 0.5, generation + 1)
} //> sim1: (healthy: Double, generation: Int)Unit
sim1(1) //> 0.0% down
//| 4.0000000000000036% down
@valtih1978
valtih1978 / Expressions.sc
Last active November 4, 2015 20:29
Symbolic expressions
trait E {
def e(dict: Map[Const, Int]): Int
def +(another: E): E = (this, another) match {
case (zero, _) => another // Bug! Bug! this unintentionally redefines zero
case (_, zero) => this
case _ => Plus(this, another)
}
def *(another: E): E = {
def par(e: E) = e match {case e @ Plus(a,b) => Par(e); case e => e}
Mult(par(this), par(another))