This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- 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... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]]) |
NewerOlder