Skip to content

Instantly share code, notes, and snippets.

View frodo821's full-sized avatar
:octocat:
Working from home

frodo821

:octocat:
Working from home
View GitHub Profile
@frodo821
frodo821 / distribution.ipynb
Last active April 9, 2021 08:32
分布の可視化
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@frodo821
frodo821 / propositions.py
Last active April 15, 2021 12:41
真理計算ができるだけのスクリプト
from abc import ABC, abstractmethod
class Proposition(ABC):
def __and__(self, other):
return CompositeProposition('and', self, other)
def __or__(self, other):
return CompositeProposition('or', self, other)
\documentclass[a4paper,11pt]{jsarticle}
\usepackage{amsmath,amsfonts}
\usepackage{bm}
\usepackage[dvipdfmx]{graphicx}
\usepackage{amsthm}
\usepackage{ascmac}
\newtheorem*{definition}{定義}
\newtheorem*{axiom}{公理}
\newtheorem*{theorem}{定理}
@frodo821
frodo821 / love.py
Created September 7, 2020 18:16
コンピュータに愛を囁いてもらえる……かもしれない
# コンピュータに愛を囁いてもらえる……かもしれない
def formula(x, y):
return x**2 + (y - (x**2)**(1/3))**2
def draw_char(x, y, point='*', space=' '):
return point if abs(formula(x, y) - 12) < 2 else space
def draw():
print('\n'.join(''.join(draw_char((y - 20) / 5, (x - 15) / 2) for y in range(0, 40)) for x in range(0, 30)[::-1]))
@frodo821
frodo821 / abstract_mapping.py
Created April 28, 2020 10:44
The minimal structure of mapping type in python
from abc import ABC, abstractmethod as abstract
class AbstractMapping(ABC):
@abstract
def __iter__(self):
"""
return an iterator or iterable denotes mapping keys
"""
yield from []
@frodo821
frodo821 / currying.py
Last active April 28, 2020 08:33
currying most of python functions, methods, built-in functions
from functools import wraps
from inspect import signature
def carry(func):
"""
carrying most of functions, methods or built-in functions.
"""
params = [*signature(func).parameters.items()]
if any(map(lambda x: x[1].kind&1 == 0, params)):
raise ValueError('Undeterminable length of varargs was declared.')
@frodo821
frodo821 / monteCarlo.js
Last active April 24, 2020 08:52
モンテカルロ法で円周率を求める
function* monteCarlo() {
let it = 0;
let prev = 0;
while (true) {
prev = prev * it / (it + 1) + (Math.random() ** 2 + Math.random() ** 2 < 1 ? 1 / (it + 1) : 0);
it++;
const res = prev * 4;
yield { current: res, iterations: it, diff: Math.PI - res };
}
}
interface Conditional<T> {
then: (expr: () => T) => Conditional<T>;
fail: (expr: () => T) => Conditional<T>;
result: () => T | undefined;
}
export default function cond<T>(condition: boolean): Conditional<T> {
let res: T | undefined;
return {
@frodo821
frodo821 / 1.Proof.v
Last active November 28, 2020 05:24
Proof Driven Development sample
Require Import List.
Import ListNotations.
Fixpoint reversed { A: Type } (xs: list A) :=
match xs with
| nil => nil
| x :: xs' => reversed xs' ++ [x]
end.
Lemma reversed_sub:
@frodo821
frodo821 / email_validator.py
Last active October 18, 2021 21:46
A ReDoS proof email address validation
from re import match
def validateEmail(email):
valid = 'abcdefghijklmnopqrstuvwxyz1234567890!#$%&\'*+-/=?^_`{}|~'
if not email:
return False
email = email.lower()
if email.startswith('"'):
i = 1
while i < min(64, len(email)):