Skip to content

Instantly share code, notes, and snippets.

View tnoda's full-sized avatar

Takahiro Noda tnoda

View GitHub Profile
@tnoda
tnoda / mell.el
Last active October 26, 2015 17:34
Mirror of some taiyaki.org elisp files
;;; mell.el --- MELL Emacs Lisp Library
;;
;; AUTHOR: Hiroyuki Komatsu <[email protected]>
;; LICENCE: GPL2
;; $Id: mell.el,v 1.4 2003/03/18 03:34:45 komatsu Exp $
;; Version: 1.4.0
;;
;; ------------------------------------------------------------
;; XEmacs と FSF Emacs の差異を吸収
@tnoda
tnoda / pe10.clj
Last active August 29, 2015 14:03
Project Euler Problem 10 は Clojure でやっても 100ms 未満で計算できるはず。
(ns tnoda.pe10)
(defn- sieve
"Returns an array of primes below len. Retrieved from
https://github.com/tnoda/tnoda.math.prime."
[^long len]
(let [n len
not-prime (doto (boolean-array n)
(aset 0 true)
(aset 1 true))
@tnoda
tnoda / union_find.py
Created June 24, 2014 08:34
Union-Find in Python
class UnionFind:
"""Weighted quick-union with path compression.
The original Java implementation is introduced at
https://www.cs.princeton.edu/~rs/AlgsDS07/01UnionFind.pdf
>>> uf = UnionFind(10)
>>> for (p, q) in [(3, 4), (4, 9), (8, 0), (2, 3), (5, 6), (5, 9),
... (7, 3), (4, 8), (6, 1)]:
... uf.union(p, q)
@tnoda
tnoda / BinarySearch.scala
Last active August 29, 2015 14:01
An unoptimized Scala equivalent of C++'s std::lower_bound and std::upper_bound
import scala.annotation.tailrec
object BinarySearch {
def lowerBound(xs: Array[Int], x: Int): Int = {
@tailrec
def loop(first: Int, count: Int): Int =
if (count == 0) first
else {
val step = count / 2
if (xs(first + step) < x) loop(first + step + 1, count - step - 1)
@tnoda
tnoda / PrimeNumber.scala
Last active August 29, 2015 14:00
Sieve of Eratosthenes in Scala
import scala.annotation._
import scala.collection.mutable.ArrayBuffer
object PrimeNumber {
def apply(n: Int = 1000000): Array[Int] = sieve(n)
def sieve(n: Int): Array[Int] = {
val primes = new ArrayBuffer[Int]
val isPrime = Array.fill(n + 1)(true)
@tnoda
tnoda / admiral.clj
Last active December 27, 2015 20:29
Translating the `break` statement into Clojure.
(let [m (BattleMap. "Ironbottom Sound")
f (Fleet. "Center Fleet")]
(loop []
(doto f .repair .supply)
(when (and (.isFullyRepaired f)
(.isFullySupplied f)
(>= (.morale f) 40))
(.attack f m)
(if (-> m .gauges pos?)
(recur))))
(defun shibayu36/chomp (str)
(replace-regexp-in-string "[\n\r]+$" "" str))
(defun anything-git-project-project-dir ()
(shibayu36/chomp
(shell-command-to-string "git rev-parse --show-toplevel")))
(defun anything-c-sources-git-project-for ()
(loop for elt in
'(("Modified files (%s)" . "--modified")
@tnoda
tnoda / DrillOne.scala
Created April 3, 2013 10:46
[再帰ドリル](https://github.com/kazu-yamamoto/recursion-drill) を Scala で書いてみる.
package recfun
object DrillOne {
// sum of arithmetic progression
def soap(x: Int): Int =
if (x == 0) 0
else soap(x - 1) + x
// factorial
def fact(x: Int): Int =
require 'rake'
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
task :default => :spec
@tnoda
tnoda / HelloWorld.java
Created February 2, 2013 07:42
MIxed-source Leiningen project example.
public class HelloWorld {
public static void helloWorld() {
System.out.println("Hello, world!");
}
}