Lesson 2 - part B


Row and Column

Let's make a function that allow us to display the all the arrangements of the string in a table. Every table should be able to display data in fixed rows and columns. So, we need to create a function that take at least three parameters.

The first parameter is the list containing all the different arrangements of the word. The second and third parameter are the rows and columns of the table. Here's an example of the table (2 rows by 3 columns) that we are trying to make for the word "abc",

bca cab abc

bac acb cba


So, our algorithm to make a table is as below,

for each row :

for each column :

print out one of the possible combination.

What the algorithm is trying to tell us is that for each row, we have to iterate through the all the column and print out one of the possible combination.

However, we cannot use 'for' loop as 'for' loop is used to iterate through a list but we do not have a list (not counting the list containing strings) to iterate through. We only had two variables referring to two integer values, the row and column. So, we can use a 'while' loop instead.

Remember, when using a 'while' loop, we must make sure the loop would end sooner or later when the condition specified is not longer fulfilled. We can do this by using a counter. A counter is a variable that decrement or increment for every loop. The while loop would use this counter as it's condition.



For example,

var = 4

while var > 0:

print(var)

var= var -1



So we choose the condition of the while loop to be the integer referred by variable 'var' is more than 0. This while loop will end sooner or later because the var is decreasing by 1 for each loop. Now, we can make a draft code for the function.



Mountain View

What's happening here? Well,we initialized the variable 'oricolumn' to number of column, the third parameter. Then we use a 'while' loop with the condition of row being more than 0. In the first 'while' loop, we have another 'while' loop which condition is that column is more than 0. This is called the nested loop. For the row 'while' loop to be looped once, the column 'while' loop needed to be completed fully first.

First :

print(“Hello”,end = “ “)

print(“World”)

Hello World

Second :

print(“Hello”)

print(“World”)

Hello

World

See the difference?

Then, we use the words.pop(0) statement in order to delete the first string from the list. Do note that since we deleted the first string from the list, during the next loop, the words[0] refer to the second string which became the first string in the list due to deletion of the original first string.

After that, we decrease column by 1 so that the loop will end sooner or later. After the column 'while' loop is completed, we are to use print(“ “) twice. The first time we use print(“ “) is to ensure that the next time something is printed out, it will be printed out on the next line. The second time we use print(“ “) is to ensure that there's an additional spaces between the two consecutive rows for neatness.

The statement row = row -1 is used to ensure that row 'while' loop would stop when the variable row is reduced to 0. Opps! What about the last line? Well, you see , after the first time the column loop is completed, variable 'column' will now be 0. So the next we encounter the column 'while' loop, it will not iterate as it do not fulfilled the condition of being more than 0. So, how do we solve the problem?

Easy! Just initialised the column to the original column value. That's the reason for having the oricolumn.




Go to Lesson 2 Part C

Back to Lesson 2 Part A

Back to Index