Skip to content

Instantly share code, notes, and snippets.

View NimaBavari's full-sized avatar
🏠
Working from home

NimaBavari NimaBavari

🏠
Working from home
View GitHub Profile
@NimaBavari
NimaBavari / pattern_parser.py
Last active February 23, 2023 20:32
Parses a pattern and checks if the given string matches that pattern
def is_matching(cand):
"""Parses a composite `cand` and check if the string matches
the pattern. E.g.,
cand = "++$*+*{2} xm4cccdvv" -> True.
`cand` is a pattern and a string separated by a space. Each "+"
in the pattern means an alphabetic character in the string; each
"$" means a numeric character (1-9); each "*" means an alphabetic
character repeated N times with N >= 1 if it is followed by "{N}"
and otherwise an alphabetic character repeated 3 times.
"""
@NimaBavari
NimaBavari / credibility_index.sql
Last active November 22, 2022 18:03
How to Determine the Credibility of "Authorities" in Social Media
/* Credibility Index
My friend Amin asked me if it was possible to find how good an opinion leader
any online person is. It is!
I came up with this algorithm: for each (user, context) grouping, find the mean
popularity of that grouping. Using this, compute a credibility score. Then, for
each user, find the minimum of the credibility scores across all contexts for
that user and assign it as his/her final credibility score (since each chain is
as strong as its weakest link).
@NimaBavari
NimaBavari / collatz.py
Created December 6, 2021 02:04
Collatz sequence generator given a starting number
def collatz_gen(start):
yield start
while start != 1:
if start % 2 == 0:
start //= 2
else:
start = 3 * start + 1
yield start
for item in collatz_gen(27):
# simpler case of decorators
# memoization with decorator
def memoize(func):
cache = {}
def wrapper(*args, **kwargs):
if args not in cache:
cache[args] = func(*args, **kwargs)
return cache[args]
class It:
"""An example of iterators."""
def __init__(self, max_, start=1, increment=1):
self.max_ = max_
self.start = start
self.increment = increment
self.counter = self.start - self.increment
def __iter__(self):
def gen_fun(num):
counter = 1
while counter <= num:
yield counter
counter += 1
for n in gen_func(100):
print(n)
const frame = document.querySelector('#frame'); // 10 sheklin divlerini saxlayan cherchive
const images = [
// 30 dene shekil
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30
]; // butun shekiller
let imagesOnScreen = images.slice(0, 10); // ekrandaki shekiller, ilk 10 denesi initialize olunur
let timeInterval = 1000 + Math.random() * 4000; // 1 saniye ile 5 saniye arasinda bir random interval
const locateImagesInDivs = () => {
// imagesOnScreen-deki shekilleri #frame-in ichindeki div-lerin ichine yig
@NimaBavari
NimaBavari / cheque_amount_reader.py
Last active July 19, 2019 21:38
A Python app that converts amount on bank cheque to text format
"""Reads the amount on a cheque as a string."""
num_sing = ['', 'one ', 'two ', 'three ', 'four ', 'five ', 'six ', 'seven ',
'eight ', 'nine ', 'ten ', 'eleven ', 'twelve ', 'thirteen ',
'fourteen ', 'fifteen ', 'sixteen ', 'seventeen ', 'eighteen ',
'nineteen ']
num_tens = ['', 'twenty ', 'thirty ', 'forty ', 'fifty ', 'sixty ', 'seventy ',
'eighty ', 'ninety ']