String Zig-Zag Pattern-Python - Spicy Coders

Recent

Friday, September 08, 2017

String Zig-Zag Pattern-Python

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

 Source Code:
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

2 comments:

  1. Not executing
    Error arising in print statement
    🤔🤔🤔🤔

    ReplyDelete
  2. Print("".join(n[l:m]))
    Else:
    Print("".join(n[l:m][::-1]))
    if m>=len(n):
    exit(0)
    l+=c
    m+=c

    ReplyDelete