Created
August 17, 2021 14:32
-
-
Save shravankumar147/67a0c214abb46efb5811293302db440e to your computer and use it in GitHub Desktop.
Compute Mean of Given Numbers.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "nbformat": 4, | |
| "nbformat_minor": 0, | |
| "metadata": { | |
| "colab": { | |
| "name": "Compute Mean of Given Numbers.ipynb", | |
| "provenance": [], | |
| "authorship_tag": "ABX9TyOM8ltB62fB30U5BYlewPhT", | |
| "include_colab_link": true | |
| }, | |
| "kernelspec": { | |
| "name": "python3", | |
| "display_name": "Python 3" | |
| }, | |
| "language_info": { | |
| "name": "python" | |
| } | |
| }, | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "view-in-github", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "<a href=\"https://colab.research.google.com/gist/shravankumar147/67a0c214abb46efb5811293302db440e/compute-mean-of-given-numbers.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "iqF6Crvdc9yJ" | |
| }, | |
| "source": [ | |
| "" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "rK0mukSvfPQf" | |
| }, | |
| "source": [ | |
| "import numpy as np\n", | |
| "def mean(input_values):\n", | |
| " return sum(input_values, 0.0) / len(input_values)\n", | |
| "\n", | |
| "def geo_mean(input_values):\n", | |
| " a = np.array(input_values)\n", | |
| " return a.prod()**(1.0/len(a))\n", | |
| "\n", | |
| "def compute_mean(input_length):\n", | |
| " \"\"\"\n", | |
| " input_length: number of values to enter \n", | |
| " \"\"\"\n", | |
| " input_values = []\n", | |
| " while len(input_values)< input_length:\n", | |
| " v = int(input())\n", | |
| " if v>0:\n", | |
| " input_values.append(v)\n", | |
| " else:\n", | |
| " input_values = None\n", | |
| "\n", | |
| " arthmetic_mean = mean(input_values)\n", | |
| " geometric_mean = geo_mean(input_values)\n", | |
| " return arthmetic_mean, geometric_mean" | |
| ], | |
| "execution_count": 16, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "OZPOT8tKg-Lp", | |
| "outputId": "2a082dbd-c8cf-44b1-97c2-4259c63f9854" | |
| }, | |
| "source": [ | |
| "AM, GM = compute_mean(input_length=4)" | |
| ], | |
| "execution_count": 18, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "text": [ | |
| "10\n", | |
| "5\n", | |
| "2\n", | |
| "5\n" | |
| ], | |
| "name": "stdout" | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "IdCmwTA_hCDQ", | |
| "outputId": "5b0f6313-851a-4902-c3c2-f6c080c9d10a" | |
| }, | |
| "source": [ | |
| "AM" | |
| ], | |
| "execution_count": 19, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "5.5" | |
| ] | |
| }, | |
| "metadata": { | |
| "tags": [] | |
| }, | |
| "execution_count": 19 | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "pFRDcElPhQxy", | |
| "outputId": "03ddf4df-a5e6-4bad-d787-03f9d0a07152" | |
| }, | |
| "source": [ | |
| "GM" | |
| ], | |
| "execution_count": 20, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "4.728708045015879" | |
| ] | |
| }, | |
| "metadata": { | |
| "tags": [] | |
| }, | |
| "execution_count": 20 | |
| } | |
| ] | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment