Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.89 KB | None | 0 0
  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. some_string = some_string.replace(',','')
  12. some_string = some_string.replace("'",'')
  13. max_substring = False
  14. for i in range(len(some_string)):
  15. for j in range(1,len(some_string)):
  16. if some_string[j] == some_string[i]:
  17. potential_palindrome = some_string[i:j+1]
  18. if potential_palindrome == potential_palindrome[::-1] and len(potential_palindrome)>1:
  19. if max_substring is False or len(potential_palindrome) > len(max_substring):
  20. max_substring = potential_palindrome
  21. return max_substring
  22. def other_palindrome_test(some_string):
  23. some_string = some_string.lower()
  24. some_string = some_string.replace(' ','')
  25. some_string = some_string.replace('.','')
  26. some_string = some_string.replace(',','')
  27. some_string = some_string.replace("'",'')
  28. for end in range(len(some_string),1,-1): # checks increasingly smaller slices of the string looking for a palindrome
  29. start = 0 # rests start to first letter after each while loop concludes
  30. while end <= len(some_string): # checks each slice for palindrome and increments if none found
  31. test_slice = some_string[start:end+1]
  32. if test_slice == test_slice[::-1]:
  33. return test_slice
  34. else:
  35. start += 1
  36. end += 1
  37. return False
  38.  
  39. def third_palindrome(some_string):
  40. some_string = some_string.lower()
  41. some_string = some_string.replace(' ','')
  42. some_string = some_string.replace('.','')
  43. some_string = some_string.replace(',','')
  44. some_string = some_string.replace("'",'')
  45. max_palindrome = ''
  46. for i in range(1,len(some_string)):
  47. left = i-1
  48. right = i+1
  49. while left >=0 and right <= len(some_string)-1 and some_string[left] == some_string[right]:
  50. right += 1
  51. left -= 1
  52. potential_palindrome = some_string[left+1:right]
  53. if len(potential_palindrome) > len(max_palindrome):
  54. max_palindrome = potential_palindrome
  55. if max_palindrome =='':
  56. return False
  57. else:
  58. return max_palindrome
  59.  
  60. def create_palindrome(random_string):
  61. pass
  62. # check for existing longest existing paldinrome
  63. # ------------------------------------------------------------
  64. # if a palindrome exists
  65. # create new blank list equal in length to string
  66. # set current longest exisiting palindrome to center of new list and remove from string
  67. # check for next longest palindrome if len is odd split the palindrome and place first half not including
  68. # middle on left in new list and right half not including middle on right in new list remove characters from string
  69. # repeat until no palindrome exists.
  70. # check for and count all duplicates.
  71. # place equal number of all duplicates on either side of center
  72. # place any odd characters at end of string
  73. # -------------------------------------------------------------
  74. # if no palindrome exists
  75. # check for and count all duplicates.
  76. # choose odd count for center
  77. # place equal number of all duplicates on either side of center
  78. # place any odd characters at end of string
  79. # -------------------------------------------------------------
  80. # return palindrome
  81.  
  82. #Tests:
  83. print(palindrome_string('racecar') == 'racecar')
  84. print(palindrome_string('ghead') is False)
  85. print(palindrome_string('dddfffgggeeeracecar') == 'racecar')
  86. print(palindrome_string('dfsracecardskjdalskjsk') == 'racecar')
  87. print(palindrome_string('racecarlkajsdlkjadr') == 'racecar')
  88. print(other_palindrome_test('racecar') == 'racecar')
  89. print(other_palindrome_test('ghead') is False)
  90. print(other_palindrome_test('dddfffgggeeeracecar') == 'racecar')
  91. print(other_palindrome_test('dfsracecardskjdalskjsk') == 'racecar')
  92. print(other_palindrome_test('racecarlkajsdlkjadr') == 'racecar')
  93. print(third_palindrome('racecar')== 'racecar')
  94. print(third_palindrome('ghead')is False )
  95. print(third_palindrome('dddfffgggeeeracecar')== 'racecar')
  96. print(third_palindrome('dfsracecardskjdalskjsk')== 'racecar' )
  97. print(third_palindrome('racecarlkajsdlkjadr')== 'racecar')
  98.  
  99. # sentence tests
  100. print('sentence tests')
  101. print(palindrome_string('Euston saw I was not Sue.') == 'eustonsawiwasnotsue')
  102. print(other_palindrome_test('Euston saw I was not Sue.') == 'eustonsawiwasnotsue')
  103. print(third_palindrome('Euston saw I was not Sue.') == 'eustonsawiwasnotsue')
  104. print(palindrome_string('When I entered, Euston saw I was not Sue.') == 'eustonsawiwasnotsue')
  105. print(other_palindrome_test('When I entered, Euston saw I was not Sue.') == 'eustonsawiwasnotsue')
  106. print(third_palindrome('When I entered, Euston saw I was not Sue.') == 'eustonsawiwasnotsue')
  107. print(palindrome_string('This is not a palindrome') == 'isi')
  108. print(other_palindrome_test('This is not a palindrome') == 'isi')
  109. print(third_palindrome('This is not a palindrome') == 'isi')
  110. print(palindrome_string('Marge let a moody baby doom a telegram.') == 'margeletamoodybabydoomatelegram')
  111. print(other_palindrome_test('Marge let a moody baby doom a telegram.') == 'margeletamoodybabydoomatelegram')
  112. print(third_palindrome('Marge let a moody baby doom a telegram.') == 'margeletamoodybabydoomatelegram')
  113. print(palindrome_string('No I am not your palindrome') is False )
  114. print(other_palindrome_test('No I am not your palindrome') is False)
  115. print(third_palindrome('No I am not your palindrome') is False)
  116. print(palindrome_string('With the days forever darkening, And the signs so very clear,We must keep our sense of urgency,'
  117. 'As the end is drawing near. We will seize this opportunity To declare Jehovah’s name, '
  118. 'And the blessings of his Kingdom Is the message we’ll proclaim.') == other_palindrome_test(
  119. 'With the days forever darkening, And the signs so very clear,We must keep our sense of urgency ,As the end is '
  120. 'drawing near. We will seize this opportunity To declare Jehovah’s name, And the blessings of his Kingdom '
  121. 'Is the message we’ll proclaim.') == third_palindrome('With the days forever darkening, And the signs so very clear,We must keep our sense of urgency,'
  122. 'As the end is drawing near. We will seize this opportunity To declare Jehovah’s name, '
  123. 'And the blessings of his Kingdom Is the message we’ll proclaim.'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement