def uniquePaths(self, m: int, n: int) -> int: """ Consider each move as 1-bit string, say for m = 3 and n = 2, possible path could be RRD, RDR, DRR. There should be at least one D and two R. So, we can find all combinations where there is one D in three moves. Below is an implementation of closed formula for n chooses k = n!/(n-k)!k!. """ total_moves = m - 1 + n - 1 down_moves = n - 1 return mf(total_moves) // (mf(total_moves - down_moves) * mf(down_moves))
def decimal_to_any(num, base): fractional = False if isinstance(num, float): points = 5 num = [int(i) for i in str(num).split(".")] integer = num[0] length = len(str(num[1])) fractional = float(str("0.") + str(num[1])) else: integer = num ans = "" # Decimal part conversion while integer != 0: intermediate = integer % base intermediate = ref[intermediate] ans = str(intermediate) + ans integer //= base # Floating part conversion if fractional: ans += "." while points: fractional, i = mf(fractional * base) fractional = round(fractional, length) i = ref[int(i)] ans += i points -= 1 return ans
def binomail(n, r): return (mf(n) / (mf(r) * mf(n - r)))