This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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) {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { _ => |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
log2 :: Integer -> Integer | |
log2 n | |
| n < 1 = undefined | |
| n < 2 = 0 | |
| otherwise = 1 + log2 (n `div` 2) |
OlderNewer