Skip to content

Instantly share code, notes, and snippets.

Avatar

Farooq Karimi Zadeh farooqkz

View GitHub Profile
@farooqkz
farooqkz / eightq.py
Last active Oct 9, 2022
Finding a solution for 8 queens problem with a randomized algorithm
View eightq.py
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 Sep 26, 2022
Genetic Algorithm for solving 8 queens problem in Python
View g8.py
# 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 Mar 23, 2022
The script remove generates inverted waves from a wave file assuming the wave file is mono channel 8000Hz and 16 bit and little endian
View noise_removal.py
# 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
Warshall algorithm calculating transitive closure
View warshall.rs
// 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
Warshall algorithm calculating transitive closure
View warshall_for.py
"""
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):
View test0.py
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 Nov 6, 2020
Clean HTML tags attributes(except IMG's src and A's href) and remove DIVs. Good for converting HTMLs to Markdown
View clean_html.py
# 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 ""
View ali.py
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):
View cherrypy-caching-tool.py
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 Jul 28, 2020
Form based auth with CherryPy using sessions
View form-based-auth.py
# 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