2018-01-01から1年間の記事一覧

Inverse (Udacity)

二文探索の応用で所定のf(x) < y条件下での、xの最大値をとるコード 二文探索で解けるとは思っていたが、yの外に枠を設ける発想ができていなかった。勉強不足か。 def inverse(f, delta=1/1024): def f_1(y): lo, hi = find_bounds(f, y) return binary_sear…

pyhtonの正規表現チェッカー+コード

www.pyregex.com 意外と便利だったので。 import redef findtags1(text): params = '(\w+\s*=\s*"[^"]*"\s*)*' tags = '(<\s*\w+\s*' + params + '\s*/?>)' return re.findall(tags, text)def findtags2(text): m = re.findall(r'<[^\/<>]*[ab].*?>', text) …

Udacity Design of Computer Programsのメモその2

左右対称の文字列を含む文字列を与えると、左右対称の開始位置から終了位置までを切り取ってくれる。 grow関数・・・ある特定の文字を中心に左右対称性があれば、2文字ずつ左右に伸ばして最長の左右対称文字列を取得する関数 def longest_subpalindrome_slic…

Udacity Design of Computer Programsのメモ

Lesson2で出たコードを模写。 fill_in2に'ODD +ODD = EVEN'のような引数を渡して実行すると、適当に数字を埋めて'3+3 = 6'のような文字列を返してくれる。 permutationで組み合わせの数を出せるのは、かなり便利だなと思った。 import reimport itertoolsimp…