Skip to content

Instantly share code, notes, and snippets.

View farooqkz's full-sized avatar

Farooq Karimi Zadeh farooqkz

View GitHub Profile
@farooqkz
farooqkz / eightq.py
Last active October 9, 2022 13:10
Finding a solution for 8 queens problem with a randomized algorithm
from typing import List, Tuple
from pprint import pprint
import random
def empty_slots(board: List[List[int]]) -> List[Tuple[int, int]]:
result = [(x//8, x%8) for x in range(64)]
for i, row in enumerate(board):
for j, slot in enumerate(row):
if slot != 0:
for k in range(8):
@farooqkz
farooqkz / g8.py
Last active September 26, 2022 09:36
Genetic Algorithm for solving 8 queens problem in Python
# Code by Farooq Karimi Zadeh under MIT/X11
import random
from typing import List, Tuple
Fitness = float
class Chromosome:
@farooqkz
farooqkz / noise_removal.py
Created March 23, 2022 17:09
The script remove generates inverted waves from a wave file assuming the wave file is mono channel 8000Hz and 16 bit and little endian
# By Farooq Karimi Zadeh under CC0.
import wave
import struct
from dataclasses import dataclass
from typing import List
@dataclass
class SoundClip16b:
@farooqkz
farooqkz / warshall.rs
Last active May 10, 2021 15:17
Warshall algorithm calculating transitive closure
// Code by Rust beginner, Farooq Karimi Zadeh
// Under CC0 1.0
// Warning! Code might not be idiomatic
fn main() {
let mut bin_matrix = [
[0, 1, 0, 0],
[1, 0, 1, 0],
[0, 0, 0, 1],
[0, 0, 0, 0]
@farooqkz
farooqkz / warshall_for.py
Last active May 10, 2021 15:16
Warshall algorithm calculating transitive closure
"""
Warshall algorithm
This calculates transitive closure for a given binary matrix
Author: Farooq Karimi Zadeh
Code is under CC0 1.0
"""
from pprint import pprint
def pretty_print_matrix(matrix):
class Customer:
account = Account()
name = "Far the chickenkiller"
...
class Account:
no = ... # some big integer
rials = 10000 # 1000 toman
dollars = 1.0 # one dollar only
@farooqkz
farooqkz / clean_html.py
Last active November 6, 2020 19:22
Clean HTML tags attributes(except IMG's src and A's href) and remove DIVs. Good for converting HTMLs to Markdown
# Code by Farooq Karimi Zadeh <fkz@riseup.net>
# Under CC0 1.0
from html.parser import HTMLParser
class MyParser(HTMLParser):
def handle_starttag(self, tag, attrs):
attrs = dict(attrs)
if tag == "img":
src = attrs["src"] if "src" in attrs else ""
import re
import requests
import networkx as nx
import pygraphviz as pgv
MAX_DEPTH = 3
ADDR_PATTERN = re.compile("[a-zA-Z0-9\-]+\.blog\.ir", re.M)
G = nx.DiGraph()
def add_all_links(addr, d=0):
import cherrypy # <3
import time
class Time:
@cherrypy.expose
@cherrypy.tools.caching(delay=30)
def index(self):
return time.ctime(time.time())
@farooqkz
farooqkz / form-based-auth.py
Last active July 28, 2020 11:54
Form based auth with CherryPy using sessions
# Code is by Farooq Karimi Zadeh
# and it is under CC0 which means
# Public Domain and no copyright :)
import cherrypy # <3
@cherrypy.tools.register('before_handler')
def myauth():
sess = cherrypy.session
if sess.get("login?"):
return "Mooo" # It should just return, not important what it returns