André Caruso
André Caruso

Reputation: 13

Keeping Python from spacing after breaking a line when printing a List

(yes, I've searched all around for a solution, and, if did I see it, I wasn't able to relate to my issue. I'm new to Python, sorry!)

I've got a work to do, and it says to me: "User will input X and Y. Show a sequence from 1 to Y, with only X elements each line."

e.g 2 4 as entrance

1 2

3 4

e.g 2 6

1 2

3 4

5 6

Okay... So, I thought on doing this:

line, final = input().split()
line = int(line)
final = int(final)
List = []
i = 0
total = (final // line)
spot = 0
correction = 0
k = 1

if i != final:
List = list(range(1, final + 1, 1))
i += 1

while k != total:
spot = line * k + correction
correction += 1
k += 1
list.insert(List, spot, '\n')

print(*List)

Ok. So I managed to build my List from 1 to the "final" var. Also managed to find on which spots (therefore, var "spot") my new line would be created. (Had to use a correction var and some math to reach it, but it's 10/10)

So far, so good. The only problem is this work is supposed to be delivered on URI Online Judge, and it DEMANDS that my result shows like this:

2 10 as entrance

1 2

3 4

5 6

7 8

9 10

And, using the code I just posted, I get this as a result:

1 2

 3 4

 5 6

 7 8 

 9 10

Thus, it says my code is wrong. I've tried everything to remove those spaces (I think). Using sys won't work since it only prints one argument. Tried using join (but I could have done it wrong, as I'm new anyway)

Well, I've tried pretty much anything. Hope anyone can help me.

Thanks in advance :)

Upvotes: 1

Views: 223

Answers (4)

Hisham Karam
Hisham Karam

Reputation: 1318

I split user input into two string then convert them into int and comapre if y greater than x by 2 because this is minimum for drawing your sequence Then i make a list from 1 to y And iterate over it 2 element for each iteration printing them

x,y=input().split()
if  int(y)>int(x)+2:
    s=range(1,int(y)+1)
    for i in range(0,len(s),2):
        print(' '.join(str(d) for d in s[i:i+2]))

result:

1 2
3 4
5 6
7 8
9 10

Upvotes: 0

sytech
sytech

Reputation: 40921

The problem with the approach you're using is a result of a space being printed after each "\n" character in the series. While the idea was quite clever, unfortunately, I think this means you will have to take a different approach from inserting the newline character into the list.

Try this approach: (EDITED)

x, y = input().split()
x, y = int(x), int(y)

for i in range(1, y+1):
    if i % x == 0 or i == y:
        print(i)
    else:
        print(i, end=" ")

Output for 3 11

1 2 3
4 5 6
7 8 9
10 11

Output for 2 10

1 2
3 4
5 6
7 8
9 10

Upvotes: 0

TigerhawkT3
TigerhawkT3

Reputation: 49318

You have built a list that includes each necessary character, including the linefeed. Therefore, you have a list like this:

[1, 2, '\n', 3, 4, '\n'...]

When you unpack arguments to print(), it puts a separator between each argument, defaulting to a space. So, it prints 1, then a space, then 2, then a space, then a linefeed, then a space... And that is why you have a space at the beginning of each line.

Instead of inserting linefeeds into a list, chunk that list with iter and next:

>>> def chunks(x, y):
...     i = iter(range(1, y+1))
...     for row in range(y//x):
...             print(*(next(i) for _ in range(x)))
...     t = tuple(i)
...     if t:
...             print(*t)
...
>>> chunks(2, 6)
1 2
3 4
5 6
>>> chunks(2, 7)
1 2
3 4
5 6
7

Upvotes: 1

juanpa.arrivillaga
juanpa.arrivillaga

Reputation: 95948

Use itertools to take from an iterable in chunks:

>>> import itertools
>>> def print_stuff(x,y):
...   it = iter(range(1, y + 1))
...   chunk = list(itertools.islice(it,X))
...   while chunk:
...     print(*chunk)
...     chunk = list(itertools.islice(it,X))
... 
>>> print_stuff(2,4)
1 2
3 4
>>> 

And here:

>>> print_stuff(2,10)
1 2
3 4
5 6
7 8
9 10
>>> 

Upvotes: 0

Related Questions