+ 3
What is the mistake in my code
Reverse an array First line of input contains N-size of array and second line contains elements of array Input format 5 1 8 4 3 9 Output 9 3 4 8 1 My code : def rev(arr,start,end): while start < end: arr[start],arr[end]=arr[end],arr[start] start+=1 end-=1 n= int(input()) arr = list(map(int,input().split())) rev(arr,0,4) print(arr) I don't want []'s in the output what corrections must be done?
8 Respostas
+ 7
DavX
there is an input taken and stored in variable *n*. this variable is never used !?
+ 6
Y Harshitha
> since there are 2 inputs required (1st: number of elements to input, 2nd: elements), i suppose the input has to be done in a loop.
nums_lst = []
count = int(input())
while count > 0:
nums_lst.insert(0, int(input()))
count -= 1
print(*nums_lst)
+ 4
Thank you
+ 4
If you need reason, then read about Tuple unpacking.. Or this thread may help you..
Your welcome...
https://www.sololearn.com/Discuss/2521834/?ref=app
+ 3
print( *arr)
+ 2
⊠Y Harshitha,
Also notice you have the start/end params of your function call as literals - you wonât be able to run:
3
1 2 3
Using your input hereâs a shorter method:
n = int(input())
arr = list(map(int,input().split()))
print(*arr[::-1])
+ 2
DavX Jayakrishna đźđł thanks learned new concepts đ
0
Lothar,
Haha indeed n isnât used, that was to demonstrate another method of reversing ( [::-1] ) - it did state âusing your inputâ, else the tests would fail.
A suggestion was also made ref using literals in their function call.
rev(arr, 0, n-1) - for example would have worked with their own attempt đđ
p.s your example doesnât work with the input format, as the second input is provided in one line seperated by whitespace.