so I had a ques that wanted me to reverse the entered array by the user and print it as output. now I did it in python but I got an output, similar to this: [2, 3, 4, 1] but I need the output in this format: 2 3 4 1 how should I do it?
PS: this is a ques on hackerrank. Q.Given an array, A, of N integers, print A's elements in reverse order as a single line of space-separated numbers. Input Format The first line contains an integer, N (the size of our array). The second line contains space-separated integers describing array A's elements.
Output Format
Print the elements of array A in reverse order as a single line of space-separated numbers.
Sample Input
4 1 4 3 2 Sample Output
2 3 4 1
n = int(input())
arr = list(map(int, input().rstrip().split()))
new_arr=arr[::-1]
print(new_arr)
You can use sep
parameter of print here.
print(*new_arr,sep=' ')
# 2 3 4 1
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
Print objects to the text stream file, separated by sep and followed by end. sep, end, file and flush, if present, must be given as keyword arguments.
Note: The default value of sep
is ' '
, so print(*new_arr)
will work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.