

ZIGZAG LINES IN WORD HOW TO
Here are some tips on how to effectively use the word "zigzag" in a sentence: Whether you are writing a story, describing a scene, or simply trying to add some flair to your sentences, incorporating the word "zigzag" can bring a dynamic element to your writing. Once that is done, we use the built-in join() function to join the three elements in result into a single string and return this string as the result.Zigzag is a versatile word that can be used in various contexts to describe a specific pattern or movement. This sequence of modifying the value of rowNumber and direction repeats until the end of the given string is reached. The next statement ( rowNumber += direction) becomes rowNumber = rowNumber + 1, which results in rowNumber being incremented by 1. As a result, the else block is executed and direction is changed to 1. Both the if and elif blocks evaluate to False. When we iterate to the next character 'R', we concatenate 'R' to result. Next, as rowNumber 0 and direction equals -1. Within the for loop, we first concatenate 'P' to result. When the function first starts, result =, rowNumber = 0 and direction = 1. Suppose numRows equals 3 and s equals 'PROGRAMMING'. Let’s look at a concrete example of how this works. Once we finish iterating through the entire string, we concatenate each of the elements in result using the join() method and return the result. Last but not least, if we are moving down and have reached the last row, or we are moving up and have reached the first row, we change the value of direction ( direction *= -1) and move up or down accordingly. rowNumber 0), we move up ( rowNumber -= 1). If direction equals 1 and we have not reached the last row (i.e. Next, we determine if we should move up or down to the next row. To concatenate the current letter ( s) to the current row (whose row number is tracked by the variable rowNumber). from row 2 to row 1).Īfter declaring the variables, we use a for loop to iterate through the string one character at a time. from row 0 to row 1) while -1 indicates that we are moving up (e.g. Positive 1 indicates that we are moving down (e.g. RowNumber tracks the current row to assign letters to, while direction tracks whether we are moving vertically down or diagonally up the zigzag. Next, we declare two variables called rowNumber and direction. For instance, if numRows equals 3, result =. The number of elements in result depends on the value of numRows.

Within the function, we first declare and initialize a list called result using the statement Here, we declare a function called convert with two parameters – s and numRows. Solutionįor i in range(len(s)): #looping through each element Note that in the description above, we refer to the first, second and third rows as row 0, row 1 and row 2 respectively, to be in line with the zero-based indexing used in programming. For instance, if the number of rows is 3 and the string is PROGRAMMING, we can iterate through the string and assign ‘P’ to row 0, ‘R’ to row 1, ‘O’ to row 2, ‘G’ to row 1, ‘R’ to row 0, ‘A’ to row 1 etc. One easy way to do it is to iterate through the given string one character at a time and assign the character to the correct row. This program takes in two arguments – the original string and the number of rows. In other words, if given the word PROGRAMMING and asked to scramble it into 3 rows of zigzag letters, our program should return the string PRIRGAMNOMG. Our job is to write a program to scramble a given string using the zigzag layout described above. If we write the word in a zig zag fashion using 4 rows, we’ll get Layout, we can then concatenate each row to get the string

If we write this word in a zig zag fashion using 3 rows, we’ll get Today, we have another interesting challenge from Leetcode ( ) we’re going to scramble a given word following a zig zag fashion.įor instance, let’s consider the word PROGRAMMING.
