I was recently doing a question on geeks for geeks website where they required median of two sorted arrays, but they are asking answer to be a int if there are no digits after decimals or a float if there are digits after decimals. For example when giving median of even number arrays, we usually divide middle elements with two, so if middle elements are 2
and 4
then after division answer should be 3
, or if elements are 3
and 4
answer should be 3.5
. But if we use /
operator in python it will return division of 2
and 4
as 3.0
, and if we use //
operator it will return division of 3
and 4
as 3
instead of 3.5
. Is there any way to get around this instead of checking divisibility by 2.
You could try something like this potentially. Do the division as normal and then apply the following:
if int(x) == x: # will be true if x=5.0 but not if x = 5.5
x = int(x)