see attached files
{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# CP5805 Practical 3 - double-click and type your name here\n", "\n", "Once you've completed these tasks, name your file as `Lastname_Firstname_prac_3.ipynb` and submit on LearnJCU.\n", "E.g., if your name is Alice Smith, your file should be called `Smith_Alice_prac_3.ipynb`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 1 - List of names\n", "Write a program that asks a user to enter a list of names. The program should keep asking for names until the user leaves their input blank (just presses Enter). Store the names in a list. Once all names have been entered, sort them in ascending order, and save them to `names.txt`, with one name per line.\n", "\n", "Sample run:\n", "\n", " Enter a name (leave blank to quit): Bob\n", " Enter a name (leave blank to quit): Carol\n", " Enter a name (leave blank to quit): Alice\n", " Enter a name (leave blank to quit):\n", " Names saved to names.txt\n", " \n", "`names.txt` would then contain:\n", "\n", " Alice\n", " Bob\n", " Carol\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 2 - CSV to JSON\n", "Download the file `products.csv` from the page where you got this notebook. Use the `csv` module to load the contents of this file, as a list of lists.\n", "\n", "Each line in the file consists of a name, quantity, and price. When loading, convert each quantity to an integer, and each price to a float.\n", "\n", "Write code to convert this list of lists into a dictionary of lists, where the keys are the names of the product, and the values are tuples containing the quantity and the price.\n", "\n", "For example, the list of lists `[\"hammer\", 31, 12.95], [\"screwdriver\", 20, 5.95]` would be converted to the dictionary `{\"hammer\": (31, 12.95), \"screwdriver\": (20, 5.95)}`.\n", "\n", "Save this dictionary as JSON to the file `products.json`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 3 - Simple calculator\n", "\n", "Write a program that serves as a simple calculator. The program will ask the user to enter an expression, then will evaluate that expression and give the result. If the user enters \"quit\", the program should end, otherwise it should keep asking for expressions.\n", "\n", "Expressions will have the form `
`, where operator is one of `+`, `-`, `*`, and `/`. Regardless of what the user enters, the program should not crash, but should instead give an error message.\n", "\n", "Sample run:\n", "\n", " Enter an expression (type quit to end): 1 + 2\n", " 3.0\n", " Enter an expression (type quit to end): 1 + t\n", " Error!\n", " Enter an expression (type quit to end): fkshfkadhfkd\n", " Error!\n", " Enter an expression (type quit to end): 700 / 17\n", " 41.1764705882353\n", " Enter an expression (type quit to end): quit\n", " Goodbye!\n", " \n", "Hint: use `split()` to extract the numbers and operator from the user's input. Use exception handling to make sure the program doesn't crash if a user enters an unexpected value.\n", "\n", "(If you know about `eval` do **not** use it - it is not safe.)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 4 - Word occurrences\n", "\n", "Write a program to count the occurrences of words in a file.\n", "The program should ask the user for a filename, then print the counts of how\n", "many of each word are in the file.\n", "\n", "If the content of a file is like:\n", "\n", " this is a collection of words of nice words this is a fun thing it is\n", "\n", "The output should look like this:\n", "\n", " a : 2\n", " collection : 1\n", " fun : 1\n", " is : 3\n", " it : 1\n", " nice : 1\n", " of : 2\n", " thing : 1\n", " this : 2\n", " words : 2\n", "\n", "### Hints:\n", "\n", "- use a dictionary where the keys are the words, and the values are the counts\n", "- there are two ways to handle incrementing counts for each word:\n", " - first check if it's `in` the dictionary; then either set the count to 1 or increment the count by 1\n", " - `try` and increment the count by 1; if it fails (`KeyError`), set the count to 1.\n", "- Notice that the sample output is sorted.\n", " *Only after* you have the program working, **make your program do this\n", " sorting**.\n", "- As a challenge, **align the outputs so that the numbers are in one nice column**. You will need to find the longest word in the list first, which you can do with a generator expression.\n", " Your output should then look something like:\n", "\n", " a : 2\n", " collection : 1\n", " fun : 1\n", " ..." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "# Starter code\n", "word_to_count = {}\n", "\n", "# Use exception handling to check if the file can be opened\n", "# Use the following text if the file cannot be opened\n", "text = \"this is a collection of words of nice words this is a fun thing it is\"\n", "\n", "# Split the text to get just the words into a list\n", "\n", "# for each word in words: count it by incrementing the value of word_to_count[word]\n", "\n", "# sort the words with something like\n", "# words = sorted(word_to_count.keys())\n", "\n", "# find the max_length with a generator expression\n", "\n", "# print the words in formatted columns\n", "# for word in words:\n", "# print(f\"{word:{max_length}} : {word_to_count[word]}\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3