+ 1

how to write code to detect middle number in python array

ex- a= {2,3,5} mid=3 b={1,1,5,6,7} mid=5

18th Feb 2023, 2:17 AM
Damsara Welivita
Damsara Welivita - avatar
3 Answers
+ 6
Is that an array or a `set`? You know that the latter cannot have duplicate members. So if it was a `set`, <b> will have {1, 5, 6, 7} only Should it return a single value even when the container has even number of elements? I was thinking, if <b> has {1, 5, 6, 7} there would be 2 middle numbers, 5 and 6 ... What if the container only has 2 members in? e.g. [ 20, 23 ] or a single member like [ 2023 ] ?
18th Feb 2023, 3:00 AM
Ipang
+ 5
Roshaan, good decision. But I think that middleNum = (len(array) - 1) // 2 = len(array) // 2
18th Feb 2023, 9:22 AM
Mikhail
Mikhail - avatar
+ 1
Damsara Welivita that is not an array I agree with Ipang
18th Feb 2023, 10:12 PM
Alex Wairegi
Alex Wairegi - avatar