"""Write a function match() that takes two arguments. The first argument is a string representing a pattern and the second argument is a string representing some text. The function returns true if the characters in the pattern match exactly against a substring of the text. Otherwise it returns false. You can use any programming or scripting language that will run from a Linux shell. You should not use any string or regular expression functions, apart from the following: * A function to get the length of a string * A function to return the Nth character in a string Your code should compile and run without any modifications.""" def match(pattern, text): contains_text = False t_index = 0 p_index = 0 while t_index < len(text) and not contains_text: if pattern[p_index] == text[t_index]: p_index += 1 if p_index == len(pattern): contains_text = True else: if p_index > 0: p_index = 0 t_index += 1 return contains_text print match('asg', 'dasf')
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question