Skip to content

Instantly share code, notes, and snippets.

View chemacortes's full-sized avatar

Chema Cortés chemacortes

View GitHub Profile
@chemacortes
chemacortes / gist:992f3fc260fa08f00e87
Last active August 29, 2015 14:01
Compute char frecuencies from a (long) string
// s:String
(s groupBy identity) mapValues (_.length)
// test
import util.Random
val s = Random.alphanumeric.slice(0, 30000).mkString
(s groupBy identity) mapValues (_.length) //-> 21.5 ms.
@chemacortes
chemacortes / gist:ab7955c242a179aba1a4
Created May 31, 2014 00:01
Value expectation for two dices
val l = for (i <- 1 to 6; j <- 1 to 6) yield List(i,j)
l.groupBy(_.sum).map(x=>x._1*x._2.length).sum / l.length
@chemacortes
chemacortes / isHex.py
Created June 5, 2014 16:23
isHex -- checking hex strings
# functional version
from string import hexdigits
def isHex(text): return all(ch in hexdigits for ch in text)
# set theory
from string import hexdigits
def isHex(text): return not (set(text) - set(hexdigits))
# regex version
import re
@chemacortes
chemacortes / bsf.scala
Created September 22, 2014 13:57
BinarySearch
def binarySearch[A:Ordering](v:Vector[A], target:A):Boolean = {
val ord = implicitly[Ordering[A]]
def bsf(start:Int, end:Int):Boolean =
if (start>end)
false
else {
val mid = (start + end ) >>> 1 // Fixed bug with N ~ 2^30
ord.compare(v(mid),target) match {
@chemacortes
chemacortes / prueba_xml.scala
Created October 3, 2014 13:24
Prueba XML en scala bastante mejorable
/*
* Prueba XML que hay que mejorar bastante
*/
import xml.parsing.XhtmlParser
val parser = new XhtmlParser(scala.io.Source.fromURL("http://ch3m4.org/blog/")) {
override val preserveWS = false
override def reportSyntaxError(s: String) {}
}
@chemacortes
chemacortes / factPar.scala
Last active August 29, 2015 14:07
Paralelización del cálculo de factorial.
import util.Random.shuffle
/* Compresión de una lista de enteros
*
* Agrupa los números enteros en multiplicandos BigInt
* listos para la multiplicación final del factorial.
*
* Se presupone que los enteros usan 32 bits y los longs usan 64 bits.
* Se multiplican enteros hasta justo sobrepasar 2^32, que es el
* límite de los enteros sin signo. Este límite es seguro, aunque
@chemacortes
chemacortes / memoize.py
Created October 15, 2014 18:56
Memoization of a function in python
from functools import wraps
def memoize(gen):
"""Memoization of a function"""
memo = {}
@wraps(gen)
def helper(k):
return memo[k] if k in memo else memo.setdefault(k, gen(k))
return helper
@chemacortes
chemacortes / fact.hs
Last active August 29, 2015 14:12
Factorial en haskell
fact :: Integral a => a -> Integer
fact n
| n<=1 = 1
| otherwise = (toInteger n) * fact (n-1)
fact' :: Integer -> Integer
fact' 1 = 1
fact' n = fact_rec n 1
where fact_rec :: Integer -> Integer -> Integer
fact_rec n' res
@chemacortes
chemacortes / para.scala
Last active August 29, 2015 14:12
Porqué es mala idea compartir *"estados mutables"* en multihilo. (Probado con una CPU de 8 núcleos).
object para extends App {
// #cpu cores (can reach 100% of CPUtime)
val NCORES = 8L
// MONOthread version
def count(n: Long) = {
var cont = 0
(1L to n) foreach { _ =>
@chemacortes
chemacortes / log2.hs
Last active August 29, 2015 14:13
Logaritmo en base 2 de números enteros
log2 :: Integer -> Integer
log2 n
| n < 1 = undefined
| n < 2 = 0
| otherwise = 1 + log2 (n `div` 2)