String – First and Last Pattern: The program must accept a string S as the input. The program must print the characters of the string S in L lines (where L is the length of the string S) based on the following conditions.
– In the 1st line, the first and the last characters of the string S must be printed.
– In the 2nd line, the first two and the last two characters of the string S must be printed in reverse order.
– In the 3rd line, the first three and the last three characters of the string S must be printed in reverse order.
– Similarly, the program must print the remaining lines as the output.
Boundary Condition(s):
2 <= Length of S <= 100
Input Format:
The first line contains S.
Output Format:
The first L lines, each contains the characters of the string S based on the given conditions.
Example Input/Output 1:
Input:
orange
Output:
oe
roeg
aroegn
naroegna
gnaroegnar
egnaroegnaro
Explanation:
Here the length of the string orange is 6. So the pattern contains 6 lines.
o e (1st and 6th characters)
ro eg (first 2 and last 2 characters in reverse order)
aro egn (first 3 and last 3 characters in reverse order)
naro egna (first 4 and last 4 characters in reverse order)
gnaro egnar (first 5 and last 5 characters in reverse order)
egnaro egnaro (first 6 and last 6 characters in reverse order)
s=input().strip() for i in range(len(s)): p=s[:i+1] q=s[-i-1:] print(p[::-1]+q[::-1])
Leave a Reply