Skip to content

Instantly share code, notes, and snippets.

View sina-programer's full-sized avatar
🎯
Focusing

Sina.F sina-programer

🎯
Focusing
View GitHub Profile
@sina-programer
sina-programer / rock-paper-scissors.py
Created July 9, 2025 06:18
new mathematically implementation of classic rock-paper-scissors game by a math engine which just compares numbers without several conditions. (the greater number is the winner, so simple)
def convert(value):
assert isinstance(value, str)
value = value.lower()
if value == 'rock':
return 0
elif value == 'paper':
return 1
elif value == 'scissors':
return 2
return -1
@sina-programer
sina-programer / perfect_cube.py
Created July 9, 2025 05:45
create a perfect cube shape in any dimensions easily by this python function
def perfect_cube(n):
return [
tuple(map(int, row[2:].rjust(n, '0')))
for row in map(bin, range(2**n))
]
if __name__ == "__main__":
from pprint import pprint
@sina-programer
sina-programer / mean.py
Created July 2, 2025 21:16
Diverse mean functions implemented in python by numpy. (arithmetic, geometric, harmonic, generalized, lehmer, quasi, weighted, winsorized, trimmed-mean, ...)
from functools import partial
import operator as op
import numpy as np
def length(array):
assert np.ndim(array) == 1
return np.shape(array)[0]
def all_positive(array):
@sina-programer
sina-programer / maxheap.py
Created June 28, 2025 12:12
implementation of Max-Heap data structure class in python from scratch
class Node:
def __init__(self, identifier, priority):
self.identifier = identifier
self.priority = priority
def set_priority(self, value):
self.priority = value
def __gt__(self, other):
return self.priority > other.priority
@sina-programer
sina-programer / matrix-multiplication.py
Created June 28, 2025 08:24
matrix multiplication implemented in python from scratch without arrays.
def matrix_multiplication(A, B):
m = len(A)
n = len(A[0])
n2 = len(B)
k = len(B[0])
assert n == n2
assert all(len(row)==n for row in A)
assert all(len(row)==k for row in B)
@sina-programer
sina-programer / gist-gatherer.py
Created June 26, 2025 12:08
gather all gist from a user in GitHub easily by this script. (I use it to back up my gist locally)
import datetime as dt
import mimetypes
import requests
import json
import os
BASE_URL = "https://api.github.com/users/{username}/gists"
def clean(clause, fill='-', encoding='ascii'):
return clause.strip().replace(' ', fill).encode(encoding, 'ignore').decode(encoding)
@sina-programer
sina-programer / tabularize.py
Created June 26, 2025 09:53
print numbers 1-n in desired number of columns
n = 11
cols = 3
k = 0
while k < n:
k += 1
if k%cols == 0:
print(k)
else:
print(k, end=', ')
@sina-programer
sina-programer / cycle_transform.py
Created June 26, 2025 09:31
project numbers in [a, b] as a cycle. (a-1 => b, b+1 => a)
def transform(x, a, b):
''' projects x in [a, b] like a cycle. a-1=>b, b+1=>a '''
n = b - a + 1
return (x - a) % n + a
if __name__ == "__main__":
a = 1
b = 10
@sina-programer
sina-programer / pascal-triangle.py
Created June 26, 2025 08:46
create pascal triangle by least effort without looping! (count & combination technique)
from math import comb
def pascal_nth_row(n):
return [comb(n, i) for i in range(n+1)]
def pascal_triangle(rows):
return list(map(pascal_nth_row, range(rows)))
def print_pascal(rows):
for idx, row in enumerate(pascal_triangle(rows)):
@sina-programer
sina-programer / in_range.py
Created June 26, 2025 08:20
check if x is in [a, b] with just one comparison! (a <= x <= b)
def in_range(x, a, b):
''' checks if x is in [a, b] '''
if x == a: return True
u = (b - a) / (x - a)
return u >= 1
if __name__ == "__main__":
assert in_range(10, 1, 100)
assert in_range(10, 9, 11)