Python3: Write the function longestSubpalindrome(s), that takes a string s and returns the longest palindrome that occurs as consecutive characters (not just letters, but any characters) in s.
so longestSubpalindrome("ab-4-be!!!") returns "b-4-b".If there is a tie, return the lexicographically larger value -- in Python, a string s1 is lexicographically greater than a string s2 if (s1 > s2).
So: longestSubpalindrome("abcbce") returns "cbc", since ("cbc" > "bcb"). Note that unlike the previous functions, this function is case-sensitive (so "A" is not treated the same as "a" here).
Also, from the explanation above, we see that longestSubpalindrome("aba") is "aba", and longestSubpalindrome("a") is "a".
Restrictions: for loop, slicing, 'in' keyword, list comprehension, lambda function can't be used. It is recommended to use while loop to solve this problem.
hints: pick all possible starting and ending positions for a substring, and verify if it is a palindrome.
Please find the python to find the longest Sub Palindome from a given string in the function argument.
Note:
1. We can count the string length using string indices and stop counting when there is no more characters left.
2. This program will return the same character if the length of the given string is 1
3. If the given length of string is more than 1 and no palindrome found then it returns None.
4. I have used two apis to calculate the count and reverse of a string
Program:
#method to calculate the string length
def stringCount(string):
count=0
#count the number of characters in the
string
while True:
try:
count=count+1
if not
string[count]:
break
except IndexError:
break
return count
#api to revrse the string
def stringReverse(string):
count = stringCount(string)
temp=""
count = count -1
while count >= 0:
temp=temp+string[count]
count = count -1
return temp
#method to find the longestSubpalindrome
def longestSubpalindrome(string):
oriCount = stringCount(string)
index = 0
innerIndex = 0
curPalindrome = ""
if oriCount == 1:
return string
prevPalindrome = string[index]
curPalindrome = string[index]
index=0
#outer loop to iterate over the
string
while index < oriCount:
#print(string[index])
innerIndex = index
if index != 0:
curPalindrome = ""
#inner loop to
indentify the sub palindrome
while innerIndex <
oriCount:
if innerIndex != 0:
curPalindrome = curPalindrome+ string[innerIndex]
#if its a palindrome then check previous palindrome matches with
current
if curPalindrome == stringReverse(curPalindrome):
#following check indentifies the which palindrome is greater in
size
if stringCount(prevPalindrome) <
stringCount(curPalindrome):
prevPalindrome = curPalindrome
elif stringCount(prevPalindrome) ==
stringCount(curPalindrome):
if prevPalindrome < curPalindrome:
prevPalindrome = curPalindrome
innerIndex = innerIndex + 1
index = index + 1
if stringCount(prevPalindrome) == 1:
#return none
because the string doesnt have prevPalindrome
return None
return prevPalindrome
#testcases to validate the string whether it has longest sub palindrome
#following are palidrome
print(longestSubpalindrome("a"))
print(longestSubpalindrome("abacbbc"))
print(longestSubpalindrome("ab-4-be!!!"))
print(longestSubpalindrome("abcbce"))
print(longestSubpalindrome("NIXIN"))
#following are not a palidrome and returns
None
print(longestSubpalindrome("Lebron"))
print(longestSubpalindrome("Kyrie"))
Output:
a
cbbc
b-4-b
cbc
NIXIN
None
None
Screen Shot:



Python3: Write the function longestSubpalindrome(s), that takes a string s and returns the longest palindrome that...
Python3 : Write the function longestRun(s, chars) that takes a possibly-empty string s and a second possibly-empty string of chars. We will say that a character is "good" if it is in the chars string (case insensitively, so "A" and "a" would match). The function should return the length of the longest consecutive run of good characters in the given string s. For example, consider: longestRun("abbcazBbcababb","bz"). This returns 3 (look for "zBb"). Restrictions: for loop, slicing cannot be used, if...
Python3: Write the function findLongestStreak(lst) which takes a list of numbers, lst, and returns the longest streak of numbers which occur in the list. A streak of numbers is a list of numbers where each number is one greater than the one that occurred before it- for example, [4, 5, 6, 7]. So findLongestStreak([3, 4, 8, 2, 3, 4, 5, 7, 8, 9]) would return [2, 3, 4, 5]. If there is more than one streak with the longest length,...
41. (10 points) EXTRA CREDIT: Write a function 'palindrome' to determine if an input string s is a palindrome. Let the empty string be (technically) Aba' is not a palindrome. You may not use the "reversed" string function, you may use either palindrome. Be case sensitive, e.g. a looping control flow to solve the problem. recursion or
Write a function check palindrome, which takes a string x as argument and which returns True if x is a palindrome, and False otherwise. A palindrome is a word that reads the same backwards as forwards (like for example “racecar”). Your function should be recursive (i.e. call itself) and proceed as follows. 1. If x is a string of length 0 or 1 return True. 2. If the rst and the last letter of x are di erent, return False....
Write the function in python and show the code
2. Write a function jscore (s1, s2) that takes two strings s1 and s2 as inputs and returns the Jotto score of s1 compared with s2 -.e., the number of characters in s1 that are shared by s2. The positions and the order of the shared characters within each string do not matter. Repeated letters are counted multiple times, as long as they appear multiple times in both strings. For example...
(Longest common prefix, C-string, loop, char comparison) Write the prefix function to find the longest prefix of two strings using C-strings with the following header: void prefix( const char s1[ ], const char s2[ ], char commonPrefix[ ]) Write a test program that prompts the user to enter two C-strings and displays their common prefix. Sample run :- String 1: Programming is fun String 2: Program logic The common prefix is program.
Write a function vowelCount(S) that takes a string, S, and returns a new dictionary containing entries of the form v:N where v is one of ’aeiou’ and N is the number of occurrances of v in S.Your dictionary should never contain any entries where N is 0; thus, for example, if the S is "wipeout", the dictionary returned would be {′e′:1,′i′:1,′o′:1,′u′:1 }and not {′a′:0,′e′:1,′i′:1,′o′:1,′u′:1}. NOTE: must use a comprehension
Using python, create a program using nested loops to write a function called substring(s,b) that returns True or False depending on whether or not b is a substring of s. For example: String: "Diamond" Substring: "mond" The code will then state that this is true and "mond" is a substring of "Diamond". Example of what the program should output: Enter string: Tree Enter substring: Tre Yes, 'Tre' is a substring of 'Tree' Limitation: This program cannot use "find()" and "in"...
Write a function called double_str that takes a string argument and returns a new string that has all of the same characters as the argument, except each one is repeated twice. For example: double_str('dog') --> 'ddoogg' looking for python coding below is what I have so far double_str = 'robert' double_str 'robert' for i in range(len(double_str)): print(double_str[i])
Write a function is_mirror(s) that takes as input a string s and returns True if s is a mirrored string (i.e., a string that could have been produced by your mirror function) and False otherwise. Examples: >>> is_mirror('baconnocab') result: True >>> is_mirror('baconnoca') result: False Warning Your function should return a boolean value – either True or False, without any quotes around them. If you see quotes around the result when you make the calls above from the console, you must...