Skip to content

Instantly share code, notes, and snippets.

View SentenceAnagram.scala
//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 Aug 29, 2015
Permutations with n replacements, which generalizes permutations with replacements (replace infinitely) and without premutations (every item is available only once)
View Permutations.sc
// 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.
View GenericCoinChange.sc
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 / CoinChange+perm=Anagrams.sc
Last active Jun 27, 2017
Describes how to exploint coin change algorithm + permutations for anagram search
View CoinChange+perm=Anagrams.sc
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
@valtih1978
valtih1978 / HuffmanScalaAssignment.sc
Last active May 3, 2018
Odersky ProgFun coursera Huffman coding assignment
View HuffmanScalaAssignment.sc
/**
* 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
View ArgParser.sc
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 Feb 6, 2015
Comparison of ANOVA F-Test with Student's T-Test
View AnovaVsStudentTest.sc
// 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 Aug 29, 2015
Indeterministic/Propabilistic Machine
View Machine.sc
// 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 / 1.sequence_generator.py
Last active Sep 17, 2017
Sequence Generator - generates some C (or Java?) programs
View 1.sequence_generator.py
# There were multiple goals: to implement AI for my game, to inject bugs ino VHDL and prove that sexual demorphims
# where boys have much higher mutation rate ' Сперматозоидов у мужчины за всю его жизнь вырабатывается гораздо больше, чем яйцеклеток у женщины. Это значит, что предшественники мужских половых клеток делятся гораздо интенсивнее и, следовательно, у них больше риск возникновения мутаций.'
#, is advantageous.
# Inspired by http://eli.thegreenplace.net/2010/01/28/generating-random-sentences-from-a-context-free-grammar
from collections import defaultdict
import random
def weighted_choice(weights):
rnd = random.random() * sum(weights)
@valtih1978
valtih1978 / Scala+gcc.sc
Last active Aug 29, 2015
Remove executable fails after gcc > execute > kill > remove executable
View Scala+gcc.sc
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)
You can’t perform that action at this time.