diff --git a/UFCFVQ-15-M Programming Task 1.ipynb b/UFCFVQ-15-M Programming Task 1.ipynb index 50fdad7d593c088046e0d1f1e2ab75558640f51a..010e397e2b0378ec0b6175334ff7f04cd7cd724d 100644 --- a/UFCFVQ-15-M Programming Task 1.ipynb +++ b/UFCFVQ-15-M Programming Task 1.ipynb @@ -14,15 +14,93 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Requirement FR1.1 - Find the arithmetic mean" + "#### Reading the Task1.dat dataset\n", + "\n", + "Creating a variable to read task 1.dat to call it faster between tasks." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "<class 'list'>\n" + ] + } + ], + "source": [ + "#Creating a variable called (List) and opening the 'task 1.dat' file as 'read' mode\n", + "List = open('task1.dat','r')\n", + "#Reading the data of the file.\n", + "List = List.read()\n", + "#print(type(List)) #The type is (string) at this point and item are one below each other (not a list)\n", + "\n", + "#Removing the \\n and making it as list\n", + "ConvertToList = list(List.strip().split())\n", + "print(type(ConvertToList)) #The type is (list) at this point but each item is str inside the list is e.g['148', '85', '183']\n", + "\n", + "#Converting all numbers to integer\n", + "List = list(map(int, ConvertToList))\n", + "#print(type(List)) #Now all numbers are integer list e.g [148, 85, 183]" + ] + }, + { + "cell_type": "markdown", "metadata": {}, - "outputs": [], - "source": [] + "source": [ + "### Requirement FR1.1 - Find the arithmetic mean" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The mean of the list = 120.895\n" + ] + } + ], + "source": [ + "#function to get the mean of a python list\n", + "def meanFinder(List):\n", + " #assining the varivale to be global so it can be called any were else (as it has been called in FR1.7)\n", + " global ListMean\n", + " MeanListSum = 0\n", + " MeanListLength = 0\n", + " \n", + " #Find the sum of the list using pure python\n", + " for DataInList in List:\n", + " MeanListSum += DataInList\n", + " \n", + " #Find the Length of the list using pure python \n", + " for DataLength in List:\n", + " MeanListLength += 1\n", + " \n", + " #Find the mean of the list using the mathematical mean equation\n", + " ListMean = MeanListSum/MeanListLength\n", + " \n", + " #Print The result and round it to 3 decimal places \n", + " print(f'The mean of the list = {round(ListMean,3)}')\n", + "\n", + " ###\n", + " ##Fast Alternative way with pre-built python functions\n", + " ##---\n", + " #meanEq = sum(List)/len(List)\n", + " #return meanEq\n", + " ##---\n", + " ###\n", + " \n", + "#Calling the function and passing the >> List << argument to it \n", + "meanFinder(List)" + ] }, { "cell_type": "markdown", @@ -41,10 +119,45 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Standard Devision of the list = 31.952\n" + ] + } + ], + "source": [ + "#Function to find the standard deviation\n", + "def StdDevFinder(List):\n", + " #assining the varivale to be global so it can be called any were else (as it has been called in FR1.7)\n", + " global STD_DEV\n", + " \n", + " \n", + " #Find the sum of the list using pre-bulid python function (len)\n", + " StdListSum = sum(List)\n", + " \n", + " #Find the length of the list using pre-bulid python function (len)\n", + " StdListLength = len(List)\n", + " \n", + " #Find the mean of the list\n", + " StdListMean = StdListSum/StdListLength\n", + " \n", + " #Find the variance among the list using its mathematical equation\n", + " # (Square each deviation from the mean & Calculate the variance)\n", + " StdVariance = sum((DataInList - StdListMean)**2 for DataInList in List) / StdListLength\n", + " STD_DEV = StdVariance ** 0.5\n", + " \n", + " #Print The result and round it to 3 decimal places \n", + " print(f'Standard Devision of the list = {round(STD_DEV,3)}')\n", + "\n", + "\n", + "#Calling the function and passing the >> List << argument to it \n", + "StdDevFinder(List)" + ] }, { "cell_type": "markdown", @@ -63,10 +176,64 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The minimum number in the list = 0\n", + "The max number in the list = 199\n" + ] + } + ], + "source": [ + "#Function to find the minimum value in a python list\n", + "def MinFinder(List):\n", + " #set a variable that carry the first number in the list to compare it with other numbers in the list\n", + " ListMin=List[0]\n", + " #For loop to accsses each number in the list\n", + " for numbers in range(1,len(List)):\n", + " #If the new number is smaller than the next number in the list, assign it as (ListMin) 'the new smallest number'\n", + " if List[numbers] <ListMin:\n", + " ListMin=List[numbers] \n", + " #Print out the smallest number \n", + " print(f'The minimum number in the list = {ListMin}')\n", + " \n", + " ###\n", + " ##Fast Alternative way with pre-built python functions\n", + " ##---\n", + " #ListMin = min(List)\n", + " #return ListMin\n", + " ##---\n", + " ###\n", + "\n", + "#Function to find the maximum value in a python list\n", + "def MaxFinder(List):\n", + " #set a variable that carry the first number in the list to compare it with other numbers in the list\n", + " ListMax=List[0]\n", + " #For loop to accsses each number in the list\n", + " for numbers in range(1,len(List)):\n", + " #If the new number is bigger than the next number in the list, assign it as (ListMax) 'the new largest number'\n", + " if(List[numbers]>ListMax):\n", + " ListMax=List[numbers]\n", + " #Print out the largest number \n", + " print(f'The max number in the list = {ListMax}')\n", + " \n", + " ###\n", + " ##Fast Alternative way with pre-build python functions\n", + " ##---\n", + " #ListMax = max(List)\n", + " #return ListMax\n", + " ##---\n", + " ###\n", + " \n", + " \n", + "#Calling the functions and passing the >> List << argument to it \n", + "MinFinder(List)\n", + "MaxFinder(List)" + ] }, { "cell_type": "markdown", @@ -80,15 +247,59 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Requirement FR1.4 - Find the 25th percentile " + "#### Percentile Function \n", + "Creating Percentile Function to be used for FR1.4 to FR1.7" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "#A function has been created before the question to calculate all percentile inside the List\n", + "#The function takes two arguments, List(the data list), percentile(the percentile number)\n", + "def percentileFinder(List,percentile):\n", + " #Firstly, finding the length of the list\n", + " ListNumber = len(List)\n", + " #Multiplying the length with the percentile number and dividing it by 100 (percentile equation) \n", + " Listpercentile = ListNumber * percentile / 100\n", + " #If the number inside the (List Percentile) is integer, sort it and return it to (List Percentile list)\n", + " if Listpercentile.is_integer():\n", + " Listpercentile = sorted(List)[int(Listpercentile)]\n", + " return Listpercentile\n", + " #else, remove it from (List Percentile list)\n", + " else:\n", + " Listpercentile = sorted(List)[int(round(Listpercentile)) -1]\n", + " return Listpercentile" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Requirement FR1.4 - Find the 25th percentile " + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The 25 percentile = 99\n" + ] + } + ], + "source": [ + "#Finding the 25 percentile\n", + "percentileOf_25 = percentileFinder(List,25)\n", + "#Print the result\n", + "print(f'The 25 percentile = {percentileOf_25}')" + ] }, { "cell_type": "markdown", @@ -107,10 +318,23 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The 50 percentile = 117\n" + ] + } + ], + "source": [ + "#Finding the 50 percentile\n", + "percentileOf_50 = percentileFinder(List,50)\n", + "#Print the result\n", + "print(f'The 50 percentile = {percentileOf_50}')" + ] }, { "cell_type": "markdown", @@ -129,10 +353,23 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The 75 percentile = 141\n" + ] + } + ], + "source": [ + "#Finding the 75 percentile \n", + "percentileOf_75 = percentileFinder(List,75)\n", + "#Print the result\n", + "print(f'The 75 percentile = {percentileOf_75}')" + ] }, { "cell_type": "markdown", @@ -304,7 +541,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3.10.5 64-bit", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -318,7 +555,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.5" + "version": "3.9.13" }, "vscode": { "interpreter": {