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
trait Monoid[A] { | |
def mappend(a: A, b: A): A | |
def mzero: A | |
} | |
object Monoid { | |
implicit val IntMonoid: Monoid[Int] = new Monoid[Int] { | |
def mappend(a: Int, b: Int) = a + b | |
def mzero = 0 | |
} |
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
// compile error | |
import scala.annotation.tailrec | |
@tailrec | |
def fib(n: Int): Int = n match { | |
case 1 | 2 => 1 | |
case _ => fib(n - 1) + fib(n - 2) | |
} |
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
f1 = 1 | |
f2 = 2 | |
sum = 0 | |
loop { | |
break if f2 > 4000000 | |
sum += f2 if f2 % 2 == 0 | |
f2 = f1 + f2 | |
f1 = f2 - f1 | |
} |
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
require 'date' | |
t1 = DateTime.parse(ARGV[0]) | |
t2 = DateTime.parse(ARGV[1]) | |
seconds = ((t2 - t1) * 24 * 60 * 60).to_i | |
r = seconds.divmod(60) | |
puts "#{r[0]}min#{r[1]}sec" |
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
require 'redis' | |
require 'hiredis' | |
$redis = Redis.new( | |
host: "127.0.0.1", | |
port: 6379, | |
timeout: 30.0, | |
driver: :hiredis | |
) |
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
// project euler 28 | |
// | |
// The sum of the vertices can be expressed as follows. The w is width of the square. | |
// | |
// sum = (w - 2)^2 + (w - 1) | |
// + (w - 2)^2 + 2(w - 1) | |
// + (w - 2)^2 + 3(w - 1) | |
// + (w - 2)^2 + 4(w - 1) | |
// = 4w^2 - 6w + 6 | |
// |
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 scala.io.Source | |
val source = Source.fromFile("triangle.txt") | |
val triangle = collection.mutable.Map[Int, List[Int]]() | |
source.getLines foreach { line => | |
triangle += triangle.size -> line.split(" ").map(_.toInt).toList | |
} | |
def neighborsMaxList(list: List[Int]): List[Int] = list match { | |
case l if l.length == 1 => Nil |
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
# coding: utf-8 | |
import math | |
def eratosthenes(x): | |
sieve = [] | |
prime = [] | |
for num in range(2, x): | |
sieve.append(num) |
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
private int gcd(int m, int n) { | |
int r = m % n; | |
if (r == 0) { | |
return n; | |
} | |
return gcd(n, r); | |
} |
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
(load (expand-file-name (concat (getenv "HOME") "/.emacs.d/common.el"))) |