Skip to content

Commit abf2b83

Browse files
committed
add result caching, speed up slow tests
1 parent ac27505 commit abf2b83

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

py3/hard/is_match/is_match.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
def _saved_score(func):
2+
def wrapper(*args, **kwargs):
3+
obj = args[0]
4+
if len(args) > 2:
5+
place = args[2]
6+
else:
7+
place = kwargs["place"]
8+
if place in obj.answers:
9+
return obj.answers[place]
10+
res = func(*args, **kwargs)
11+
obj.answers[place] = res
12+
return res
13+
return wrapper
14+
15+
116
class Solution(object):
217
class Rune:
318
def __new__(cls, c, prev):
@@ -13,9 +28,11 @@ def __init__(self, c, prev):
1328
self.any = c == "."
1429
self.next = None
1530
self.any_quantity = False
31+
self.answers = {}
1632
if prev:
1733
prev.next = self
1834

35+
@_saved_score
1936
def match(self, s, place):
2037
if place >= len(s):
2138
if self.any_quantity:

0 commit comments

Comments
 (0)