-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.py
executable file
·51 lines (38 loc) · 1.53 KB
/
cache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# -*- coding: utf-8 -*-
import unittest
def cache(cache_size: int, cities: list) -> int:
cache = list()
cache_dict = {'hit': 1, 'miss': 5}
cities_lower = list(map(lambda _: _.lower(), cities))
runtime = 0
for city in cities_lower:
if city in cache:
cache.remove(city)
cache.append(city)
runtime += cache_dict['hit']
else:
if cache_size != 0:
if cache_size == len(cache):
cache.pop(0)
cache.append(city)
runtime += cache_dict['miss']
return runtime
class TestCache(unittest.TestCase):
def test_cache(self):
for cache_size, cities, expected in [
(3, ['Jeju', 'Pangyo', 'Seoul', 'NewYork', 'LA',
'Jeju', 'Pangyo', 'Seoul', 'NewYork', 'LA'], 50),
(3, ['Jeju', 'Pangyo', 'Seoul', 'Jeju', 'Pangyo',
'Seoul', 'Jeju', 'Pangyo', 'Seoul'], 21),
(2, ['Jeju', 'Pangyo', 'Seoul', 'NewYork', 'LA',
'SanFrancisco', 'Seoul', 'Rome', 'Paris',
'Jeju', 'NewYork', 'Rome'], 60),
(5, ['Jeju', 'Pangyo', 'Seoul', 'NewYork', 'LA',
'SanFrancisco', 'Seoul', 'Rome', 'Paris',
'Jeju', 'NewYork', 'Rome'], 52),
(2, ['Jeju', 'Pangyo', 'NewYork', 'newyork'], 16),
(0, ['Jeju', 'Pangyo', 'Seoul', 'NewYork', 'LA'], 25)
]:
self.assertEqual(expected, cache(cache_size, cities))
if __name__ == '__main__':
unittest.main()