Skip to content

Instantly share code, notes, and snippets.

View ngsw-taro's full-sized avatar

Taro Nagasawa ngsw-taro

View GitHub Profile
@ngsw-taro
ngsw-taro / divisor.kt
Created December 8, 2012 07:17 — forked from necoco/divisor.kt
宿題
fun divisor(val a: Int,val b: Int) : Int = if(b == 0) a else divisor(b, a % b)
fun main(args: Array<String>){
test(1, divisor(1, 1))
test(2, divisor(2, 4))
test(2, divisor(4, 2))
test(6, divisor(24, 18))
test(6, divisor(18, 24))
test(1, divisor(13, 19))
test(1, divisor(19, 13))
@ngsw-taro
ngsw-taro / hit_and_blow.rb
Last active December 11, 2015 08:08
Rubyの学習のため、ヒット&ブローを作ってみた。 Rubyよくわからないので、指摘しまくってください。
# ヒット&ブローを表すクラス。
# このクラスのオブジェクトはイミュータブル。
class HitAndBlow
NUMBERS_LENGTH = 4
NUMBER_RANGE = (1..9)
# 指定された配列が、正解の数の形式として妥当か判定する。
# 数の重複がなく、所定の範囲ないの値を持ち、長さがNUMBERS_LENGTHの配列ならばtrueを、それ以外ならfalseを返す。
def self.valid_numbers? submitted_numbers
@ngsw-taro
ngsw-taro / gist:5988944
Last active December 19, 2015 17:09
Clojureの練習
(defn index-to-pos [index width] [(int (/ index width)) (rem index width)])
(defn pos [{width :width cells :cells} cell] (index-to-pos (.indexOf cells cell) width))
(defn distance [[r1 c1] [r2 c2]] (+ (Math/abs (- r1 r2)) (Math/abs (- c1 c2))))
(defn win-distance [{width :width cells :cells :as board} cell]
(distance (pos board cell) (pos {:width width :cells (range 0 (count cells))} cell)))
(defn total-win-distance [{cells :cells :as board}]
@ngsw-taro
ngsw-taro / gist:6149089
Last active December 20, 2015 14:48
KotlinでDBアクセス
// 原始的でシンプルな方法
package com.taroid.sample
import java.sql.DriverManager
import java.sql.ResultSet
import java.sql.Statement
import java.sql.Connection
fun main(args: Array<String>) {
@ngsw-taro
ngsw-taro / Slidepuzzle.scala
Created August 26, 2013 09:30
Scalaの練習
object Main {
def main(args: Array[String]) {
val puzzle = new SlidePuzzle(3, List(3, 1, 2, 4, 7, 5, 6, 0, 8), 0)
val solvedPuzzleHistory = SlidePuzzle.solve(puzzle)
assert(solvedPuzzleHistory(3).cells == List(3, 1, 2, 4, 7, 5, 6, 0, 8))
assert(solvedPuzzleHistory(2).cells == List(3, 1, 2, 4, 0, 5, 6, 7, 8))
assert(solvedPuzzleHistory(1).cells == List(3, 1, 2, 0, 4, 5, 6, 7, 8))
assert(solvedPuzzleHistory(0).cells == List(0, 1, 2, 3, 4, 5, 6, 7, 8))
}
@ngsw-taro
ngsw-taro / Main.scala
Last active December 21, 2015 20:49
Scalaの練習。SlideshareのAPI叩いてみた
// 使用例
import com.taroid.slideshare4s.{SortOrder, SlideShare, Query}
object Main {
def main(args: Array[String]) {
val apiKey = System.getenv("SLIDESHARE_API_KEY")
val sharedSecret = System.getenv("SLIDESHARE_SHARED_SECRET")
val ss = SlideShare(apiKey, sharedSecret)
val query = Query(words = "kotlin", itemsPerPage = 10, language = "ja", sortOrder = SortOrder.LATEST)
@ngsw-taro
ngsw-taro / gist:6365287
Created August 28, 2013 12:07
ClojureでSlideshareのAPIを叩く。まずはSHA-1求める関数を作る
(import 'java.security.MessageDigest)
(defn sha1 [s]
(let [md (MessageDigest/getInstance "SHA-1")]
(do
(. md update (. s getBytes))
(apply str (map #(format "%02x" %) (. md digest))))))
import java.awt.AWTException;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.InputEvent;
public class Main {
public static void main(String[] args) throws InterruptedException, AWTException {
final Robot robot = new Robot();
@ngsw-taro
ngsw-taro / Main.scala
Last active December 24, 2015 01:38
駅すぱあとのWebAPIを使って駅名しりとり
package com.taroid.ekisample
import scala.collection.mutable
import scala.xml.XML
import java.net.URLEncoder
object Main {
def main(args: Array[String]) {
val shiritori = new Shiritori()
@ngsw-taro
ngsw-taro / OnePersonGame.scala
Last active August 29, 2015 13:56
Scala朝練
package com.taroid.scala.practice
import scala.collection.immutable.Queue
object OnePersonGame {
def main(args: Array[String]) {
import Operations._
test(solve(List()), List())
test(solve(List(5)), List(REMOVE))