Skip to content

Instantly share code, notes, and snippets.

View suspectpart's full-sized avatar
🐍
Python for Dummies - Out now!

Horst Schneider suspectpart

🐍
Python for Dummies - Out now!
  • Universität Heidelberg, Germany
  • Mannheim
View GitHub Profile
@suspectpart
suspectpart / pdf_forms.py
Created April 5, 2017 07:55
FDF and XFDF generator to populate forms on a PDF file
#!/usr/bin/env python
# -*- coding: utf-8 -*-
class XFDF(object):
"""Generate XFDF files to populate PDF forms with data.
Form data on PDF files can be populated by using one of these formats:
- FDF (File Data Format)
- XFDF (XML Forms Data Format)
XFDF is an XML format and utf-8 by default. Prefer this to FDF if possible.
@suspectpart
suspectpart / validate.py
Created February 19, 2018 16:21
Enforce nonsensical password strength rules
#!/usr/bin/env python3
import unittest
"""Valid passwords
- are between 8 and 20 characters long
- contain at least one number
- have at least one uppercase and one lowercase character
- have a special char of !"§$%&/()=?
"""
@suspectpart
suspectpart / validate.py
Created June 6, 2018 10:35
Python Password Validation
#!/usr/bin/env python3
import unittest
"""Valid passwords
- are between 8 and 20 characters long
- contain at least one number
- have at least one uppercase and one lowercase character
- have a special char of !"§$%&/()=?
"""
import unittest
class Lookahead(object):
def __init__(self, things, count):
self.things = tuple(things)
self.count = count
self.windows = len(things) - count + 1
def __iter__(self):
return (self.things[i:i+self.count] for i in range(self.windows))
@suspectpart
suspectpart / aoc_2018_day_2.py
Last active December 3, 2018 01:10
Advent of Code 2018, Day 2
import difflib
import itertools
import string
def check_sum(box_ids):
"""Advent of Code 2018, Day 2, Part I"""
counts = [Counter(id_).values() for id_ in box_ids]
twos = sum(2 in c for c in counts)
@suspectpart
suspectpart / aoc_2018_day_3.py
Created December 3, 2018 16:44
Advent of Code 2018, Day 3
import fileinput
import re
from collections import defaultdict
from itertools import product
class Fabric:
def __init__(self):
self._claimed = defaultdict(int)
@suspectpart
suspectpart / bst.py
Last active December 13, 2018 22:51
Binary Search Tree
import unittest
from abc import ABC, abstractmethod
class DuplicateKeyException(Exception):
def __init__(self, key):
msg = f"Key {key} is already taken"
super(DuplicateKeyException, self).__init__(msg)
const { performance } = require('perf_hooks');
// LIZENZ: Gehört Horst Schneider
function Statistics(X) {
function Percentile(X, percent) {
this.value = function () {
const k = (X.length - 1) * percent;
const lower = Math.floor(k);
@suspectpart
suspectpart / parse.js
Last active February 18, 2019 16:38
Parse numbers in JS
const assert = require('assert');
describe('Parse', () => {
it('strict', function () {
// these are fine
assert.strictEqual(parseStrict(0), 0);
assert.strictEqual(parseStrict(-0), -0);
assert.strictEqual(parseStrict(1), 1);
assert.strictEqual(parseStrict(3.14159), 3.14159);
assert.strictEqual(parseStrict(-3.14159), -3.14159);
@suspectpart
suspectpart / gen.js
Created February 6, 2019 14:21
python-like generators in javascript
function* imap(iterable, func) {
for (item of iterable) {
yield func(item);
}
}
function* ifilter(iterable, predicate) {
for (item of iterable) {
if(predicate(item)) {
yield item;