Skip to content

Instantly share code, notes, and snippets.

@kencoba
kencoba / n-queen.clj
Created May 23, 2011 05:54
solve n_queen problem
(use '[clojure.contrib.combinatorics :only (permutations)])
(defn- valid? [board]
{:pre [(vector? board)]}
(let [n (count board)]
(every?
#(let [[i j] %] (and (not= (board i) (board j))
(not= (- i (board i)) (- j (board j)))
(not= (+ i (board i)) (+ j (board j)))))
(for [i (range n) j (range n) :when (not= i j)] (vector i j)))))
@kencoba
kencoba / graph_iso.clj
Created May 23, 2011 10:05
check graph isomorphism (not complete)
(use '[clojure.contrib.graph]
'[clojure.test])
(def empty-graph (struct directed-graph #{} {}))
(def test-graph-1
(struct directed-graph
#{:a :b :c :d :e}
{:a #{:b :c}
:b #{:a :c}
@kencoba
kencoba / PublicKeyEncoder.java
Created June 14, 2011 10:13
IPSJ problem: public key encoding
/*
* 共通鍵暗号化
* 仕様:
* キー = {キーの長さ,左循環シフト回数,キー本体}
* 平文 = byte列
* 暗号化操作
*  1.平文を、キー本体のサイズごとに分割し、部分平文を作る
* 2.部分平文に対し、左循環シフト回数分、左循環シフトし、シフト文を作る
* 3.シフト文とキー本体のxorをとり、暗号化部分バイト列を作る
* 4.暗号化部分バイト列を結合し、暗号化文を作る
@kencoba
kencoba / user.clj
Created August 9, 2011 09:59
awk-like utility functions for clojure.
(use '[clojure.contrib.duck-streams :only (write-lines writer read-lines)])
(use '[clojure.contrib.str-utils :only (re-split)])
(in-ns 'clojure.contrib.duck-streams)
(def *default-encoding* "Shift_JIS")
(in-ns 'user)
(defmacro awk-print [input-file f]
`(map ~f (for [line# (read-lines ~input-file)] line#)))
@kencoba
kencoba / fmf_20111010.v
Created October 11, 2011 09:16
Purely Functional Data Structures Exercise 2.1 and 2.2
Require Import List.
Check 1::2::nil.
Fixpoint suffixes{A:Set}(xs:list A):list (list A) :=
match xs with
| nil => nil
| _::xs' => xs :: (suffixes xs')
end.
@kencoba
kencoba / AbstractFactory.scala
Created February 21, 2012 05:08
AbstractFactory pattern with Scala
/**
* Example of AbstractFactory pattern
*
* create OS Specific GUIWidget with same operation.
*
*/
trait Button {
def paint: String
}
@kencoba
kencoba / Builder.scala
Created February 21, 2012 05:43
Builder pattern (Design patterns in Scala)
abstract class Product
abstract class PizzaBuilder {
var dough: String
var sauce: String
var topping: String
def withDough(dough: String): PizzaBuilder
def withSauce(sauce: String): PizzaBuilder
def withTopping(topping: String): PizzaBuilder
@kencoba
kencoba / FactoryMethod.scala
Created February 21, 2012 06:04
FactoryMethod pattern (Design Patterns in Scala)
// http://blog.designrecipe.jp/2011/01/03/factory-method-strategy/
abstract class ListPrinter {
def printList(list: List[String]) {
val comparator = createComparator
list.sortWith(comparator).foreach(println)
}
protected def createComparator: (String, String) => Boolean
}
@kencoba
kencoba / Prototype.scala
Created February 21, 2012 07:46
Prototype pattern (Design Patterns in Scala)
@cloneable
class Command(value: Int, name: String) {
override def clone = super.clone
override def toString = value + ": " + name
}
object Main {
def main(args: Array[String]) = {
val com1 = new Command(1, "Hoge")
val com2 = com1.clone
@kencoba
kencoba / Singleton.scala
Created February 21, 2012 07:48
Singleton pattern (Design Patterns in Scala)
object Singleton {
var name: String = ""
}
object Main {
def main(args: Array[String]) {
var s1 = Singleton
var s2 = Singleton
println (s1 eq s2)
s1.name = "First"