Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Last active December 27, 2020 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/b8d32e24c750c4fe9aafaef235d8ca84 to your computer and use it in GitHub Desktop.
Save parzibyte/b8d32e24c750c4fe9aafaef235d8ca84 to your computer and use it in GitHub Desktop.
def obtener_mediana(arreglo):
# Primero lo ordenamos. Puedes hacerlo manualmente o con métodos del lenguaje:
# https://parzibyte.me/blog/2020/09/25/ordenar-listas-python/
arreglo.sort()
# Obtener longitud
longitud = len(arreglo)
mitad = int(longitud / 2)
# Si la longitud es par, promediar elementos centrales
if longitud % 2 == 0:
mediana = (arreglo[mitad - 1]+arreglo[mitad]) / 2
else:
# Si no, es la del centro
mediana = arreglo[mitad]
return mediana
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment