Write a python function that takes a string as argument and returns the longest…
- User
Write a python function that takes a string as argument and returns the longest palindrome substring
- Assistant
def longest_palindrome_substring(string): # Initialize two variables to store the start and end index of the longest palindrome substring start_index = 0 end_index = 0 # Initialize a variable to store the length of the longest palindrome substring longest_length = 0 # Iterate through the string and find the longest palindrome substring for x in range(1, len(string)): # Initialize two variables to store the beginning and ending index of the current palindrome substring temp_start_index = x temp_end_index = x + 1 # Find the longest palindrome substring while (temp_start_index >= 0 and temp_end_index < len(string) and (string[temp_start_index] == string[temp_end_index])): temp_start_index = temp_start_index - 1 temp_end_index = temp_end_index + 1 # Update the lengths and start/end index of the longest palindrome substring if temp_start_index < 0: longest_length = longest_length + 1 else: longest_length = max(longest_length, x - start_index + 1) start_index = min(start_index, temp_start_index) end_index = min(end_index, temp_end_index) # Return the length and start/end index of the longest palindrome substring return longest_length, start_index, end_index
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.