Skip to content

Instantly share code, notes, and snippets.

View mebusw's full-sized avatar

Jacky Shen mebusw

View GitHub Profile
@mebusw
mebusw / snow.cpp
Created May 25, 2017 05:58
输入是结点力,输出是位移
#include "include.hpp"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using std::cout;
using std::endl;
using std::string;
using std::ifstream;
using std::ofstream;
import scrapy
class CsxCourseSpider(scrapy.Spider):
name = "csx"
start_urls = ['https://www.scrumalliance.org/courses-events/course.aspx?pageCount=50&country=&state=&city=&zip=&type=Csd;&trainer=&language=&startdate=5/15/2017%2012:00:00%20AM&enddate=1/1/1900%2012:00:00%20AM&discount=False&page=1&orderby=StartDate&sortdir=asc&radius=0&view=map']
# https://www.scrumalliance.org/courses-events/courses/csd/us/ohio/columbus/2017/may/201702145-csd
def parse(self, response):
# print '}}}}}}}}', response.url, response.xpath('*//tr')
@mebusw
mebusw / scrapy_books.py
Last active May 15, 2017 13:02
using scrapy to crawl http://books.toscrape.com/ interatively
import scrapy
class BookSpider(scrapy.Spider):
name = "books"
start_urls = ['http://books.toscrape.com/']
def parse(self, response):
for book in response.css('article.product_pod'):
name = book.xpath('./h3/a/@title').extract_first()
price = book.css('p.price_color::text').extract_first()
@mebusw
mebusw / np-ai.py
Created May 1, 2017 09:54
Linear Unit of neural network with numpy
#!/usr/local/bin/python
# -*- coding: UTF-8 -*-
import numpy as np
import matplotlib.pyplot as plt
def predict(input_vec, f):
# print (input_vec, W)
return f(np.dot(input_vec, W))
def balance_recurse(chars, cnt=0):
if len(chars) == 0: return cnt == 0
if chars[0] == '(': cnt += 1
if chars[0] == ')': cnt -= 1
return cnt >= 0 and balance(chars[1:], cnt)
PARENS = (('(', ')'), ('[', ']'),)
def balance_recurse_mul(chars, cnt=[0]*len(PARENS)):
if len(chars) == 0: return all(map(lambda b: b==0, cnt))
for i, paren in enumerate(PARENS):
@mebusw
mebusw / Monte-Carlo-estimation.py
Created December 18, 2016 14:08
given a team can finish 1~6 feature of same size per day, then how many days can you commit to finish 20 features?
import random
TIMES = 10000
def once(acc=0, days=0):
return days if acc >= 20 else once(acc + random.choice(xrange(1,7)), days + 1)
s1 = [once() for i in xrange(TIMES)]
s1.sort()
#! /usr/local/bin/python
# -*- coding: utf-8 -*-
import subprocess, time
# 程序执行终止时间为当前时刻延迟N秒
stoptime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()+60))
def main(command):
popen = subprocess.Popen(
@mebusw
mebusw / prime_factor.py
Created August 19, 2016 13:47
functional programming for Prime Factor
# https://codingstyle.cn/topics/202
def f(n, p=2):
if n < 2: return []
if n % p == 0: return [p] + f(n/p, p)
return f(n, p+1)
assert [2,2,3,3,3,5,5,19] == f(2*2*3*3*3*5*5*19)
@mebusw
mebusw / primes.py
Last active August 20, 2016 06:45
functional programming for Primes, inspired by Haskell example
# !encoding=utf8
# primes = filterPrime [2..]
# where filterPrime (p:xs) =
# p : filterPrime [x | x <- xs, x `mod` p /= 0]
# Infinite List
from pipe import *
from pipe import itertools
def primes(xs):
#! encoding=utf8
import unittest
def maxProfit(prices):
if not prices:
return 0
currBuy = -prices[0] #表示当天最终未持股的情况下,当天结束后的累计最大利润
currSell = 0 #表示当天最终持股的情况下,当天结束后的累计最大利润