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]:
In [6]:
checkanagram('hello', 'olleh')
Out[6]:
In [16]:
import time
start_time = time.time()
checkanagram('hello', 'ollhe')
print('{} seconds'.format(time.time()-start_time))
In [17]:
#runtime check
help(sorted)
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...
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]:
In [81]:
checkpalindrome('noon')
Out[81]:
In [76]:
checkpalindrome('lufol')
Out[76]:
In [77]:
checkpalindrome('racecar')
Out[77]:
In [78]:
checkpalindrome('loul')
Out[78]: