This file contains hidden or 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 c(x, k, d): | |
x1 = [int(x) for x in str(x)] + [0] | |
x1 = x1[::-1] | |
Y = (x//pow(10, k)) *pow(10, k - 1) | |
if d > 0: | |
if x1[k] > d: | |
return Y + pow(10, k - 1) | |
elif x1[k] == d: | |
return Y + (x % pow(10, k - 1)) + 1 | |
else: |
This file contains hidden or 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
import random, fractions | |
def monte_carlo(trials, n): | |
total = 0 | |
for _ in range(trials): | |
painted = 0 | |
pts = sorted([random.random() for _ in range(n)]) | |
prev = 1 | |
for i, x in enumerate(pts): | |
if i == 0: |
This file contains hidden or 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 functools import cache | |
@cache | |
def a(n): | |
if n == 0: | |
return 0 | |
if n == 1: | |
return 1 | |
if n % 2 == 0: | |
return a(n//2) |
This file contains hidden or 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 compute(p, number = 524487): | |
S = [0] | |
#V keeps tracks of which connected component vertex i is in | |
V = [i for i in range(10**6)] | |
#These are the connected components of the graph, initially every vertex is in its own connected component | |
#The head of the connected component is itself | |
cc = {i:set([i]) for i in range(10**6)} | |
k = 1 | |
calls = 0 | |
while True: |
This file contains hidden or 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
import math | |
import decimal as dc | |
def is_sq(x): | |
sq = (x ** (1 / 2)) | |
if round(sq) ** 2 == x: | |
return True | |
return False | |
def compute(N): |
This file contains hidden or 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 MaxContiguousSubarray(A): | |
currMax = 0 | |
currSum = 0 | |
for i in range(len(A)): | |
currSum = max(currSum + A[i], A[i]) | |
currMax = max(currMax, currSum) | |
return currMax | |
def MaxsumSubseq(M): | |
currMax = 0 |
This file contains hidden or 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 is_sq(x): | |
sqrt = (x ** (1 / 2)) | |
if round(sqrt) ** 2 == x: | |
return True | |
return False | |
def compute(): | |
for b in range(1, 1000): | |
for a in range(b + 2, 1000, 2): | |
x = (a*a + b*b)//2 |
This file contains hidden or 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 solve(part, j, S, S_set, S_all_sets, P): | |
if j == len(part): | |
S_all_sets.append(S_set) | |
return S_set | |
for x in P[part[j]]: | |
if all(S[int(i)] + 1 != 2 for i in str(x)): | |
SS = [S[k] for k in range(10)] | |
for i in str(x): | |
SS[int(i)] += 1 | |
if x == max(S_set + [x]): |
This file contains hidden or 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 compute(limit): | |
v = [0]*(limit + 1) #v[k] = minimum number of multiplication to get n^k | |
count = 1 #Counting the number of solved k's. It's used to stop the while loop | |
stack = [(1,)] #The start stack, the only path of length 0 in the tree | |
currdepth = 1 #Keeping track of the current depth we are in the tree | |
while count != limit: | |
#We continue till we have found all 0 < k < 201 |
This file contains hidden or 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 is_special_sum_set(A): | |
l = len(A) | |
#Generate all subsets and categorize them by length of set | |
#Then subsets[x] = all subsets of length x | |
subsets = [[] for x in range(l + 1)] | |
#Keep track of sums to test for duplicates (Condition 1) | |
sums = [] | |
#Here we generate all subsets while checking if condition 1 is satisfied |
NewerOlder