From 10a092770fe42f92fa98ff322f60fb6dd215d79d Mon Sep 17 00:00:00 2001 From: Nishant Ingle Date: Sat, 26 Oct 2019 18:00:15 +0530 Subject: [PATCH] Added early string length check --- anagram.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/anagram.py b/anagram.py index 1fb8057..ff77bc8 100644 --- a/anagram.py +++ b/anagram.py @@ -1,13 +1,16 @@ # Check whether two string is anagram of each other def anagram(a, b): - x = ''.join(sorted(a.lower())).strip() - y = ''.join(sorted(b.lower())).strip() - - if len(x) != len(y) or x != y: + if len(a) != len(b): print('"{}" is not an anagram to "{}"'.format(a, b)) - else: - print('"{}" is an anagram to "{}"'.format(a, b)) + else: + x = ''.join(sorted(a.lower())).strip() + y = ''.join(sorted(b.lower())).strip() + + if x != y: + print('"{}" is not an anagram to "{}"'.format(a, b)) + else: + print('"{}" is an anagram to "{}"'.format(a, b)) if __name__ == '__main__': a = "Tom Marvolo Riddle"