Created
October 28, 2018 02:09
-
-
Save peijunz/e4b979ce2c7b6e024a706b0a11036180 to your computer and use it in GitHub Desktop.
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
class Arrayify: | |
def __init__(self, f): | |
self.f = f | |
def __getitem__(self, i): | |
print("Getitem Called") | |
return self.f(i) | |
def f(x): | |
print("Computing...") | |
return ((x+1)*x)//2 | |
g = Arrayify(f) | |
print(f(5)) | |
print(g[5]) |
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 gcd | |
from bisect import bisect_left | |
class PQ: | |
def __init__(self, p, q): | |
self.p, self.q = p, q | |
def __getitem__(self, k): | |
# Number of multiples of p or q that is in range (0, k] | |
return k//self.p + k//self.q | |
class Solution: | |
def magicNumber(self, r, p, q): | |
M = PQ(p, q) | |
return bisect_left(M, r, 0, p*q) | |
def nthMagicalNumber(self, N, A, B): | |
M = 10**9 + 7 | |
m = gcd(A, B) | |
p, q = A//m, B//m | |
n, r = divmod(N, p+q-1) | |
# npq = n*p*q | |
npq = (n*p*q)%M | |
R = self.magicNumber(r, p, q) | |
return ((npq + R)*m)%M |
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
class Padding: | |
def __init__(self, arr): | |
self.arr = arr | |
def __getitem__(self, i): | |
if i < 0: | |
return -inf | |
elif i >= len(self.arr): | |
return inf | |
else: | |
return self.arr[i] | |
a = [2,3,4,5,6] | |
b = Padding(a) | |
print(type(b)) | |
for i in range(-3, 6): | |
print(i, b[i]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment