富途面试-编程题

功能题

请对query(keywords)函数实现cache功能; “类似实现一个缓存” 要求:

  • a.若在n秒内使用相同的keywords调用进行查询,则直接返回query(两数上一次查询的内容,否则需要重新进行DB查询
  • b.缓行数据不借助redis请用内存存储,请构造出不命中cache、命中cache l以及cache超时三种场景,执行结果分别打印出三种请求的使用缓存情况
  • c.请在30分钟内完成
import time
class LocalCache():
    time_key_expire = {
    }
    def __setitem__(self, key, value):
        self.__dict__[key] = value
    def __getitem__(self, item):
        if item in self.__dict__:
            return self.__dict__[item]
        return None
local = LocalCache()


def search_cache(expire_time):
    def inner(func_name):
        def wraps(key):
            now = time.time()
            # 过期,惰性删除,并回设
            if local.time_key_expire.get(key) and now - local.time_key_expire.get(key) > expire_time:
                print("--缓存过期--")
                del local.time_key_expire[key]
                del local.__dict__[key]
                res = func_name(key)
                local.time_key_expire[key] = time.time()
                local[key]=res
                return res
            # 没过期并且存在
            if local[key]:
                print("--使用缓存--")
                return local[key]
            res = func_name(key)
            # 不存在
            if key not in local.__dict__:
                print("--未使用缓存--")
                local.time_key_expire[key] = time.time()
                local[key] = res
            return res
        return wraps
    return inner


@search_cache(expire_time=3)
def query(key):
	# 假设DB数据如下
    db_values = {
        3: 'values'
    }
    return db_values.get(key)

res = query(3)
print(res)
res = query(3)
print(res)
time.sleep(4)
res1 = query(3)
print(res1)

算法题:

  1. 请实现一个西数find number(give_list),传给定一个整形数组,找出数组中所有满足如下要求的数出来:这个数所有
    它左边的数都比它小,且所有它右边的数都比它大
    要求:时间复杂度要求小于等于O(Xn),×为常数;在30分钟内完成
    例如:传递数组[1,2,4,3,10,13,15,12,16,18,17],数组中1,2,10,16 满足条件,因此返回 [1,2,10,16]
    当时比较急,一时间没有思绪,用了O(n*2), 后边再想想。
def find_number(give_list):

    def get_item(data1, item, data2):
        if max(data1) < item < min(data2):
            return item
        return None
    res = []
    for index in range(len(give_list)):
        if index in (0, len(give_list)-1):
            if index == 0:
                if give_list[0] < min(give_list[1:]):
                    res.append(give_list[0])
            else:
                if give_list[-1] > max(give_list[:-1]):
                    res.append(give_list[-1])
            continue
        r = get_item(give_list[:index], give_list[index], give_list[index+1:])
        if r:
            res.append(r)
    return res

print(find_number([1,2,4,3,10,13,15,12,16,18,17]))
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

积极向上的Coder

一杯咖啡支持原创,技术支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值