Skip to content

Instantly share code, notes, and snippets.

@kwatch
kwatch / index_by.py
Created August 14, 2014 08:16
問題2: シーケンスseqと関数keyを受け取り、要素を値とする辞書を返すような関数 index_by(seq, key) を定義してください。
# -*- coding: utf-8 -*-
##
## 問題2: シーケンスseqと関数keyを受け取り、要素を値とする辞書を返すような関数 index_by(seq, key) を定義してください。
## 例:
## seq = [ {'id': 1, 'name': "Haruhi", 'gender': "F"},
## {'id': 2, 'name': "Mikuru", 'gender': "F"},
## {'id': 3, 'name': "Yuki", 'gender': "F"},
## {'id': 4, 'name': "Ituki", 'gender': "M"},
## {'id': 5, 'name': "Kyon", 'gender': "M"},
@kwatch
kwatch / group_by.py
Created August 14, 2014 08:24
問題3: シーケンスseqと関数keyを受け取り、要素のリストを値とするような辞書を返すような関数 group_by(seq, key) を定義してください。
# -*- coding: utf-8 -*-
##
## 問題3: シーケンスseqと関数keyを受け取り、要素のリストを値とするような辞書を返すような関数 group_by(seq, key) を定義してください。
## 例:
## seq = [ {'id': 1, 'name': "Haruhi", 'gender': "F"},
## {'id': 2, 'name': "Mikuru", 'gender': "F"},
## {'id': 3, 'name': "Yuki", 'gender': "F"},
## {'id': 4, 'name': "Ituki", 'gender': "M"},
## {'id': 5, 'name': "Kyon", 'gender': "M"},
@kwatch
kwatch / flatten.py
Created August 14, 2014 09:15
問題4:入れ子になっているリストを、入れ子を外すような関数 flatten(arr) を定義してください。
# -*- coding: utf-8 -*-
##
## 問題4:入れ子になっているリストを、入れ子を外すような関数 flatten(arr) を定義してください。
## 例:
## nested = [1, [2, [3, 4], 5, [6]]]
## arr = flatten(nested)
## print(arr)
## #=> [1, 2, 3, 4, 5, 6]
##
@kwatch
kwatch / indented.py
Last active August 29, 2015 14:05
問題5:入れ子になっているリストを、インデント幅2の文字列に変換する関数 indented(arr, width=2) を定義してください。
# -*- coding: utf-8 -*-
##
## 問題5:入れ子になっているリストを、インデント幅2の文字列に変換する関数 indented(arr, width=2) を定義してください。
## 例:
## nested = [1, [2, [3, 4], 5, [6]]]
## string = indented(nested)
## print(string)
## #=> 1
## # 2
@kwatch
kwatch / entity-role-example.js
Last active August 29, 2015 14:05
EntityクラスとRoleクラス
function Company(name, addr) { // Entityクラス
this.name = name;
this.addr = addr;
//
this._entity = this; // 姑息な方法
}
function Supplier(company) { // Roleクラス
this.__proto__ = company;
this.products = []; // 商品一覧 (Role固有のデータ)
@kwatch
kwatch / value-object-example.js
Last active August 29, 2015 14:05
数値や文字列をラップしたValueクラスを作るサンプル
function Length(value, uom) {
this.value = value;
this.uom = uom || "cm";
}
function Square(length, uom) {
this.length = new Length(length, uom);
}
function Colored(figure, r, g, b) {
@kwatch
kwatch / config-example.js
Created August 20, 2014 11:52
__proto__ を活用した、環境別設定オブジェクト
// -*- coding: utf-8 -*-
// ベースとなる設定
var config_common = {
name: 'My Application',
mode: null,
secret: null,
db: {
host: 'localhost',
port: 9876,
@kwatch
kwatch / bench1.py
Last active August 29, 2015 14:06
str.startswith() と re.match() のベンチマーク
# -*- coding: utf-8 -*-
import sys, re
from benchmarker import Benchmarker
try:
xrange
except:
xrange = range
@kwatch
kwatch / bench2.py
Created September 14, 2014 06:52
str.endswith() と re.search() のベンチマーク
# -*- coding: utf-8 -*-
import sys, re
from benchmarker import Benchmarker
try:
xrange
except:
xrange = range
@kwatch
kwatch / bench3.py
Created September 14, 2014 06:53
str.isdigit() と re.match() のベンチマーク
# -*- coding: utf-8 -*-
import sys, re
from benchmarker import Benchmarker
try:
xrange
except:
xrange = range