In [4]:
def checkanagram(string1, string2):
    a = sorted(string1)
    b = sorted(string2)
    if a==b:
        return True
    else:
        return False

In [3]:
sorted('hello')#returns list of chars


Out[3]:
['e', 'h', 'l', 'l', 'o']

In [6]:
checkanagram('hello', 'olleh')


Out[6]:
True

In [16]:
import time
start_time = time.time()
checkanagram('hello', 'ollhe')
print('{} seconds'.format(time.time()-start_time))


6.103515625e-05 seconds

In [17]:
#runtime check
help(sorted)


Help on built-in function sorted in module builtins:

sorted(iterable, key=None, reverse=False)
    Return a new list containing all items from the iterable in ascending order.
    
    A custom key function can be supplied to customise the sort order, and the
    reverse flag can be set to request the result in descending order.


In [25]:
words1 = ['a', 'god', 'hello', 'sevenzz', 'alsdkfjlaskdfjaslf']
words2 = ['a', 'dog', 'olleh', 'senevzz', 'aflkjasdlfkjasffas']

import time

for i in range(len(words1)):
    start_time = time.time()
    isanagram = checkanagram(words1[i], words2[i])
    print('{} seconds. Returns {}'.format(time.time() - start_time,isanagram))

#I don't understand the timings yet...


5.7220458984375e-06 seconds. Returns True
3.0994415283203125e-06 seconds. Returns True
2.6226043701171875e-06 seconds. Returns True
2.6226043701171875e-06 seconds. Returns True
6.4373016357421875e-06 seconds. Returns False

In [23]:
#import matplotlib.pyplot as plt
#plt.plot([1.6689300537109375e-06,9.5367431640625e-07,7.152557373046875e-07,4.76837158203125e-07])
#plt.ylabel('some numbers')
#plt.show()

In [68]:
def checkpalindrome(string1):
    len1 = len(string1)
    c = len1//2 #integer divide. Works for both even and odd lengths
    reverse_right_half = string1[::-1][0:c]
    if (string1[0:c] == reverse_right_half):
        return True
    else:
        return False

In [32]:



1 1 2

In [81]:
checkpalindrome('noon')


Out[81]:
True

In [76]:
checkpalindrome('lufol')


Out[76]:
False

In [77]:
checkpalindrome('racecar')


Out[77]:
True

In [78]:
checkpalindrome('loul')


Out[78]:
False