203 lines
5.5 KiB
Plaintext
203 lines
5.5 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# 1. [Rock-paper-scissors](https://en.wikipedia.org/wiki/Rock%E2%80%93paper%E2%80%93scissors) \n",
|
|
"Implement `rock_paper_scissors` function which takes the player's rock-paper-scissors choice as an input (as integer), randomly selects the choice of the computer and reveals it (prints) and finally announces (prints) the result. The function should return `PLAYER_WINS`, `COMPUTER_WINS` or `TIE`."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"editable": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Constants, you should use these in your implementation\n",
|
|
"ROCK = 1\n",
|
|
"PAPER = 2\n",
|
|
"SCISSORS = 3\n",
|
|
"\n",
|
|
"PLAYER_WINS = 'Player wins!! Woop woop!'\n",
|
|
"COMPUTER_WINS = 'Robocop wins :-('\n",
|
|
"TIE = \"It's a tie!\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Your implementation here\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Once you have finished the implementation of `rock_paper_scissors` function, you can check if it works as expected by playing the game:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def play_rps():\n",
|
|
" print('Welcome to play rock-paper-scissors')\n",
|
|
" print('The options are:\\nrock: 1\\npaper: 2\\nscissors: 3')\n",
|
|
"\n",
|
|
" result = TIE\n",
|
|
" while result == TIE:\n",
|
|
" player_choice = input('Give your choice\\n')\n",
|
|
" \n",
|
|
" if not player_choice in ['1', '2', '3']:\n",
|
|
" print('Invalid choice')\n",
|
|
" continue\n",
|
|
" \n",
|
|
" result = rock_paper_scissors(int(player_choice))\n",
|
|
" \n",
|
|
"if __name__ == '__main__':\n",
|
|
" play_rps()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"If you copy the code from above cells into a single .py file, you have a rock-paper-scissor command line game!"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# 2. Data analyzer\n",
|
|
"Implement `DataAnalyzer` class which has the following specification:\n",
|
|
"* `__init__` takes one argument which is a path to the file to be analyzed\n",
|
|
"* `total_samples` method returns the amount of the data samples in the file\n",
|
|
"* `average` method returns the average of the data samples in the file\n",
|
|
"* `median` method returns the median of the data samples in the file\n",
|
|
"* `max_value` method returns the maximum value of the data samples in the file\n",
|
|
"* `min_value` method returns the minimum value of the data samples in the file\n",
|
|
"* `create_report` method returns a report (string) of the file in the following format:\n",
|
|
"\n",
|
|
"```\n",
|
|
"Report for <filename>\n",
|
|
"samples: x\n",
|
|
"average: x.xx\n",
|
|
"median: xx.xx\n",
|
|
"max: xx.xx\n",
|
|
"min: x.xx\n",
|
|
"```\n",
|
|
" \n",
|
|
"Note that average, median, max, and min should be presented with two decimal places in the report.\n",
|
|
"\n",
|
|
"The format of the input file is comma separated and the file contains only numeric values.\n",
|
|
"\n",
|
|
"If there is no data in the input file (empty file), `NoData` exception should be raised. Note: `NoData` should be your custom exception."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Your implementation here\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Let's verify it works."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"editable": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"import os\n",
|
|
"WORKING_DIR = os.getcwd()\n",
|
|
"DATA_DIR = os.path.join(os.path.dirname(WORKING_DIR), 'data')\n",
|
|
"DATA_FILE = os.path.join(DATA_DIR, 'random_data.txt')\n",
|
|
"\n",
|
|
"da = DataAnalyzer(DATA_FILE)\n",
|
|
"\n",
|
|
"assert da.total_samples() == 20\n",
|
|
"assert da.average() == 49.35\n",
|
|
"assert da.median() == 47.5\n",
|
|
"assert da.max_value() == 94\n",
|
|
"assert da.min_value() == 4\n",
|
|
"\n",
|
|
"report = da.create_report()\n",
|
|
"print(report)\n",
|
|
"\n",
|
|
"expected_report = (\n",
|
|
"'Report for random_data.txt\\n'\n",
|
|
"'samples: 20\\n'\n",
|
|
"'average: 49.35\\n'\n",
|
|
"'median: 47.50\\n'\n",
|
|
"'max: 94.00\\n'\n",
|
|
"'min: 4.00')\n",
|
|
"assert report == expected_report "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Let's check that it raises `NoData` with empty file."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"editable": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"EMPTY_FILE = os.path.join(DATA_DIR, 'empty_file.txt')\n",
|
|
"try:\n",
|
|
" da_empty = DataAnalyzer(EMPTY_FILE)\n",
|
|
"except NoData:\n",
|
|
" print('All ok :)')\n",
|
|
"else: # There was no exception\n",
|
|
" assert False"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.5.4"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|