All public gists https://gist.github.com/vxgmichel
Copyright 2018, Vincent Michel
MIT License, http://www.opensource.org/licenses/mit-license.php
| import asyncio | |
| try: | |
| import curio | |
| except ImportError: | |
| curio = None | |
| try: | |
| import trio | |
| except ImportError: |
| """Test parallelism in python. | |
| $ \time -f "%e s - %P" python3 par.py 1 > /dev/null | |
| 6.74 s - 100% | |
| $ \time -f "%e s - %P" python3 par.py 4 > /dev/null | |
| 1.87 s - 385% | |
| $ bc -l <<< "6.74/1.87" | |
| 3.60... | |
| """ |
| import asyncio | |
| async def long_sleep(arg): | |
| hours = 60*60 | |
| async def bg(): | |
| while True: | |
| await asyncio.sleep(12 * hours) |
| -- Solve the 'Infinite House of Pancakes' problem. | |
| -- https://code.google.com/codejam/contest/6224486/dashboard#s=p1 | |
| import Text.Printf | |
| -- Generic solver code | |
| main :: IO () | |
| main = interact | |
| $ unlines . cases . map (format . solve) . parse . tail . lines |
| import sys | |
| import time | |
| import hashlib | |
| from fractions import Fraction | |
| from contextlib import contextmanager | |
| from sympy import Matrix | |
| # https://gist.github.com/vxgmichel/080e9999a1020711f27cd60b5c2d14de | |
| from bareiss import adjugate |
| def adjugate(a, m=None): | |
| # Initialize | |
| sign = 1 | |
| previous = pivot = 1 | |
| # Bareiss formula | |
| def do_pivot(a, b, c, d, e): | |
| x = a * d - b * c | |
| if m is None: | |
| q, r = divmod(x, e) |
All public gists https://gist.github.com/vxgmichel
Copyright 2018, Vincent Michel
MIT License, http://www.opensource.org/licenses/mit-license.php
| import bisect | |
| import itertools | |
| def binary_search(f, lower=None, upper=None, precision=1): | |
| # Switch lower and upper | |
| if lower is None and upper is not None: | |
| return precision - binary_search( | |
| lambda x: not f(-x), -upper, None, precision) |
| import sys | |
| import gevent.event | |
| from collections import defaultdict, deque | |
| from PyQt5.QtWidgets import QApplication, QPushButton | |
| from PyQt5.QtCore import QAbstractEventDispatcher, QTimer, QTimerEvent | |
| class QGeventDispatcher(QAbstractEventDispatcher): |
| import asyncio | |
| from itertools import count | |
| from collections import AsyncIterable | |
| async def clock(start=0, step=1, interval=1.): | |
| for i in count(start, step): | |
| yield i | |
| await asyncio.sleep(interval) |