Skip to content

Instantly share code, notes, and snippets.

View otykhonruk's full-sized avatar

Oleksandr Tykhonruk otykhonruk

View GitHub Profile
A simple game logic example which demonstrates basic OOP techniques in Python.
import io
import os.path
from PIL import Image
TEMPLATE = """
<!DOCTYPE html>
<html>
<head>

Requirements: Python 3.9+

Running: python3 -m vpm < input.txt

KBD_LAYOUT = {
'en': 'abcdefghijklmnopqrstuvwxyz[];\',.ABCDEFGHIJKLMNOPQRSTUVWXYZ{}:"<>`~@#$^&',
'ru': 'фисвуапршолдьтщзйкыегмцчняхъжэбюФИСВУАПРШОЛДЬТЩЗЙКЫЕГМЦЧНЯХЪЖЭБЮёЁ"№;:?',
'uk': 'фисвуапршолдьтщзйкіегмцчняхїжєбюФИСВУАПРШОЛДЬТЩЗЙКІЕГМЦЧНЯХЇЖЄБЮ\'₴"№;:?'
}
# Cellular automata
# https://natureofcode.com/book/chapter-7-cellular-automata/
def ca(rule, ncells):
def _next_cell(a, b, c):
a <<= 1
a |= b
a <<= 1
a |= c
return (rule & (1 << a)) >> a
def reverse_bits(n):
assert 1 <= n <= 1000000000,\
'argument must be in range [1, 1000000000]'
r = 0
while n:
r <<= 1
r |= n & 1
n >>= 1
return r
def spiral(m, n):
""" Spiral matrix traversal (counterclockwise). """
m -= 1
dx, dy = 0, 1
x, y = 0, -1
while m >= 0 and n >= 0:
for _ in range(m if dx else n):
x += dx
y += dy
@otykhonruk
otykhonruk / rgb.py
Last active November 5, 2018 22:49
from collections import namedtuple
from operator import itemgetter, sub
class rgb:
# https://www.w3.org/TR/REC-html40/types.html#h-6.5
_NAMED_COLORS = {
'black': 0x000000,
'green': 0x008000,
'silver': 0xC0C0C0,
@otykhonruk
otykhonruk / dkd.py
Last active November 10, 2017 16:47
import json
import math
import re
import urllib.parse
from collections import Counter
from difflib import SequenceMatcher
from pprint import pprint
from tldextract import extract as tldextract
from unidecode import unidecode
import this
from random import choice
from django.conf import settings
from django.conf.urls import url
from django.core.management import execute_from_command_line
from django.http import HttpResponse
text = ''.join(this.d.get(c, c) for c in this.s)