コメントの削除
こう、Haskell の練習というか HUnit の使い方確認というか。
import Test.HUnit removeComment :: String -> String removeComment = outOfComment where outOfComment ('/':'*':cs) = inComment cs outOfComment (c:cs) = c : outOfComment cs outOfComment [] = [] inComment ('*':'/':cs) = outOfComment cs inComment (c:cs) = inComment cs inComment [] = [] testData = [ t "AAA" "AAA", t "AAA/*BBB*/" "AAA", t "AAA/*BBB" "AAA", t "AAA/*BBB*/CCC" "AAACCC", t "AAA/*BBB/*CCC*/DDD*/EEE" "AAADDD*/EEE", t "AAA/a//*BB*B**/CCC" "AAA/a/CCC" ] where t input expected = expected ~=? removeComment input main :: IO Counts main = runTestTT (test testData)
uniq
ちょいとトリッキーなのと素朴で遅い奴。
def uniq(iterable): """ >>> uniq([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9]) [3, 1, 4, 5, 9, 2, 6, 8, 7] """ s = set() return [s.add(i) or i for i in iterable if i not in s] def uniq2(ls): """ >>> uniq2([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9]) [3, 1, 4, 5, 9, 2, 6, 8, 7] """ return [ls[i] for i in xrange(len(ls)) if ls[i] not in ls[:i]] def _test(): import doctest doctest.testmod() if __name__ == '__main__': _test()