The spacing in your desired output is not clear. I'll assume you want each number to take 4 spaces, as in your attempted code. Here is Python 3.6.1 code that uses multiple ranges and avoids lists, dictionaries, comprehensions, and joins, as your code also does. Those features might make the code more concise. This code does use a Boolean flag variable and a range using a ternary operator.
rows = 4
cols = 6
increasing = True
for j in range(rows * cols, 0, -cols):
increasing = not increasing
for k in range(j-cols+1, j+1) if increasing else range(j, j-cols, -1):
print('{0:4d}'.format(k), sep='', end='')
print()
I get the result:
24 23 22 21 20 19
13 14 15 16 17 18
12 11 10 9 8 7
1 2 3 4 5 6
Here is a version that does not use a Boolean flag variable, directly using the largest value in a row to decide if that row is in increasing or decreasing order:
rows = 4
cols = 6
for j in range(rows * cols, 0, -cols):
for k in range(j-cols+1, j+1) if j // cols % 2 else range(j, j-cols, -1):
print('{0:4d}'.format(k), sep='', end='')
print()
This code is two lines shorter but less clear than my previous code. For 4 rows and 6 columns that gives the same output as my previous code. However, my previous code always prints the first row in decreasing order, while this code always prints the last row in increasing order. You only see the difference if the number of rows is odd, and your problem description does not make it clear which you want. This last code could be modified to always agree with my first code but that would make it more complicated.
A Boolean value is a truth value, with only two possible values, encoded into a computer. In Python the two values are True
and False
. The type is named after George Boole who first made an algebra of logic. A flag variable is a variable that holds one of two possible values, often but not always Boolean values. It represents some state of the system and communicates that state to parts of the program. It is like the "flag" on old-fashioned American postal boxes which were raised to show that the box contained outgoing mail and lowered to show no outgoing mail. Many railroad systems had similar flags--only two states, showing/raised or not showing/not raised.
In my first code, increasing
is a Boolean flag variable. It represents the direction that the numbers in the "current row" are to be printed--True
means increasing order, while False
means decreasing order. That flag is reversed for each line, to get the desired "snake pattern." I start it out by setting it to True
(increasing order) before any rows are examined, so the first printed row will be printed in decreasing order. The state of that flag is used to choose the proper range for the row--the two possible ranges for a row have the same values but in a different order.
In my second code, I avoided a flag variable by directly calculating the proper order for a row by examining the value of j
, the largest number in the row. I divide that by the number of columns, yielding the row number for the current row, where 1
is the last row. I then see if that row number is even or odd by checking the remainder after dividing the row number by two. All that is done in the expression j // cols % 2
. If that value is odd, the remainder is one, and Python considers any non-zero number to be "Truthy" and treats that expression as True
. If the row number is even, the calculated remainder is zero and thus "Falsey" and is treated as False
. So I get a logical result without using a flag variable or any Boolean value at all. As I said, this is shorter than my first code but harder to understand.