0
Could any one Help me in solving this
python code to check whether the given string is a magic string or not. A string is said to be a magic string if the substringsformed by characters ven positions and odd positions are the same. If the string is of odd length, the middle character has to be shipped or example, the string aabbcc is a magic string as the substring 'abc that is formed using character at even position and the substring 'abe that is formed using character at odd position is same. Howeve tring 'abcabe' is not a magic string.
4 odpowiedzi
+ 6
can you show us the code so we can help you with it better.
0
im not getting any idea
0
s=input("enter the string")
b=len(s)
For i in range (0,b):
print (s[i]+s[i], end="")
0
Karthik, what did you mean "If the string is of odd length, the middle character has to be shipped"? it means it should be ignored or something else?
(Edit)
Is what you're looking for, this code checks characters on even & odd position, and ignores middle position if string length is odd:
sample, oddPart, evenPart = "aabxbcc", "", ""
pos, l = 0, len(sample)
midpos = l // 2 if l & 1 else -1
for char in sample:
if pos == midpos:
pos += 2 # ignore middle character
continue
if pos & 1:
oddPart += char
else:
evenPart += char
pos += 1
if oddPart == evenPart:
print("{} is magic number".format(sample))
else:
print("{} is not magic number".format(sample))