Skip to content

Instantly share code, notes, and snippets.

View toaco's full-sized avatar
🎯
Focusing

toaco toaco

🎯
Focusing
View GitHub Profile
@toaco
toaco / caculator.js
Last active March 28, 2020 15:28
A simple four arithmetic operations implementation
const Decimal = require('decimal.js');
// 计算表达式的值
function calculate(expression) {
const words = splitExpression(expression);
const outputs = [];
const signs = [];
while (words.length) {
const word = words.shift();
@toaco
toaco / solution.py
Last active February 15, 2019 01:36
Flight Ticket Booking Problem
# -*- coding: utf-8 -*-
import datetime
from collections import defaultdict
FLIGHTS = {
"chengdu_to_xian": {
"reward": [
{'name': 'GD2501', 'time': '08:00', 'price': 500, 'at_weekends': True},
{'name': 'GD2501', 'time': '08:00', 'price': 800, 'at_weekends': False},
{'name': 'GD2606', 'time': '12:25', 'price': 500, 'at_weekends': True},
@toaco
toaco / README.md
Last active April 4, 2018 07:30
rubric设计

功能

  • 对象转换器,提供load_xxx/dump_xxx方法
  • 对象验证器,定义一个模式,判断一个Python对象是否符合该模式. validate
  • 对象生成器,generate
  • 提供其他库的插件:
    • flask/django等Web框架的表单验证插件
    • pymongo 扩展

对象验证

目标在于提供Pythonic的对象验证方式,如:

@toaco
toaco / demo.py
Created February 8, 2018 01:45
计算某天所属区间
import datetime
import arrow
def diff_month(d1, d2):
return (d1.year - d2.year) * 12 + d1.month - d2.month
def calc_current_period(start_date, period, now=None, unit='months'):
@toaco
toaco / demo.py
Created January 30, 2018 02:31
导入某个路径下的所有模块
import importlib
import pkgutil
for loader, name, is_pkg in pkgutil.walk_packages(__path__):
importlib.import_module('.{}'.format(name), 'models')
@toaco
toaco / draft.py
Last active February 5, 2018 03:49
年阶梯气价计算方式
# encoding=utf8
from decimal import Decimal
def create_ladders(price_name, last_reading, current_reading, price):
""" 目前不考虑优惠但是可以合计金额里面体现出来
ladder['price_name'] = price_name
ladder['last_reading'] = last_reading
ladder['current_reading'] = current_reading
ladder['gas'] = current_reading - last_reading
@toaco
toaco / wfc.sql
Last active December 15, 2017 07:26
wfc
-- 条件为IN的时候是51条,条件为Not In的时候应该是7条结果是0
SELECT
DISTINCT CH || QH AREACODE
FROM PTB_YHZL
-- 58 rows
WHERE PTB_YHZL.CH || QH NOT IN (
SELECT PTB_DQ.CH || QH
FROM PTB_FCXX
LEFT JOIN ptb_dq ON PTB_FCXX.CH = PTB_DQ.CH
-- 53 rows
@toaco
toaco / demo.sql
Last active December 15, 2017 03:14
Oracle9i - Why this left join return empty result ?
CREATE TABLE TABLE_A
(
AID NUMBER(10) NOT NULL PRIMARY KEY
);
CREATE TABLE TABLE_B
(
BID NUMBER(12) NOT NULL PRIMARY KEY,
AID NUMBER(10) NOT NULL REFERENCES TABLE_A (AID)
);
@toaco
toaco / sp.py
Created December 4, 2017 07:44
判断国内电话号码运营商,将多个电话号码按照运营商分组
import collections
class groupby2(object):
"""
[k for k, g in groupby2('AAAABBBCCDAABBB')] --> A B C D
"""
def __init__(self, iterable, key=lambda x: x):
self.key_func = key
@toaco
toaco / error
Created November 29, 2017 09:46
python2.7: free(): corrupted unsorted chunks: 0x00000000036437e0
*** Error in `/home/jeffrey/workspace/romulan-env/bin/python2.7': free(): corrupted unsorted chunks: 0x00000000036437e0 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7fcf86e667e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7fcf86e6f37a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7fcf86e7353c]
/home/jeffrey/workspace/romulan-env/bin/python2.7[0x509019]
/home/jeffrey/workspace/romulan-env/bin/python2.7[0x4b5244]
/home/jeffrey/workspace/romulan-env/bin/python2.7[0x4a6359]
/home/jeffrey/workspace/romulan-env/local/lib/python2.7/site-packages/pymssql.so(+0x20cb2)[0x7fcf738a9cb2]
/home/jeffrey/workspace/romulan-env/bin/python2.7(PyEval_EvalFrameEx+0x5780)[0x4c9780]