Printing pattern without importing modules - python

Please help me print the pattern below as it is, if the input entered is 7:
1 1 1 1 1 1 1
1 2 2 2 2 2 1
1 2 3 3 3 2 1
1 2 3 4 3 2 1
1 2 3 3 3 2 1
1 2 2 2 2 2 1
1 1 1 1 1 1 1
I figured out to find the middle element of the pattern with any input:
rows=int(input("Enter the number of rows:"))
l=[]
for x in range(1,rows+1):
if x%2!=0:
l.append(x)
mid_value=len(l)
Please help me complete the above pattern......
Thanks in advance!

If you use a list-of-lists to store the values, the value for any specific cell can be determined by doing some basic math involving the cell indexes and the number of rows.
An illustration:
def cell_value(i, j, n_rows):
# The value of any cell is the minimum distance
# from its own coordinates (i, j) to the "outside" (ie,
# an i or j less than 0 or equal to n_rows). Imagine an
# ant in the grid. How many steps would it have to take
# to escape the grid, using the shortest route?
return min(
abs(i - -1),
abs(i - n_rows),
abs(j - -1),
abs(j - n_rows),
)
N_ROWS = 7
rows = [
[
cell_value(i, j, N_ROWS)
for j in range(N_ROWS)
]
for i in range(N_ROWS)
]
for r in rows:
print(*r)
Output:
1 1 1 1 1 1 1
1 2 2 2 2 2 1
1 2 3 3 3 2 1
1 2 3 4 3 2 1
1 2 3 3 3 2 1
1 2 2 2 2 2 1
1 1 1 1 1 1 1

This looks like a homework question, so I'm going to try and explain how to approach it rather than just provide code.
A few things worth noting to start:
- The pattern's symmetrical in both directions, so we can save some effort and logic by only solving the top-left quarter, and copying it to the rest.
- Each row is similar the the one before, with one added at the point where the row and column indices (i and j) are equal - rather than recalculate every row from scratch, we can take the one before as a base.
So, for the first row, make a list of 1s the length of your input (7, in this case).
Copy this for the seventh row (note: row6 = row0 won't create a copy; you'll need row6 = list(row0) )
For the second and sixth rows, take a copy of the first row. If i is equal to or greater than j and is in the first half of the row, add 1 to it. You'll need to copy that in reverse for the back half of the row. (Alternative - set the value to j+1 rather than just adding 1)
Repeat until the fourth row, and you should be done.
EDIT: code included, because it was an interesting problem
numberOfRows = int(input("Enter the number of rows:"))
listOut = [[1]*numberOfRows] * numberOfRows #grid of 1s of appropriate size
for j in range(int((numberOfRows+1)/2)): #symmetrical, so only look to the middle
if j > 0:
listOut[j] = list(listOut[j-1]) #copy previous row
for i in range(int((numberOfRows+1)/2)):
if i>=j:
listOut[j][i] = j+1
listOut[j][numberOfRows-(i+1)] = j+1
#copy current row to appropriate distance from the end
listOut[numberOfRows-(j+1)] = list(listOut[j])
for row in listOut:
# * for sequence unpacking, printing lists as strings w/o commas
print(*row)

It might not be the most elegant solution but something like this should work:
n = int(input('Enter the number of rows:'))
table = [[1 for _ in range(n)] for _ in range(n)]
start = 0
end = n
while start < end:
start += 1
end -= 1
for i in range(start, end):
for j in range(start, end):
table[i][j] += 1
for row in table:
print(' '.join(str(ele) for ele in row))

