SHARE
TWEET

Untitled

a guest Jan 21st, 2020 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '''Given a string s find and return the longest palindromic substring. Ideally in less than n^3 time'''
  2.  
  3. def palindrome_string(some_string):
  4.     some_string = some_string.lower()
  5.     '''to_remove = [',', "'", ':', '.', '!', '?', '-', ' ']
  6.     for item in to_remove:
  7.         if item in some_string:
  8.             some_string.replace(item, '')'''# this didnt work for some reason?
  9.     some_string = some_string.replace(' ','')
  10.     some_string = some_string.replace('.','')
  11.     max_substring = False
  12.     for i in range(len(some_string)):
  13.         for j in range(1,len(some_string)):
  14.             if some_string[j] == some_string[i]:
  15.                 potential_palindrome = some_string[i:j+1]
  16.                 #print(potential_palindrome ,potential_palindrome[::-1])
  17.                 if potential_palindrome == potential_palindrome[::-1] and len(potential_palindrome)>1:
  18.                     if max_substring is False or len(potential_palindrome) > len(max_substring):
  19.                         max_substring = potential_palindrome
  20.     return max_substring
  21. def other_palindrome_test(some_string):
  22.     some_string = some_string.lower()
  23.     some_string = some_string.replace(' ','')
  24.     some_string = some_string.replace('.','')
  25.     for end in range(len(some_string),1,-1): # checks increasingly smaller slices of the string looking for a palindrome
  26.         start = 0 # rests start to first letter after each while loop concludes
  27.         while end <= len(some_string): # checks each slice for palindrome and increments if none found
  28.             test_slice = some_string[start:end+1]
  29.             if test_slice == test_slice[::-1]:
  30.                 return test_slice
  31.             else:
  32.                 start +=1
  33.                 end +=1
  34.     return False
  35. #Tests:
  36. print(palindrome_string('racecar') == 'racecar')
  37. print(palindrome_string('ghead') is False)
  38. print(palindrome_string('dddfffgggeeeracecar') == 'racecar')
  39. print(palindrome_string('dfsracecardskjdalskjsk') == 'racecar')
  40. print(palindrome_string('racecarlkajsdlkjadr') == 'racecar')
  41. print(other_palindrome_test('racecar') =='racecar')
  42. print(other_palindrome_test('ghead') is False)
  43. print(other_palindrome_test('dddfffgggeeeracecar')=='racecar')
  44. print(other_palindrome_test('dfsracecardskjdalskjsk')=='racecar')
  45. print(other_palindrome_test('racecarlkajsdlkjadr')=='racecar')
  46.  
  47. # sentence tests
  48. print('sentence tests')
  49. print(palindrome_string('Euston saw I was not Sue.') == 'eustonsawiwasnotsue')
  50. print(other_palindrome_test('Euston saw I was not Sue.') == 'eustonsawiwasnotsue')
  51. print(palindrome_string('This is not a palindrome') == 'isi')
  52. print(other_palindrome_test('This is not a palindrome') == 'isi')
  53. print(palindrome_string('Marge let a moody baby doom a telegram.') == 'margeletamoodybabydoomatelegram')
  54. print(other_palindrome_test('Marge let a moody baby doom a telegram.')== 'margeletamoodybabydoomatelegram')
  55. print(palindrome_string('No I am not your palindrome') is False )
  56. print(other_palindrome_test('No I am not your palindrome') is False)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top