################################# # Module: middle.py # Author: MobileDigit # Date: 2006-11-10 11:30 # Version: 1.0 """ Middle is a function that compares what you think is in the middle with what actually is. It has an offset of 1, so the 'xyz' in '1xyz21' is considered to be in the middle. """ def odd(item): return not len(item)%2==0 def even(item): return len(item)%2==0 def half(i, j): return (len(i)//2, len(j)//2) def check(string, what): """check returns the strings in the middle, depending on 'what' is entered ('what' must be greater than one letter and smaller than string).""" i, j = half(string, what) if even(string) and even(what): return string[i-j:i+j], elif even(string) and odd(what): return string[i-(j+1):i+1], string[i-j:i+2] elif odd(string) and even(what): return string[i-j:i+j], string[i:i+j+1] elif odd(string) and odd(what): return string[i-j:i+j+1], def middle(string, what = 'xyz'): arg = check(string, what) for item in arg: if item == what: return True else: return False