का उपयोग करते समय मैं पाइथन में लेवेनशेटिन दूरी की गणना करने के लिए एक प्रोग्राम लिख रहा हूं। मैंने यादों को लागू किया क्योंकि मैं एल्गोरिदम को पुनरावर्ती रूप से चला रहा हूं। मेरे मूल कार्य ने समारोह में ही ज्ञापन लागू किया। यहां यह दिखता है:अधिकतम रिकर्सन गहराई पार हो गई, लेकिन केवल सजावट
# Memoization table mapping from a tuple of two strings to their Levenshtein distance
dp = {}
# Levenshtein distance algorithm
def lev(s, t):
# If the strings are 0, return length of other
if not s:
return len(t)
if not t:
return len(s)
# If the last two characters are the same, no cost. Otherwise, cost of 1
if s[-1] is t[-1]:
cost = 0
else:
cost = 1
# Save in dictionary if never calculated before
if not (s[:-1], t) in dp:
dp[(s[:-1], t)] = lev(s[:-1], t)
if not (s, t[:-1]) in dp:
dp[(s, t[:-1])] = lev(s, t[:-1])
if not (s[:-1], t[:-1]) in dp:
dp[(s[:-1], t[:-1])] = lev(s[:-1], t[:-1])
# Returns minimum chars to delete from s, t, and both
return min(dp[(s[:-1], t)] + 1,
dp[(s, t[:-1])] + 1,
dp[(s[:-1], t[:-1])] + cost)
यह काम करता है! हालांकि, मुझे using decorators को याद करने का एक तरीका मिला। मैंने इस तकनीक को अपने एल्गोरिदम पर लागू करने का प्रयास किया:
# Memoization table mapping from a tuple of two strings to their Levenshtein distance
def memoize(func):
memo = {}
def wrap(s, t):
if (s, t) not in memo:
memo[(s, t)] = func(s, t)
return memo[(s, t)]
return wrap
# Levenshtein distance algorithm
@memoize # lev = memoize(lev)
def lev(s, t):
# If the strings are 0, return length of other
if not s:
return len(t)
if not t:
return len(s)
# If the last two characters are the same, no cost. Otherwise, cost of 1
if s[-1] is t[-1]:
cost = 0
else:
cost = 1
# Returns minimum chars to delete from s, t, and both
return min(lev(s[:-1], t) + 1,
lev(s, t[:-1]) + 1,
lev(s[:-1], t[:-1]) + cost)
मेरे लिए, यह क्लीनर और कम भ्रमित दिखता है। मैंने सोचा कि दोनों कार्यात्मक रूप से समकक्ष होंगे, लेकिन जब मैंने सजावट के साथ संस्करण चलाया, तो मुझे आश्चर्य हुआ कि मुझे RecursionError: maximum recursion depth exceeded
मिला है।
मुझे वास्तव में क्या याद आ रही है? सजावटी का उपयोग कार्यात्मक रूप से बराबर नहीं है? मैंने sys.setrecursionlimit(1500)
जोड़कर एक फिक्स का प्रयास किया और यह काम करता है, लेकिन यह एक हैक है और यह समझ में नहीं आता कि दोनों फ़ंक्शन अलग-अलग क्यों हैं।
नोट: मैं और टी के लिए अपने परीक्षण तारों के रूप में lorem ipsum में से एक पैरा उपयोग कर रहा हूँ विकिपीडिया से:,
Lorem Ipsum मातम AMET बैठते हैं, consectetur adipiscing elit sed eiusmod tempor है incididunt ut labore एट dolore Magna aliqua। इस साइट पर क्लिक करने के लिए, इस तरह के अभ्यास के लिए उपयोग करने के लिए आवेदन कर रहे हैं। Voluptate velit esse cillum dolore eu fugiat nulla pariatur में reprehenderit में Duis aute irure dolor। Excepteur sint occaakat cupidatat गैर proident, चुपके में चुपचाप के रूप में काम कर रहे हैं।
मैं समझता हूं कि लंबे तारों के लिए, मेरा पहला कार्य विफल हो जाएगा। मैं सिर्फ यह जानना चाहता हूं कि सजाया गया पहला क्यों विफल रहता है। धन्यवाद!
एक मामूली टिप्पणी यह है कि आपका कोड 'लागत = 1 - (एस [-1] टी [-1] के साथ साफ किया जा सकता है)' –