जब स्ट्रिंग में वर्ण के क्रम की जांच करना आवश्यक हो, तो 'OrderedDict' पद्धति का उपयोग किया जा सकता है।
नीचे उसी का प्रदर्शन है -
उदाहरण
from collections import OrderedDict
def check_order(my_input, my_pattern):
my_dict = OrderedDict.fromkeys(my_input)
pattern_length = 0
for key,value in my_dict.items():
if (key == my_pattern[pattern_length]):
pattern_length = pattern_length + 1
if (pattern_length == (len(my_pattern))):
return 'The order of pattern is correct'
return 'The order of pattern is incorrect'
my_input = 'Hi Mark'
input_pattern = 'Ma'
print("The string is ")
print(my_input)
print("The input pattern is ")
print(input_pattern)
print(check_order(my_input,input_pattern)) आउटपुट
The string is Hi Mark The input pattern is Ma The order of pattern is correct
स्पष्टीकरण
-
आवश्यक पैकेज आयात किए जाते हैं।
-
'check_order' नाम की एक विधि परिभाषित की गई है, जिसमें दो पैरामीटर होते हैं।
-
'फ्रॉमकी' पद्धति का उपयोग करके एक आदेशित शब्दकोश बनाया जाता है।
-
पैटर्न की लंबाई 0 से आरंभ की जाती है।
-
यदि कुंजी पैटर्न के बराबर है, तो पैटर्न की लंबाई बढ़ जाती है।
-
यदि पैटर्न की लंबाई वर्तमान लंबाई के समान है, तो इसका मतलब है कि क्रम सही है, अन्यथा क्रम गलत है।
-
प्रासंगिक संदेश कंसोल पर आउटपुट के रूप में प्रदर्शित होते हैं।