Skip to content

Instantly share code, notes, and snippets.

View yaotti's full-sized avatar

Hiroshige Umino yaotti

View GitHub Profile
test for gisty
# -*- coding: utf-8 -*-
def rotate_arr(arr, t):
"""programming peals
rotate array algorithm(destructive)"""
l = len(arr)
tmp = arr[0]
k = 0
while True:
j = k
@yaotti
yaotti / 3-otedama.py
Created October 19, 2009 11:59
Programming Pearls section 2
# -*- coding: utf-8 -*-
def rotate_arr(arr, t):
"""programming peals
rotate array algorithm(destructive)"""
l = len(arr)
tmp = arr[0]
k = 0
while True:
j = k
#!/usr/bin/env python
from time import sleep
def do_heavy_process():
sleep(3)
return 10
class myclass():
def __init__(self):
self.__val = None
#!/usr/bin/env python
# coding: utf8
def hanoi(n, from_, to, via):
"tower of hanoi"
if n == 1:
print "move %d: %s => %s" % (n, from_, to)
else:
hanoi(n-1, from_, via, to)
print "move %d: %s => %s" % (n, from_, to)
hanoi(n-1, via, to, from_)
#!/usr/bin/env python
# coding: utf8
def comb(n, r):
"calc combination"
if n == 0 or r == 0: return 1
# return (n - r + 1) / r * comb(n, r - 1)
# 先にrで割ると,小数点を切り捨ててしまう
return comb(n, r - 1) * (n - r + 1) / r
#print comb(10, 2) # 45
;; はてな記法のリスト(-)をいい感じに挿入してくれる
;; C-lを押すたびに深いリストになっていく
(defun insert-list()
(interactive)
(let* ((end-point (point))
(start-point (- end-point 2))
(str (buffer-substring start-point end-point)))
(if (string-equal "- " str)
(delete-backward-char 1))
(insert "- ")))
(* fibonacci *)
let fib(x:int) =
let rec fib_i(n, a, b) =
if n = 1 || n = 2 then a
else fib_i(n-1, a+b, a)
in fib_i(x, 1, 1);;
(* 実行:
# #trace fib;;
fib is now traced.
# fib 4;;
#!/usr/bin/env python
# coding: utf-8
def _upheap(heap, x):
heap += [x]
l = len(heap) - 1 # 挿入する要素のindex
while (l-1)/2 >= 0: # 親がいる場合
if heap[(l-1)/2] > heap[l]:
heap[(l-1)/2], heap[l] = heap[l], heap[(l-1)/2]
l = (l-1)/2
type student = { name: string; id: int };;
type hoge = { name: string; age: int };;
{ name = "hi"; id = 12 };;
(*
# type student = { name: string; id: int };;
type student = { name : string; id : int; }
# type hoge = { name: string; age: int };;
type hoge = { name : string; age : int; }
# { name = "hi"; id = 12 };;