LeetCafe

Part A

def overlap (x1, x2, x3, x4):
    #minimum first
    x1, x2 = sorted([x1, x2])
    x3, x4 = sorted([x3, x4])
    return max(x1, x3) <= min(x2, x4)
Part B

def varcompare (a, b):
    parts1 = list(map(int, a.split('.')))
    parts2 = list(map(int, b.split('.')))
    
    # add 0 in the end
    max_length = max(len(parts1), len(parts2))
    parts1.extend([0] * (max_length - len(parts1)))
    parts2.extend([0] * (max_length - len(parts2)))
    
    # Compare
    for a, b in zip(parts1, parts2):
       if a > b:
            return 'greater than'
        elif a < b:
             return 'less than'
        return 'equal'
Part C

import time
from collections import OrderedDict

# improving from existing LRU
class LRUExpiry:
    def __init__(self, capacity, expiry):
        self.cache = OrderedDict()
        self.capacity = capacity
        self.expiry = expiry

    def get(self, key):
        if key in self.cache and (time.time() - self.cache[key][1]) < self.expiry:
            self.cache.move_to_end(key)
            return self.cache[key][0]
        return -1

    def put(self, key, value):
        if key in self.cache:
            self.cache.move_to_end(key)
        self.cache[key] = (value, time.time())
        if len(self.cache) > self.capacity:
            self.cache.popitem(last=False)
Scroll to Top