What is the mistake in my code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

20th Feb 2023, 2:56 PM
Y Harshitha
8 Answers
+ 7
DavX there is an input taken and stored in variable *n*. this variable is never used !?
20th Feb 2023, 7:26 PM
Lothar
Lothar - avatar
+ 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)
20th Feb 2023, 7:28 PM
Lothar
Lothar - avatar
+ 4
Thank you
20th Feb 2023, 3:14 PM
Y Harshitha
+ 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
20th Feb 2023, 3:19 PM
Jayakrishna 🇮🇳
+ 3
print( *arr)
20th Feb 2023, 3:05 PM
Jayakrishna 🇮🇳
+ 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])
20th Feb 2023, 3:20 PM
DavX
DavX - avatar
+ 2
DavX Jayakrishna 🇮🇳 thanks learned new concepts 😊
20th Feb 2023, 3:26 PM
Y Harshitha
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.
20th Feb 2023, 8:00 PM
DavX
DavX - avatar