> 2021年04月24日信息消化 ### 每天学点算法 这个问题是由谷歌提出的。 实现一个LRU(最近使用最少的)缓冲区。它应该能够以缓存的大小n进行初始化,并包含以下方法。 set(key, value):将key设置为value。如果缓存中已经有n个项目,而我们正在添加一个新的项目,那么它也应该删除最近使用最少的项目。 get(key):获取key的值。如果不存在这样的键,则返回null。 每个操作都应该在O(1)时间内运行。 This problem was asked by Google. Implement an LRU (Least Recently Used) cache. It should be able to be initialized with a cache size `n`, and contain the following methods: - `set(key, value)`: sets `key` to `value`. If there are already `n` items in the cache and we are adding a new item, then it should also remove the least recently used item. - `get(key)`: gets the value at `key`. If no such key exists, return null. Each operation should run in O(1) time. ```python #https://github.com/vineetjohn/daily-coding-problem/blob/master/solutions/problem_052.py class Node: def __init__(self, key, value): self.key = key self.value = value self.prev = None self.next = None class LRU: def __init__(self, cache_limit): self.cache_limit = cache_limit self.cache_contents = dict() self.head = Node(None, None) self.tail = Node(None, None) self.head.next = self.tail self.tail.prev = self.head def _remove(self, node): prev_node = node.prev next_node = node.next prev_node.next = next_node next_node.prev = prev_node def _add(self, node): prev_node = self.tail.prev node.next = self.tail node.prev = prev_node prev_node.next = node self.tail.prev = node def set_value(self, key, value): if key in self.cache_contents: node = self.cache_contents[key] self._remove(node) node = Node(key, value) self._add(node) self.cache_contents[key] = node if len(self.cache_contents) > self.cache_limit: node_to_delete = self.head.next self._remove(node_to_delete) del self.cache_contents[node_to_delete.key] def get_value(self, key): if key in self.cache_contents: node = self.cache_contents[key] self._remove(node) self._add(node) return node.value return None lru = LRU(cache_limit=3) assert not lru.get_value("a") lru.set_value("a", 1) assert lru.get_value("a") == 1 lru.set_value("b", 2) lru.set_value("c", 3) lru.set_value("d", 4) lru.set_value("e", 5) lru.set_value("a", 1) assert lru.get_value("a") == 1 assert not lru.get_value("b") assert lru.get_value("e") == 5 assert not lru.get_value("c") ``` ### 其他值得阅读 #### 中国程序员容易发音错误的单词 [shimohq/chinese-programmer-wrong-pronunciation](https://github.com/shimohq/chinese-programmer-wrong-pronunciation) - admin → /'ædmɪn/ - agile → /'ædʒaɪl/ - amazon → /'æməzən/ - archive → /'ɑːkaɪv/ - avatar → /'ævətɑː/ - event → /ɪ'vent/ - format → /'fɔːmæt/ - integer → /'ɪntɪdʒə/ - locale → /ləʊ'kɑːl/ - miscellaneous → /ˌmɪsəˈleɪniəs/ - retina → /'retɪnə/ - suite → /swiːt/ - variable → /'veəriəbl/ 发现重音真的很容易搞错... #### 英语口语发音中的重音规则 原文:https://zhuanlan.zhihu.com/p/350948985 ##### 1、双音节单词的重音通常在第一个音节 **dai**ly **ma**tter **com**mon ##### 2、某些双音节单词的重音落在第二个音节 a、带 a、be、en、for、im、in、mis、pre、pro、to、trans、un 等不可分前缀的双音节单词 a**go** be**gin** for**get** im**ply** in**stall** mis**take** pre**pare** to**night** b、少数双音节的外来词 ma**chine** ho**tel** po**lite** ##### 3、多音节单词的重音多落在倒数第三个音节,次重音落在主重音前第二个音节 **sa**tisfy mag**ni**ficent ex**ec**utive anni**ver**sary hy**po**thesis im**me**diately phi**lan**thropy ### 一点收获 - 看到一篇观点很新颖的文章:[Will Worrying Make You Better?](https://every.to/superorganizers/will-worrying-make-you-better) --[Dan Shipper](http://danshipper.com/blog) > “How do you know if you’re doing the job? If you’re up at 3am talking into a tape recorder and writing notes on paper, have a knot in your stomach, rash on your skin, losing sleep and losing touch with your wife and kids, have no appetite or sense of humor and feel everything might turn out wrong, you’re probably doing the job.” The message is pretty clear: if you’re not doing well, maybe you’re not worried enough. And maybe if you worry more, you’ll do better. > > "你怎么知道你是否在做工作?如果你凌晨3点起来对着录音机说话,在纸上写笔记,胃里有疙瘩,皮肤上有疹子,失眠,与妻子和孩子失去联系,没有食欲和幽默感,觉得一切都可能出错,你可能在做这份工作。" 这个信息非常清楚:如果你做得不好,也许你的担心不够。也许如果你更担心,你会做得更好。 很极端的想法,但是也有一定道理。担心是出于对坏结果的害怕,恐惧意味着肾上腺素(adrenaline),意味着可以更努力的去学习。人人都想轻松的活着,这算是构建了一种被动学习的状态吧。 - 油管 [Ok, Who is CodeMiko?](https://www.youtube.com/watch?v=CsQjxEd-gsw&t=2s) CodeMiko是Twitch上的一个Just Chatting流媒体人。她把她的人类自己称为技术员。她在虚幻引擎中为Miko建模,并为她的流媒体建立所有环境/资产/动画。她的动作捕捉服是**Xsens MVN**。视频中解释了更多细节。Code Miko是Twitch、VTubers和整个病毒性内容创作者的未来吗? - 没有人有权利改变别人的想法。尊重所有专注自己。