Newline Print List Items

I want to print array elements without commas and single lines. I am writing a function to sort an insert. Although it works correctly, it's hard for me to print them correctly. Code I wrote:

#!/bin/python def insertionSort(ar): newNo = ar[-1] for i in range(0, m-1): if(newNo < ar[mi-1]): ar[mi] = ar[mi-1] for e in ar: print e, print '\n' ar[mi-1] = newNo for f in ar: print f, m = input() ar = [int(i) for i in raw_input().strip().split()] insertionSort(ar) 

The output I get is:

 2 4 6 8 8 2 4 6 6 8 2 4 4 6 8 2 3 4 6 8 

I need to get the following code to pass the code:

 2 4 6 8 8 2 4 6 6 8 2 4 4 6 8 2 3 4 6 8 

ie no extra space between lines. Click here for a detailed description of the problem.

+6
source share
2 answers
Operator

print automatically adds a new line, so if you use

 print '\n' 

You will get two new lines. So you can use an empty string, as in print '' , or the best way would be to use print yourself:

 print 
+5
source

It could be a fix -

 def insertionSort(ar): newNo = ar[-1] for i in range(0, m-1): if(newNo < ar[mi-1]): ar[mi] = ar[mi-1] for e in ar: print e, print ar[mi-1] = newNo for f in ar: print f, m = input() ar = [int(i) for i in raw_input().strip().split()] insertionSort(ar) 

Change - Good! ^ Someone already mentioned this.

+1
source

Source: https://habr.com/ru/post/988675/


All Articles