Created
February 17, 2015 22:04
-
-
Save realeroberto/76aafd5cc757dfa8954d to your computer and use it in GitHub Desktop.
X-ian, in Python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Given a string x, returns True iff all the letters in x | |
# are contained in word in the same order as they appear in x. | |
# An exercise from MITx: 6.00.1x Introduction to Computer Science and Programming Using Python. | |
def x_ian(x, word): | |
""" | |
Given a string x, returns True iff all the letters in x are | |
contained in word in the same order as they appear in x. | |
>>> x_ian('eric', 'meritocracy') | |
True | |
>>> x_ian('eric', 'cerium') | |
False | |
>>> x_ian('john', 'mahjong') | |
False | |
x: a string | |
word: a string | |
returns: True if word is x_ian, False otherwise | |
""" | |
if not word: | |
return False | |
elif not x or len(x) == 0: | |
return True | |
elif len(x) == 1: | |
return x in word | |
else: | |
x_pos = word.find(x[0]) | |
if x_pos > -1: | |
return x_ian(x[1:], word[(x_pos + 1):]) | |
else: | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment