0
r, c = input().split()
r=int(r)
c=int(c)
list1=[]
v=1
for i in range(r):
    list2=[]
    for j in range(c):
        list2.append(v)
        v=v+1
    list1.append(list2)


for i in range(r):
    for j in range(c):
        print(list1[i][j],end=" ")
    print()        

Here is an image showing the actual output and the output I am getting:

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Afzal Sayyed
  • 13
  • 1
  • 6
  • Possible duplicate of https://stackoverflow.com/questions/493386/how-to-print-without-newline-or-space – David Hempy Sep 06 '18 at 04:47
  • I'd recommend accumulating each line of output as elements in an array, then joining the array with newline separators. Trying to do something different on the *last* iteration of a loop always results in messy, complicated code. – David Hempy Sep 06 '18 at 04:49
  • Possible duplicate of [How to print without newline or space?](https://stackoverflow.com/questions/493386/how-to-print-without-newline-or-space) – Roy Scheffers Sep 06 '18 at 04:52
  • Don't post screenshots of terminal output. Use code formatting. – Mad Physicist Sep 06 '18 at 05:08
  • 1
    What benefit have Hackerrank-questions if you do not solve them yourself? – Patrick Artner Sep 06 '18 at 05:22

3 Answers3

1

The issue is that you need to skip the newline at the end of the outermost loop and the spaces at the end of each line. For a general iterator, this requires a bit of extra work, but for your simple case, just checking iand j will suffice:

for i in range(r):
    for j in range(c):
        print(list1[i][j], end=" " if j < c - 1 else "")
    if i < r - 1:
        print()
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
1

I had the same issue, this is what I did: >>> help(print)

Help on built-in function print in module builtins:
print(...)

    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

I am very new to python, but this is my code to eliminate a new line at the end of a print statement:

for ch in message:
    print (ord(ch), end=' ')

If I wanted to eliminate the ' ' at the end of each line of my statements, since this comes from the default (sep=" "), then I would use the following:

for ch in message:
        print (ord(ch), ch, sep = '' if ch==message[-1] else ' ', end=' ', )

#Please note that message is a string.

Bugbeeb
  • 2,021
  • 1
  • 9
  • 26
  • The question is of poor quality but this answer is acceptable given that you mention the use of the help function. The standard library documentation is very clear on builtin functions – Bugbeeb Jul 10 '20 at 03:18
0

You can create sublist that partition the data you need to print. Before printing the eacht part, test if you need to print a '\n' for the previous line and print partitiones without '\n':

r, c = map(int, input().split())

# create the parts that go into each line as sublist inside partitioned
partitioned = [ list(range(i+1,i+c+1)) for i in range(0,r*c,c)]
#                       ^^^^ 1 ^^^^              ^^^^ 2 ^^^^

for i,data in enumerate(partitioned):
    if i>0: # we need to print a newline after what we printed last
        print("")

    print(*data, sep = " ", end = "") # print sublist with spaces between numbers and no \n
  • ^^^^ 1 ^^^^ creates a range of all the numbers you need to print for each partition
  • ^^^^ 2 ^^^^ creates the starting numbers of each "row" thats used in ^^^^ 1 ^^^^ (reduced by 1 but thats fixed in 1's range)
  • enumerate(partitioned) returns position inside sequence and the data at that position - you only want to print '\n' after the first output was done.

After the last partitioned - output the for ... is finished and wont enter again - hence no \n after it.


Output for '6 3' (\n added for claritiy reasons):

1 2 3\n
4 5 6\n
7 8 9\n
10 11 12\n
13 14 15\n
16 17 18

with partitioned being:

[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]]
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69