Skip to content

Instantly share code, notes, and snippets.

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

corehello corehello

🏠
Working from home
View GitHub Profile
#!/bin/env python
def generateNdimensionArray(n):
"""
this function generate a n-dimension array like following
[[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]]
from operator import mul, add
from functools import reduce
def chinese_remainder_theorem(tuple):
transpose = list(zip(*tuple))
product = reduce(mul, transpose[0])
min_result = reduce(add, [
j * l *(l%i) for i,j in tuple for l in [reduce(mul, [k for k in transpose[0] if k != i])]
]) % product
print("The value is: %s + n * %s which n = 0..inf" % (min_result, product))
return min_result, product
@corehello
corehello / index.pug
Created March 5, 2022 14:47
Rubik's cube in Javascript
- var faces = ['left', 'right', 'top', 'bottom', 'back', 'front'], pieces = 26
#scene.scene
#pivot.pivot.centered(style = 'transform: rotateX(-35deg) rotateY(-135deg);')
#cube.cube
while pieces--
.piece
each face in faces
div(class = 'element ' + face)
.credits
.text(style = 'position: initial') Wanna make solver, but I'm too lazy to translate 100 lines of my Cpp's solver to JS...
@corehello
corehello / sqrtn.py
Last active September 28, 2021 17:49
nth root calculation proof of concept
def sqrtn(n, p, z):
print("sqrtn {} with {} precision is:".format(n, p))
if len(n) %z != 0:
n = "0"*(z-len(n)%z) + n
splited_n = [n[i:i+z] for i in range(0,len(n),z)]
A,B,Remainer,Result = 0,0,0,""
def get_binomial_coefficient(n,k):
if k == 0:
return 1
if k == n:
@corehello
corehello / sqrt3.py
Last active September 29, 2021 15:31
3th root calculation proof of concept
def sqrt3(n, p):
# any number can be writen as n = a*10 +b, where a >=0, b is 0~9
# for example 123 = 12 * 10 + 3
# so then any number N = (a*10 + b)^3 = 1000*a^3 + 300*a^2*b + 30*a*b^2 + b^3 + remainer
# = 1000*a^3 + b*(300*a^2 + 30*a*b + b^2) + remainer
# then b*(300*a^2 + 30*a*b + b^2) <= N - 1000*a^3
# then we can calulate max b to match the expression
print("sqrt3 {} with {} precision is:".format(n, p))
if len(n) %3 != 0:
n = "0"*(3-len(n)%3) + n
@corehello
corehello / uniform_sum_distribution_for_e.py
Created August 31, 2021 17:14
use uniform sum distribution to calculate e
def uniform_sum_distribution_for_e(N,target):
count_sum = 0
import random
for i in range(N):
sum = 0
while True:
j = random.random()
sum+=j
count_sum+=1
if sum > target:
@corehello
corehello / sqrt.py
Last active September 29, 2021 14:44
sqrt function(concept of proof) in python 3
def sqrt(n, p):
# any number can be writen as n = a*10 +b, where a >=0, b is 0~9
# for example 123 = 12 * 10 + 3
# so then any number N = (a*10 + b)^2 + remainer = a^2*100 + b*(b+20*a) + remainer
# then b*(b+20*a) <= N - a^2*100
# then we can calulate max b to match the expression
print("sqrt {} with {} precision is:".format(n, p))
if len(n) %2 != 0:
n = "0"*(2-len(n)%2) + n
splited_n = [n[i:i+2] for i in range(0,len(n),2)]
@corehello
corehello / climbStairs.py
Created January 12, 2021 03:53
yet another solution for https://leetcode.com/problems/climbing-stairs/ without fibonacci
from math import floor
from functools import reduce
from operator import mul
class Solution:
def climbStairs(self, n: int) -> int:
ret_sum = 0
for i in range(0, floor(n/2)+1):
if i == 0:
ret_sum += 1
@corehello
corehello / perm.py
Created January 11, 2021 15:28
string permutation
def perm(string):
if len(string) <= 1:
return [string]
return [i+j for idx,i in enumerate(string)
for j in perm(string[0:idx] + string[idx+1:])]
if __name__ == "__main__":
TCs = [
("a", ["a"]),
("ao", ["ao", "oa"])
@corehello
corehello / quicksort.py
Created January 11, 2021 11:06
quicksort in python - most understandable
def qsort(arr):
if len(arr) <= 1:
return arr
else:
# construct array with [(values bigger than pivot), pivot, (values smaller than pivot)]
return qsort([x for x in arr[1:] if x > arr[0]]) + \
[arr[0]] + \
qsort([x for x in arr[1:] if x <= arr[0]])