Zig-Zag Conversion

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 82 :-  Zig-Zag Conversion

The Zig-Zag Conversion is a text transformation problem where you are given a string and a number of rows, and you need to convert the string into a zig-zag pattern with the given number of rows. You can then read the transformed string row by row to obtain the final result.

Here's a Python function to perform the Zig-Zag Conversion:

pythonCopy code

def zigzag_conversion(s, num_rows):    if num_rows == 1 or len(s) <= num_rows:        return s    result = [''] * num_rows    row, step = 0, 1    for char in s:        result[row] += char        if row == 0:            step = 1        elif row == num_rows - 1:            step = -1        row += step    return ''.join(result) # Example usage: input_str = "PAYPALISHIRING" num_rows = 3 output_str = zigzag_conversion(input_str, num_rows) print(output_str)

Output:

arduinoCopy code

"PAHNAPLSIIGYIR"

In this example, the function zigzag_conversion takes the input string s and the number of rows num_rows. It initializes an array of strings called result, where each element corresponds to a row in the Zig-Zag pattern. It then iterates through the characters of the input string and appends each character to the appropriate row in the result array.

The step variable controls the direction of movement while traversing the Zig-Zag pattern. When row is at the top or bottom row, the step is reversed to change direction.

Finally, the function joins the rows of the result array to create the final Zig-Zag converted string and returns it.

Note that if num_rows is equal to 1 or the length of the input string is less than or equal to num_rows, the function returns the original string as the Zig-Zag pattern with a single row is the same as the original string.

11. Week5 - Assignments

Comments: 2

profile
@mk.info.work
17-Feb-2024, 10:20 PM

SCIAKU Team please upload 1st video of TREE please please please, please

profile
@na3744
23-Feb-2024, 02:52 AM

I bought this course, it worth it!

profile
@mk.info.work
15-Nov-2023, 10:25 PM

Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it

profile
@sciaku1
11-Jan-2024, 03:23 PM

Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?