{ "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...

1 answer below »
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
Answered 1 days AfterMay 24, 2021

Answer To: { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# CP5805 Practical 3 -...

Saravana answered on May 25 2021
145 Votes
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# CP5805 Practical 2 - double-click and type your name here\n",
"\n",
"Once you've completed these tasks, name your file as Lastname_Firstname_prac_2.ip
ynb and submit on LearnJCU.\n",
"E.g. if your name is Alice Smith, your file should be called Smith_Alice_prac_2.ipynb"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Task 1 - Countdown\n",
"\n",
"Write a function called `countdown` that takes a number as a parameter, then prints a countdown starting from that number down to 1, then prints \"Liftoff!\".\n",
"\n",
"For example, if you run `countdown(5)`, the output will be:\n",
"\n",
" 5\n",
" 4\n",
" 3\n",
" 2\n",
" 1\n",
" Liftoff!"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n",
"4\n",
"3\n",
"2\n",
"1\n",
"Liftoff!\n"
]
}
],
"source": [
"def countdown(n):\n",
" for i in range(n, 0, -1):\n",
" print(i) # print number from reverse\n",
" \n",
" print(\"Liftoff!\")\n",
"\n",
"countdown(5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Task 2 - Password checker\n",
"\n",
"A certain organisation has the following requirements for its users' passwords:\n",
"* at least 8 characters\n",
"* at least one number\n",
"* at least one special character (non-number, non-alphabetic)\n",
"\n",
"Write a program that asks a user to enter their choice of password and tell them whether it is valid. The program should repeat until a valid password is provided. \n",
"\n",
"Use a `main` function for controlling the overall logic of the program, and create a second function that takes a password choice as a parameter, and returns `True` or `False` based on the requirements above. Call this second function from inside `main`, not to get the password, but only to determine if it is valid. `main` will do the rest."
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the password string: trevor\n",
"Password is...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here