Simple implementation without using any list
n = int(input())
x = n
for i in range((n // 2 + 1) if n % 2 and n > 1 else n //2):
for l in range(1, i + 1):
print(l, end=' ')
print((str(i + 1) + ' ') * x, end='')
for r in range(i, 0, -1):
print(r, end=' ')
print()
x -= 2
y = 1
for j in range(n // 2, 0, -1):
for l in range(1, j):
print(l, end=' ')
print((str(j) + ' ') * (2 * y + 1 if n % 2 else 2 * y), end='')
for r in range(j-1, 0, -1):
print(r, end=' ')
print()
y += 1

Related

Finding a Pattern in a Grid Python [duplicate]

This question already has answers here:
Largest rectangle of 1's in 2d binary matrix
(6 answers)
Closed 2 years ago.
I have randomly generated grid containing 0 and 1:
1 1 0 0 0 1 0 1
1 1 1 0 1 1 1 1
1 0 0 0 1 0 1 1
0 0 1 0 1 0 1 1
1 1 1 1 0 0 1 1
0 0 1 1 1 1 1 0
0 1 0 0 1 0 1 1
How can I iterate through the grid to find the largest cluster of 1s, that is equal or larger than 4 items (across row and column)?
I assume I need to keep a count of each found cluster while iterating and ones its more than 4 items, record and count in a list and then find the largest number.
The problem is that I cannot figure out how to do so across both rows and columns and record the count. I have iterated through the grid but not sure how to move further than two rows.
For example in the above example, the largest cluster is 8. There are some other clusters in the grid, but they have 4 elements:
A A 0 0 0 1 0 1
A A 1 0 1 1 1 1
1 0 0 0 1 0 1 1
0 0 1 0 1 0 1 1
1 1 B B 0 0 1 1
0 0 B B 1 1 1 0
0 1 0 0 1 0 1 1
The code I tried:
rectcount = []
for row in range(len(grid)):
for num in range(len(grid[row])):
# count = 0
try:
# if grid[row][num] == 1:
# if grid[row][num] == grid[row][num + 1] == grid[row + 1][num] == grid[row + 1][num + 1]:
# count += 1
if grid[row][num] == grid[row][num + 1]:
if grid[row + 1][num] == grid[row][num + 1]:
count += 1
# if grid[row][num] == grid[row][num + 1] and grid[row][num] == grid[row + 1][num]:
# count += 1
else:
count = 0
if grid[row][num] == grid[row + 1][num]:
count += 1
except:
pass
I've implemented three algorithms.
First algorithm is Simple, using easiest approach of nested loops, it has O(N^5) time complexity (where N is one side of input grid, 10 for our case), for our inputs of size 10x10 time of O(10^5) is quite alright. Algo id in code is algo = 0. If you just want to see this algorithm jump to line ------ Simple Algorithm inside code.
Second algorithm is Advanced, using Dynamic Programming approach, its complexity is O(N^3) which is much faster than first algorithm. Algo id in code is algo = 1. Jump to line ------- Advanced Algorithm inside code.
Third algorithm Simple-ListComp I implemented just for fun, it is almost same like Simple, same O(N^5) complexity, but using Python's list comprehensions instead of regular loops, that's why it is shorter, also a bit slower because doesn't use some optimizations. Algo id in code is algo = 2. Jump to line ------- Simple-ListComp Algorithm inside code to see algo.
The rest of code, besides algorithms, implements checking correctness of results (double-checking between algorithms), printing results, producing text inputs. Code is split into solving-task function solve() and testing function test(). solve() function has many arguments to allow configuring behavior of function.
All main code lines are documented by comments, read them to learn how to use code. Basically if s variable contains multi-line text with grid elements, same like in your question, you just run solve(s, text = True) and it will solve task and print results. Also you may choose algorithm out of two versions (0 (Simple) and 1 (Advanced) and 2 (Simple-ListComp)) by giving next arguments to solve function algo = 0, check = False (here 0 for algo 0). Look at test() function body to see simplest example of usage.
Algorithms output to console by default all clusters, from largest to smallest, largest is signified by . symbol, the rest by B, C, D, ..., Z symbols. You may set argument show_non_max = False in solve function if you want only first (largest) cluster to be shown.
I'll explain Simple algorithm:
Basically what algorithm does - it searches through all possible angled 1s rectangles and stores info about maximal of them into ma 2D array. Top-left point of such rectangle is (i, j), top-right - (i, k), bottom-left - (l, j + angle_offset), bottom-right - (l, k + angle_offset), all 4 corners, that's why we have so many loops.
In outer two i (row) , j (column) loops we iterate over whole grid, this (i, j) position will be top-left point of 1s rectangle, we need to iterate whole grid because all possible 1s rectangles may have top-left at any (row, col) point of whole grid. At start of j loop we check that grid at (i, j) position should always contain 1 because inside loops we search for all rectangle with 1s only.
k loop iterates through all possible top-right positions (i, k) of 1s rectangle. We should break out of loop if (i, k) equals to 0 because there is no point to extend k further to right because such rectangle will always contain 0.
In previous loops we fixed top-left and top-right corners of rectangle. Now we need to search for two bottom corners. For that we need to extend rectangle downwards at different angles till we reach first 0.
off loop tries extending rectangle downwards at all possible angles (0 (straight vertical), +1 (45 degrees shifted to the right from top to bottom), -1 (-45 degrees)), off basically is such number that grid[y][x] is "above" (corresponds to by Y) grid[y + 1][x + off].
l tries to extend rectangle downwards (in Y direction) at different angles off. It is extended till first 0 because it can't be extended further then (because each such rectangle will already contain 0).
Inside l loop there is if grid[l][max(0, j + off * (l - i)) : min(k + 1 + off * (l - i), c)] != ones[:k - j + 1]: condition, basically this if is meant to check that last row of rectangle contains all 1 if not this if breaks out of loop. This condition compares two list slices for non-equality. Last row of rectangle spans from point (l, j + angle_offset) (expression max(0, j + off * (l - i)), max-limited to be 0 <= X) to point (l, k + angle_offset) (expression min(k + 1 + off * (l - i), c), min-limited to be X < c).
Inside l loop there are other lines, ry, rx = l, k + off * (l - i) computes bottom-right point of rectangle (ry, rx) which is (l, k + angle_offset), this (ry, rx) position is used to store found maximum inside ma array, this array stores all maximal found rectangles, ma[ry][rx] contains info about rectangle that has bottom-right at point (ry, rx).
rv = (l + 1 - i, k + 1 - j, off) line computes new possible candidate for ma[ry][rx] array entry, possible because ma[ry][rx] is updated only if new candidate has larger area of 1s. Here rv[0] value inside rv tuple contains height of such rectangle, rv[1] contains width of such rectangle (width equals to the length of bottom row of rectangle), rv[2] contains angle of such rectangle.
Condition if rv[0] * rv[1] > ma[ry][rx][0] * ma[ry][rx][1]: and its body just checks if rv area is larger than current maximum inside array ma[ry][rx] and if it is larger then this array entry is updated (ma[ry][rx] = rv). I'll remind that ma[ry][rx] contains info (width, height, angle) about current found maximal-area rectangle that has bottom-right point at (ry, rx) and that has these width, height and angle.
Done! After algorithm run array ma contains information about all maximal-area angled rectangles (clusters) of 1s so that all clusters can be restored and printed later to console. Largest of all such 1s-clusters is equal to some rv0 = ma[ry0][rx0], just iterate once through all elements of ma and find such point (ry0, rx0) so that ma[ry0][rx0][0] * ma[ry0][rx0][1] (area) is maximal. Then largest cluster will have bottom-right point (ry0, rx0), bottom-left point (ry0, rx0 - rv0[1] + 1), top-right point (ry0 - rv0[0] + 1, rx0 - rv0[2] * (rv0[0] - 1)), top-left point (ry0 - rv0[0] + 1, rx0 - rv0[1] + 1 - rv0[2] * (rv0[0] - 1)) (here rv0[2] * (rv0[0] - 1) is just angle offset, i.e. how much shifted is first row along X compared to last row of rectangle).
Try it online!
# ----------------- Main function solving task -----------------
def solve(
grid, *,
algo = 1, # Choose algorithm, 0 - Simple, 1 - Advanced, 2 - Simple-ListComp
check = True, # If True run all algorithms and check that they produce same results, otherwise run just chosen algorithm without checking
text = False, # If true then grid is a multi-line text (string) having grid elements separated by spaces
print_ = True, # Print results to console
show_non_max = True, # When printing if to show all clusters, not just largest, as B, C, D, E... (chars from "cchars")
cchars = ['.'] + [chr(ii) for ii in range(ord('B'), ord('Z') + 1)], # Clusters-chars, these chars are used to show clusters from largest to smallest
one = None, # Value of "one" inside grid array, e.g. if you have grid with chars then one may be equal to "1" string. Defaults to 1 (for non-text) or "1" (for text).
offs = [0, +1, -1], # All offsets (angles) that need to be checked, "off" is such that grid[i + 1][j + off] corresponds to next row of grid[i][j]
debug = False, # If True, extra debug info is printed
):
# Preparing
assert algo in [0, 1, 2], algo
if text:
grid = [l.strip().split() for l in grid.splitlines() if l.strip()]
if one is None:
one = 1 if not text else '1'
r, c = len(grid), len(grid[0])
sgrid = '\n'.join([''.join([str(grid[ii][jj]) for jj in range(c)]) for ii in range(r)])
mas, ones = [], [one] * max(c, r)
# ----------------- Simple Algorithm, O(N^5) Complexity -----------------
if algo == 0 or check:
ma = [[(0, 0, 0) for jj in range(c)] for ii in range(r)] # Array containing maximal answers, Lower-Right corners
for i in range(r):
for j in range(c):
if grid[i][j] != one:
continue
for k in range(j + 1, c): # Ensure at least 2 ones along X
if grid[i][k] != one:
break
for off in offs:
for l in range(i + 1, r): # Ensure at least 2 ones along Y
if grid[l][max(0, j + off * (l - i)) : min(k + 1 + off * (l - i), c)] != ones[:k - j + 1]:
l -= 1
break
ry, rx = l, k + off * (l - i)
rv = (l + 1 - i, k + 1 - j, off)
if rv[0] * rv[1] > ma[ry][rx][0] * ma[ry][rx][1]:
ma[ry][rx] = rv
mas.append(ma)
ma = None
# ----------------- Advanced Algorithm using Dynamic Programming, O(N^3) Complexity -----------------
if algo == 1 or check:
ma = [[(0, 0, 0) for jj in range(c)] for ii in range(r)] # Array containing maximal answers, Lower-Right corners
for off in offs:
d = [[(0, 0, 0) for jj in range(c)] for ii in range(c)]
for i in range(r):
f, d_ = 0, [[(0, 0, 0) for jj in range(c)] for ii in range(c)]
for j in range(c):
if grid[i][j] != one:
f = j + 1
continue
if f >= j:
# Check that we have at least 2 ones along X
continue
df = [(0, 0, 0) for ii in range(c)]
for k in range(j, -1, -1):
t0 = d[j - off][max(0, k - off)] if 0 <= j - off < c and k - off < c else (0, 0, 0)
if k >= f:
t1 = (t0[0] + 1, t0[1], off) if t0 != (0, 0, 0) else (0, 0, 0)
t2 = (1, j - k + 1, off)
t0 = t1 if t1[0] * t1[1] >= t2[0] * t2[1] else t2
# Ensure that we have at least 2 ones along Y
t3 = t1 if t1[0] > 1 else (0, 0, 0)
if k < j and t3[0] * t3[1] < df[k + 1][0] * df[k + 1][1]:
t3 = df[k + 1]
df[k] = t3
else:
t0 = d_[j][k + 1]
if k < j and t0[0] * t0[1] < d_[j][k + 1][0] * d_[j][k + 1][1]:
t0 = d_[j][k + 1]
d_[j][k] = t0
if ma[i][j][0] * ma[i][j][1] < df[f][0] * df[f][1]:
ma[i][j] = df[f]
d = d_
mas.append(ma)
ma = None
# ----------------- Simple-ListComp Algorithm using List Comprehension, O(N^5) Complexity -----------------
if algo == 2 or check:
ma = [
[
max([(0, 0, 0)] + [
(h, w, off)
for h in range(2, i + 2)
for w in range(2, j + 2)
for off in offs
if all(
cr[
max(0, j + 1 - w - off * (h - 1 - icr)) :
max(0, j + 1 - off * (h - 1 - icr))
] == ones[:w]
for icr, cr in enumerate(grid[max(0, i + 1 - h) : i + 1])
)
], key = lambda e: e[0] * e[1])
for j in range(c)
]
for i in range(r)
]
mas.append(ma)
ma = None
# ----------------- Checking Correctness and Printing Results -----------------
if check:
# Check that we have same answers for all algorithms
masx = [[[cma[ii][jj][0] * cma[ii][jj][1] for jj in range(c)] for ii in range(r)] for cma in mas]
assert all([masx[0] == e for e in masx[1:]]), 'Maximums of algorithms differ!\n\n' + sgrid + '\n\n' + (
'\n\n'.join(['\n'.join([' '.join([str(e1).rjust(2) for e1 in e0]) for e0 in cma]) for cma in masx])
)
ma = mas[0 if not check else algo]
if print_:
cchars = ['.'] + [chr(ii) for ii in range(ord('B'), ord('Z') + 1)] # These chars are used to show clusters from largest to smallest
res = [[grid[ii][jj] for jj in range(c)] for ii in range(r)]
mac = [[ma[ii][jj] for jj in range(c)] for ii in range(r)]
processed = set()
sid = 0
for it in range(r * c):
sma = sorted(
[(mac[ii][jj] or (0, 0, 0)) + (ii, jj) for ii in range(r) for jj in range(c) if (ii, jj) not in processed],
key = lambda e: e[0] * e[1], reverse = True
)
if len(sma) == 0 or sma[0][0] * sma[0][1] <= 0:
break
maxv = sma[0]
if it == 0:
maxvf = maxv
processed.add((maxv[3], maxv[4]))
show = True
for trial in [True, False]:
for i in range(maxv[3] - maxv[0] + 1, maxv[3] + 1):
for j in range(maxv[4] - maxv[1] + 1 - (maxv[3] - i) * maxv[2], maxv[4] + 1 - (maxv[3] - i) * maxv[2]):
if trial:
if mac[i][j] is None:
show = False
break
elif show:
res[i][j] = cchars[sid]
mac[i][j] = None
if show:
sid += 1
if not show_non_max and it == 0:
break
res = '\n'.join([''.join([str(res[ii][jj]) for jj in range(c)]) for ii in range(r)])
print(
'Max:\nArea: ', maxvf[0] * maxvf[1], '\nSize Row,Col: ', (maxvf[0], maxvf[1]),
'\nLowerRight Row,Col: ', (maxvf[3], maxvf[4]), '\nAngle: ', ("-1", " 0", "+1")[maxvf[2] + 1], '\n', sep = ''
)
print(res)
if debug:
# Print all computed maximums, for debug purposes
for cma in [ma, mac]:
print('\n' + '\n'.join([' '.join([f'({e0[0]}, {e0[1]}, {("-1", " 0", "+1")[e0[2] + 1]})' for e0_ in e for e0 in (e0_ or ('-', '-', 0),)]) for e in cma]))
print(end = '-' * 28 + '\n')
return ma
# ----------------- Testing -----------------
def test():
# Iterating over text inputs or other ways of producing inputs
for s in [
"""
1 1 0 0 0 1 0 1
1 1 1 0 1 1 1 1
1 0 0 0 1 0 1 1
0 0 1 0 1 0 1 1
1 1 1 1 0 0 1 1
0 0 1 1 1 1 1 0
0 1 0 0 1 0 1 1
""",
"""
1 0 1 1 0 1 0 0
0 1 1 0 1 0 0 1
1 1 0 0 0 0 0 1
0 1 1 1 0 1 0 1
0 1 1 1 1 0 1 1
1 1 0 0 0 1 0 0
0 1 1 1 0 1 0 1
""",
"""
0 1 1 0 1 0 1 1
0 0 1 1 0 0 0 1
0 0 0 1 1 0 1 0
1 1 0 0 1 1 1 0
0 1 1 0 0 1 1 0
0 0 1 0 1 0 1 1
1 0 0 1 0 0 0 0
0 1 1 0 1 1 0 0
"""
]:
solve(s, text = True)
if __name__ == '__main__':
test()
Output:
Max:
Area: 8
Size Row,Col: (4, 2)
LowerRight Row,Col: (4, 7)
Angle: 0
CC000101
CC1011..
100010..
001010..
1BBB00..
00BBBDD0
010010DD
----------------------------
Max:
Area: 6
Size Row,Col: (3, 2)
LowerRight Row,Col: (2, 1)
Angle: -1
10..0100
0..01001
..000001
0BBB0101
0BBB1011
CC000100
0CC10101
----------------------------
Max:
Area: 12
Size Row,Col: (6, 2)
LowerRight Row,Col: (5, 7)
Angle: +1
0..01011
00..0001
000..010
BB00..10
0BB00..0
001010..
10010000
01101100
----------------------------

Trying to solve increment a double list in python by an index

I am trying to solve this problem.This is leet code 1252.
Basically you have a matrix and index.
Say you have a matrix
0 0 0
0 0 0
and you have an index
0 1
1 1
The left side of the index is which row value you increment by one and the right hand side is which columns value you increment by one.
Well the you increment the 0 row by one and the 1 row by so you get
1 1 1
1 1 1
and then you look at the let side of the index and that means you increment the 1 column by one and the one colums by one so you get
1 2 1
1 2 1
1 3 1
1 3 1
I tried to solve this with the following code
def matrix(n,m,index):
nums=[[0]*m]*n
print(nums)
a=len(nums)
b=len(nums[0])
for i in range(a):
c=index[i][0]
print("the value of c is ",c)
for j in range(b):
nums[c][j]=nums[c][j]+1
print(nums)
for j in range(a):
c=index[1][j]
print(c)
for i in range(a):
nums[i][c]=nums[i][c]+1
print(nums)
index=[[0,1],[1,1]]
n=2
m=3
matrix(n,m,index)
but I end up getting
2 6 2
2 6 2
for my input
0 0 0
0 0 0
the weird thing is Try the same code and get the correct answer.
def inc(nums):
print(nums)
a=len(nums)
b=len(nums[0])
for i in range(a):
c=index[i][0]
for j in range(b):
nums[c][j]=nums[c][j]+1
print(nums)
for j in range(a):
c=index[1][j]
print(c)
for i in range(a):
nums[i][c]=nums[i][c]+1
print(nums)
a=[[0,0,0],[0,0,0]]
index=[[0,1],[1,1]]
inc(a)
the correct answer is
1 3 1
1 3 1
Instead of instantly updating matrix, take 2 lists for row changes and column changes initialized with zero.
Traverse through index list and make changes to row and column lists:
initially
r = [0,0] , c = [0,0,0]
index -> 0 1
r = [1,0] , c = [0,1,0]
index -> 1 1
r = [1,1] , c = [0,2,0]
then, at last, you increment each row with the corresponding value in row list and same with column list.
This is an optimised solution too.
Here's my code :
n = 2
m = 3
index = [[0,1],[1,1]]
mat = [[0 for i in range(m)] for j in range(n)]
r = [0 for i in range(n)]
c = [0 for i in range(m)]
for ri,ci in index:
r[ri]+=1
c[ci]+=1
for i in range(n):
mat[i] = [r[i]]*m
for i in range(m):
if c[i]>0:
for j in range(n):
mat[j][i] += c[i]
print(mat)
Output:
[[1, 3, 1],
[1, 3, 1]]

Python program that prints this pattern with n rows, where n is input by the user. In this example output, n = 6

I tried to workout some code but I am not able to meet the expected output. The expected output looks like this :
Here is what I've tried :
n = int(input())
for row in range(1, n + 1):
print(' ' * (n - row), end = '')
for col in range(row, 0 , -1):
print(col, end = ' ')
print()
This is the printout of your code (when the user inputs 6):
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
As you can see, there are not enough spaces at the top. The first row now has 5 spaces, but it needs another 5 spaces to push the 1 all the way to the right. The second row has 4 spaces but needs another 4. In fact, you need to double the number of spaces at the start of each row. So just multiply the number of desired spaces by two:
n = int(input())
for row in range(1, n + 1):
print(' ' * 2 * (n - row), end = '')
for col in range(row, 0 , -1):
print(col, end = ' ')
print()
Do you see the * 2 that I added to your code? The printout is now
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
which is what you want.
There are other ways to get what you want. For example, rather than adding * 2 to that line of code, you could replace the single space with two spaces:
print(' ' * (n - row), end = '')
That also works and looks better.

How to invert the printed 'tirangle' output in a nested loop function

so for something like this:
for j in range(n):
for i in range(j+1):
print(i, end = ' ')
It would print:
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
How can i invert it to look like this:
0 1 2 3 4
1 2 3 4
2 3 4
3 4
4
You want to count down in the outer loop instead of up. Use
for j in range(n - 1, -1, -1):
instead of for j in range(n) to count from n - 1 to 0 instead of from 0 to n-1
Edit (following the question edit)
Your inner loop should be counting up from j to n - 1. Then, you also won't need to invert the outer loop:
for j in range(n):
for i in range(j, n): # From `j` to `n - 1`
print(i, end=' ')
print() # for the newline

How do I get rid of the zero at the beginning of the output of the following code?

for i in range(7,0,-1):
for i in range(i-1):
print i,
print
The above code prints:
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0
I want to get rid of the zeroes in the first 4 lines.
Start your range at 1 instead of the default 0:
for i in range(1, i-1):
print i,
Instead of ranging to i - 1, adjust your outer loop to produce the right values but increase the end-point; there is little point in producing a range from 1 to 1 or from 1 to 0.
for i in range(6, 1, -1):
for i in range(1, i):
print i,
print
This saves you from printing too many empty newlines at the end:
>>> for i in range(6, 1, -1):
... for i in range(1, i):
... print i,
... print
...
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
You could also use print i + 1 instead of starting at 1; decrement the values in the outer loop in that case:
for i in range(5, 0, -1):
for i in range(i):
print i + 1,
print

Categories