|
72 | 72 | "source": [ |
73 | 73 | "If you copy the code from above cells into a single .py file, you have a rock-paper-scissor command line game!" |
74 | 74 | ] |
| 75 | + }, |
| 76 | + { |
| 77 | + "cell_type": "markdown", |
| 78 | + "metadata": {}, |
| 79 | + "source": [ |
| 80 | + "# 2. Data analyzer\n", |
| 81 | + "Implement `DataAnalyzer` class which has the following specification:\n", |
| 82 | + "* `__init__` takes one argument which is a path to the file to be analyzed\n", |
| 83 | + "* `total_samples` property returns the amount of the data samples in the file\n", |
| 84 | + "* `average` property returns the average of the data samples in the file\n", |
| 85 | + "* `median` property returns the median of the data samples in the file\n", |
| 86 | + "* `max_value` property returns the maximum value of the data samples in the file\n", |
| 87 | + "* `min_value` property returns the minimum value of the data samples in the file\n", |
| 88 | + "* `create_report` method returns a report (string) of the file in the following format:\n", |
| 89 | + "\n", |
| 90 | + "```\n", |
| 91 | + "Report for <filename>\n", |
| 92 | + "samples: x\n", |
| 93 | + "average: x.xx\n", |
| 94 | + "median: xx.xx\n", |
| 95 | + "max: xx.xx\n", |
| 96 | + "min: x.xx\n", |
| 97 | + "```\n", |
| 98 | + " \n", |
| 99 | + "Note that average, median, max, and min should be presented with two decimal places in the report.\n", |
| 100 | + "\n", |
| 101 | + "The format of the input file is comma separated and the file contains only numeric values.\n", |
| 102 | + "\n", |
| 103 | + "If there is no data in the input file (empty file), `NoData` exception should be raised. Note: `NoData` should be your custom exception." |
| 104 | + ] |
| 105 | + }, |
| 106 | + { |
| 107 | + "cell_type": "code", |
| 108 | + "execution_count": null, |
| 109 | + "metadata": {}, |
| 110 | + "outputs": [], |
| 111 | + "source": [ |
| 112 | + "# Your implementation here" |
| 113 | + ] |
| 114 | + }, |
| 115 | + { |
| 116 | + "cell_type": "markdown", |
| 117 | + "metadata": {}, |
| 118 | + "source": [ |
| 119 | + "Let's verify it works." |
| 120 | + ] |
| 121 | + }, |
| 122 | + { |
| 123 | + "cell_type": "code", |
| 124 | + "execution_count": null, |
| 125 | + "metadata": { |
| 126 | + "editable": false |
| 127 | + }, |
| 128 | + "outputs": [], |
| 129 | + "source": [ |
| 130 | + "import os\n", |
| 131 | + "WORKING_DIR = os.getcwd()\n", |
| 132 | + "DATA_DIR = os.path.join(os.path.dirname(WORKING_DIR), 'data')\n", |
| 133 | + "DATA_FILE = os.path.join(DATA_DIR, 'random_data.txt')\n", |
| 134 | + "\n", |
| 135 | + "da = DataAnalyzer(DATA_FILE)\n", |
| 136 | + "\n", |
| 137 | + "assert da.total_samples == 20\n", |
| 138 | + "assert da.average == 49.35\n", |
| 139 | + "assert da.median == 47.5\n", |
| 140 | + "assert da.max_value == 94\n", |
| 141 | + "assert da.min_value == 4\n", |
| 142 | + "\n", |
| 143 | + "report = da.create_report()\n", |
| 144 | + "print(report)\n", |
| 145 | + "\n", |
| 146 | + "expected_report = (\n", |
| 147 | + "'Report for random_data.txt\\n'\n", |
| 148 | + "'samples: 20\\n'\n", |
| 149 | + "'average: 49.35\\n'\n", |
| 150 | + "'median: 47.50\\n'\n", |
| 151 | + "'max: 94.00\\n'\n", |
| 152 | + "'min: 4.00')\n", |
| 153 | + "assert report == expected_report " |
| 154 | + ] |
| 155 | + }, |
| 156 | + { |
| 157 | + "cell_type": "markdown", |
| 158 | + "metadata": {}, |
| 159 | + "source": [ |
| 160 | + "Let's check that it raises `NoData` with empty file." |
| 161 | + ] |
| 162 | + }, |
| 163 | + { |
| 164 | + "cell_type": "code", |
| 165 | + "execution_count": null, |
| 166 | + "metadata": { |
| 167 | + "editable": false |
| 168 | + }, |
| 169 | + "outputs": [], |
| 170 | + "source": [ |
| 171 | + "EMPTY_FILE = os.path.join(DATA_DIR, 'empty_file.txt')\n", |
| 172 | + "try:\n", |
| 173 | + " da_empty = DataAnalyzer(EMPTY_FILE)\n", |
| 174 | + "except NoData:\n", |
| 175 | + " print('All ok :)')\n", |
| 176 | + "else: # There was not exception\n", |
| 177 | + " assert False" |
| 178 | + ] |
75 | 179 | } |
76 | 180 | ], |
77 | 181 | "metadata": { |
|
0 commit comments