Skip to content

Instantly share code, notes, and snippets.

@aeg
aeg / yyyymmdd.py
Last active November 18, 2021 02:16
Python で yyymmdd_hhMMss みたいな文字列を作る。
from datetime import datetime
datestr = {0:%Y%m%d_%H%M%S}'.format(datetime.now())
@aeg
aeg / sorted_by_time_list
Created August 25, 2020 13:41
ある拡張子のファイルを日付順でソートする。
import os
csv_dir='./'
l:list = list(filter(lambda x: x.endswith('.py'), os.listdir(path=csv_dir)))
l.sort (key=lambda x: int(os.path.getctime(csv_dir + '/' + x)),
reverse=True)
print(l)
@aeg
aeg / CloseTest.java
Last active October 25, 2015 17:07
AutoCloseable のテスト
public class CloseTest {
public static void main(String... args) {
try (TestResource rs = new TestResource()){
System.out.println("try:start");
throw(new Exception());
} catch (Exception e) {
System.out.println("catch:start");
e.printStackTrace();
System.out.println("catch:end");
} finally {
@aeg
aeg / CaclFibo.groovy
Last active August 29, 2015 13:56
@memoized について
@Memoized
def fibo(n) {
def result = (n == 0 || n == 1) ? 1G : fibo(n - 2) + fibo(n - 1)
println "$n: $result"
result
}
println "result:" + fibo(5)
@aeg
aeg / RepeatString.groovy
Last active December 28, 2015 03:19
文字列を繰り返す
// Case #1
// 文字列を繰り返すときには * 演算子を使う。繰り返す回数を指定します。
def s = 'Go! '
assert s * 3 == 'Go! Go! Go! '
// Case #2
// 当たり前ですが、繰り返し回数の指定は変数も使えます
@aeg
aeg / MultiLine.groovy
Created November 12, 2013 16:11
複数行の文字列を取り扱う
// Case #1
// 複数行の文字列(改行を含んだ文字列)を定義するには"""を使う。
def name = "文字列"
def str = """こんにちは
${name}さん
複数行に渡る
文字列です。"""
@aeg
aeg / padLeft.groovy
Created October 14, 2013 09:08
0埋めの文字列として出力する
def str="1"
// Case #1
// 0埋めの文字列として出力する
assert "001"==str.padLeft(3, "0")
// Case #2
// 空白文字で左埋めするには(埋める文字列を指定しないと空白になる)
assert " 1"==str.padLeft(3)
@aeg
aeg / RightFunction.groovy
Created May 11, 2013 17:53
文字列の末尾から何文字目までを取り出す
// Case #1
// 文字列の末尾から何文字目までを取り出す(BasicのRight関数の動き)
def rightStr(String string, int cutLength) {
string.substring(string.length() - cutLength)
}
assert rightStr("12345678",3) == "678"
// Case #2
// 文字列の末尾から何文字目までを取り出す。Stringにメソッド追加。
@aeg
aeg / LeftFunction.groovy
Created May 11, 2013 17:51
文字列の先頭から何文字目までを取り出す
// Case #1
// 文字列の先頭から何文字目までを取り出す(BasicのLEFT関数の動き)
def leftStr(String string, int length) {
string.substring(0, length)
}
assert leftStr("12345678",3) == "123"
// Case #2
@aeg
aeg / CalcFactorial.groovy
Created May 11, 2013 17:31
階乗を計算する
// Case #1
// 階乗(n!)を計算する(再帰バージョン)
def BigInteger fact(n) {
if (n == 0) return 1
return n * fact(n - 1)
}
// 4! = 24
assert fact(4) == 24