Skip to content

Instantly share code, notes, and snippets.

View md2perpe's full-sized avatar

Per Persson md2perpe

View GitHub Profile
@md2perpe
md2perpe / prime_iterator.py
Last active April 17, 2022 18:27
Prime iterator
import itertools
from typing import Iterator
def sift(ns: Iterator[int]) -> Iterator[int]:
p = next(ns)
yield p
yield from sift(filter(lambda n: n%p != 0, ns))
def primes():
yield from sift(itertools.count(2))
@md2perpe
md2perpe / sum_big_and_small_numbers.py
Created February 28, 2022 11:23
Sum big and small numbers
BIG = 1_000_000.0
SMALL = 1e-12
COUNT = 1_000_000
def big_first():
yield BIG
for _ in range(COUNT):
yield SMALL
words = ['peace','piece','geese','tease','spoon']
def ham_dis(word1, word2):
return sum(c1 != c2 for c1, c2 in zip(word1, word2))
output = [
[ 'Ham-Dis', *words ],
*(
[ w1, *(ham_dis(w1, w2) for w2 in words) ]
for w1 in words
' ** PYTHON CODE CHALLENGE - SCHOOL ADMISSIONS ** '
# write code to handle admissions for a school - print results
# rules: pass test and interview. If applicant is legacy
# passing either test or interview is enough to be admitted
# your output should be sorted
# output = 'Accepted students: An, Bo, Mo, My, Xi'
appl =['Jay','Sam','Vi','Li','My','Xi','On','Mo','An','Bo']
test_ok =['xi','my','sam','an','mo','on']
#include <stdio.h>
typedef int square;
typedef int row;
typedef int col;
int is_valid_row(row n)
{
return (
n == 0b0011 ||
@md2perpe
md2perpe / pi_sequence.hs
Created March 20, 2021 22:40
Pi sequence
t :: Integer -> Float
t 0 = 0
t n = sqrt(2 + t(n-1))
p :: Integer -> Float
p n = 2**(n+1) * sqrt(2 - t(n))
@md2perpe
md2perpe / monads.ts
Created February 21, 2021 22:26
Monads
interface Thenable<a> {
then<b>(cont: (_: a) => Thenable<b>): Thenable<b>;
}
/*
run_slow_task.then((result) => {
do_something(result).then((new_result) => {
do_something_else(new_result)
})
})
@md2perpe
md2perpe / primes.py
Created February 11, 2021 22:07
Prime number generator in Python
import itertools
def primes():
def sieve(numbers):
try:
n = next(numbers)
yield n
yield from sieve(filter(lambda m: m%n!=0, numbers))
except StopIteration:
@md2perpe
md2perpe / prepare-commit-msg
Created January 31, 2021 14:06
Prepare commit message for merge
#!/usr/bin/env python3
import sys
import fileinput
import re
if sys.argv[2] == "merge":
with fileinput.input(sys.argv[1], inplace=True) as f:
for line in f:
if line.startswith("Merge branch"):