में कनवर्ट करना मेरे पास पाइथन 3.2 में एक कोड था और मैं इसे पायथन 2.7 में चलाने के लिए चाहता था। मैंने इसे परिवर्तित कर दिया है (दोनों संस्करणों में missing_elements
का कोड डाला है) लेकिन मुझे यकीन नहीं है कि यह करने का सबसे प्रभावी तरीका है या नहीं। असल में क्या होता है यदि दो yield from
कॉल ऊपरी आधे में नीचे और missing_element
फ़ंक्शन में निचले आधे में हैं? क्या दो हिस्सों (ऊपरी और निचले) की प्रविष्टियां एक-दूसरे में एक-दूसरे में संलग्न होती हैं ताकि माता-पिता रिकर्सन yield from
कॉल के साथ काम कर सकें और दोनों हिस्सों को एकसाथ इस्तेमाल कर सकें?"से उपज" कथन को पायथन 2.7 कोड
def missing_elements(L, start, end): # Python 3.2
if end - start <= 1:
if L[end] - L[start] > 1:
yield from range(L[start] + 1, L[end])
return
index = start + (end - start) // 2
# is the lower half consecutive?
consecutive_low = L[index] == L[start] + (index - start)
if not consecutive_low:
yield from missing_elements(L, start, index)
# is the upper part consecutive?
consecutive_high = L[index] == L[end] - (end - index)
if not consecutive_high:
yield from missing_elements(L, index, end)
def main():
L = [10, 11, 13, 14, 15, 16, 17, 18, 20]
print(list(missing_elements(L, 0, len(L)-1)))
L = range(10, 21)
print(list(missing_elements(L, 0, len(L)-1)))
def missing_elements(L, start, end): # Python 2.7
return_list = []
if end - start <= 1:
if L[end] - L[start] > 1:
return range(L[start] + 1, L[end])
index = start + (end - start) // 2
# is the lower half consecutive?
consecutive_low = L[index] == L[start] + (index - start)
if not consecutive_low:
return_list.append(missing_elements(L, start, index))
# is the upper part consecutive?
consecutive_high = L[index] == L[end] - (end - index)
if not consecutive_high:
return_list.append(missing_elements(L, index, end))
return return_list
आप शायद 'उपज के लिए bar', नहीं' foo' चाहते हैं। – ovgolovin
@ovgolovin: धन्यवाद; तय की। – abarnert
एक सवाल कैसे रिकर्सिव कॉल काम करते हैं? क्या पैरेंट फ़ंक्शन से "उपज" बच्चे में दो "उपज" से जुड़ी होती है। अगर लगातार नहीं है: missing_elements (एल, स्टार्ट, इंडेक्स) से उपज # लगातार ऊपरी हिस्सा है? consecutive_high = एल [इंडेक्स] == एल [अंत] - (अंत - सूचकांक) अगर लगातार नहीं है: missing_elements (एल, इंडेक्स, अंत) – vkaul11