Given a string S and an integer N as the input, the program must split the string into groups whose size is N and print them as the output in separate lines in a zig zag manner. If the last group size is less than N then the remaining letters must be filled with asterisk as shown in the Example Input/Output.
Input Format:
The first line contains S.
The second line contains N.
Output Format:
LENGTH(S)/N + LENGTH(S)%N lines containing the desired pattern.
Boundary Conditions:
4 <= LENGTH(S) <= 500
2 <= N <= LENGTH(S)
Example Input/Output 1:
Input:
ENVIRONMENT
3
Output:
ENV
ORI
NME
*TN
Example Input/Output 2:
Input:
ENVIRONMENT
4
Output:
ENVI
MNOR
ENT*
Example Input/Output 3:
Input:
EVERYDAY
2
Output:
EV
RE
YD
YA
Old Logic
n,m,l=list(input().strip()),int(input()),0
c=m
while len(n)%m!=0:
n.append('*')
d=len(n)
for i in range(d):
if i%2==0:
print(''.join(n[l:m]))
else:
print(''.join(n[l:m][::-1]))
l+=c
m+=c
New Logic
# Read the string and the integer value
string, N = input().strip(), int(input())
# Initialize a variable to keep track of the current position in the string
position = 0
# While there are characters left in the string
while position < len(string):
# Extract a substring of size N or whatever remains
substring = string[position:position+N]
# If the length of the substring is less than N, pad it with asterisks
while len(substring) < N:
substring += '*'
# If the position is even, print the substring as it is
if (position // N) % 2 == 0:
print(substring)
# If the position is odd, print the reversed substring
else:
print(substring[::-1])
# Move the position by N
position += N
Leave a Reply