Skip to content

Instantly share code, notes, and snippets.

Avatar

Per Persson md2perpe

View GitHub Profile
View prime_iterator.py
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))
View sum_big_and_small_numbers.py
BIG = 1_000_000.0
SMALL = 1e-12
COUNT = 1_000_000
def big_first():
yield BIG
for _ in range(COUNT):
yield SMALL
View ham_dis.py
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
View main.py
' ** 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']
View a_not_so_magic_square.c
#include <stdio.h>
typedef int square;
typedef int row;
typedef int col;
int is_valid_row(row n)
{
return (
n == 0b0011 ||
View pi_sequence.hs
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))
View monads.ts
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 Feb 11, 2021
Prime number generator in Python
View primes.py
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 Jan 31, 2021
Prepare commit message for merge
View prepare-commit-msg
#!/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"):