This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sample = [5, 3, 1, 2, 4] | |
n = len(sample) | |
mean = sum(sample)/n | |
median = (sample[n//2 - (n + 1)%2] + sample[n//2])/2 | |
stddev = (sum((e - mean)**2 for e in sample)/n)**0.5 | |
cnt = {} | |
for e in sample: | |
cnt[e] = cnt.get(e, 0) | |
cnt[e] += 1 | |
mode = sorted(cnt.keys(), key=lambda e: (cnt[e], -e))[-1] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from asyncio import coroutine, sleep | |
from aiohttp import web | |
@coroutine | |
def handle(request): | |
seconds = float(request.match_info.get('seconds', 1)) | |
yield from sleep(seconds) | |
text = "You've been waiting for {} seconds".format(seconds) | |
return web.Response(body=text.encode('utf-8')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" Inspired by http://flask.pocoo.org/snippets/40/ """ | |
app = Flask(__name__) | |
@app.url_defaults | |
def hashed_url_for_static_file(endpoint, values): | |
if 'static' == endpoint or endpoint.endswith('.static'): | |
filename = values.get('filename') | |
if filename: | |
if '.' in endpoint: # has higher priority |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; pen test on functional programming | |
(define (min-2 a b) (if (> a b) b a)) | |
(define (min-3 a b c) (min-2 (min-2 a b) (min-2 b c))) | |
(define (sq x) (* x x)) | |
(define (foo a b c) (cond ((= a (min-3 a b c)) (+ (sq b) (sq c))) | |
((= b (min-3 a b c)) (+ (sq a) (sq c))) | |
(else (+ (sq a) (sq b))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
/** | |
* Пример использования socket.io namespaces в сервисе AngularJS. | |
* | |
* Цель: получить несколько отдельных каналов связи в рамках одного соединения. | |
* | |
* Проблема: так как сервисы в AngularJS являются синглтонами, не возможно | |
* получить несколько экземпляров сервиса Socket с разными значениями | |
* namespace. При этом хочется иметь отдельные сервисы, представляющие |
NewerOlder