Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- isAnagram = lambda s1,s2 : False if False in map(lambda i: i in s1, s2) else True
- print isAnagram("Hello", "ebola") # False
- print isAnagram("Hello", "olelH") # True
- #### EXPLAINING BEGINS ####
- # def isAnagram(s1,s2):
- ##Taking two arguments: s1, and s2, both being strings.
- #return False if False in ... else True
- ##A clever one line solution as opposed to typing the following:
- #if False in ...:
- # return False
- #else:
- # return True
- #map(lambda i: i in s1, s2)
- #...
- #lambda i: i in ...
- ##lambda is basically an on-the-go version of 'def' where 'i' is the argument of the function
- ##You should know that 'x in y' returns True or False. This lambda function is basically saying:
- #def function(i, ...):
- # if i in ...:
- # return True
- # else:
- # return False
- #map(..., s2)
- ##This is basically a 'zip()' but for functions.
- ##It replaces the letters in s2 with the output of the 'lambda' function(which returns True or False).
- #Now for an intentionally longer version...
- def isAnagram(s1, s2):
- confirmation = [] #blank list for later
- for i in s2: #iterate over s1
- if i in s2:
- confirmation.append(True)
- else:
- confirmation.append(False)
- if True in confirmation:
- return True
- else:
- return False
- #A shorter version:
- def isAnagram2(s1, s2):
- confirmation = [] #blank list for later
- for i in s2: #iterate over s1
- if i in s2:
- confirmation.append(True)
- if len(confirmation) > 0: #True was the only thing we could have oppended to it
- #So logically...
- return True
- else:
- return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement