enumerate()
आइए enumerate करें! आपका कार्य है: किसी दिए गए string के लिए, फ़ंक्शन retrieve_character_indices() परिभाषित करें, जो एक dictionary character_indices बनाता है. इसमें हर key उस string का एक यूनिक कैरेक्टर होगी और उसकी वैल्यू उस अक्षर के string में आने वाले सभी इंडेक्स/स्थितियों की list होगी.
उदाहरण के लिए, यदि आप string 'ukulele' को retrieve_character_indices() फ़ंक्शन में पास करते हैं, तो आउटपुट यह होना चाहिए: {'e': [4, 6], 'k': [1], 'l': [3, 5], 'u': [0, 2]}.
इस कार्य के लिए आप कोई भी string मेथड इस्तेमाल नहीं कर सकते!
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में कोडिंग इंटरव्यू प्रश्नों का अभ्यास
अभ्यास निर्देश
- ऐसा
forलूप परिभाषित करें जो string के कैरेक्टर्स और उनके इंडेक्स पर इटरेट करे. - यदि key पहले से मौजूद हो, तो dictionary को अपडेट करें.
- यदि key अनुपस्थित हो, तो dictionary को अपडेट करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
def retrieve_character_indices(string):
character_indices = dict()
# Define the 'for' loop
for index, character in ____(____):
# Update the dictionary if the key already exists
if character in character_indices:
____[____].____(____)
# Update the dictionary if the key is absent
else:
____[____] = [____]
return character_indices
print(retrieve_character_indices('enumerate an Iterable'))