Skip to content

Instantly share code, notes, and snippets.

@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 / 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) = {
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 / HuffmanScalaAssignment.sc
Last active May 3, 2018 17:30
Odersky ProgFun coursera Huffman coding assignment
/**
* Assignment 4: Huffman coding
*
*/
object Huffman1 {
abstract class CodeTree
case class Fork(left: CodeTree, right: CodeTree, chars: List[Char], weight: Int) extends CodeTree
case class Leaf(char: Char, weight: Int) extends CodeTree
@valtih1978
valtih1978 / CoinChange+perm=Anagrams.sc
Last active June 27, 2017 17:23
Describes how to exploint coin change algorithm + permutations for anagram search
package forcomp
object SicpAnagrams {
// Inplementing Odersky anagrams (FP Scala coursera) using SICP algorithm, http://mitpress.mit.edu/sicp/full-text/sicp/book/node16.html
// PART I: SICP count change algorithm
// Odersky recomends SICP book as the course resource material. Before taking the course,
// I studied it. Might be for this reason, I realized that the anagram assignment is the
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)
@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.
//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())