{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# MCS 275 Spring 2022 Homework 13 Solutions\n", "\n", "* Course Instructor: David Dumas\n", "* Solutions prepared by: Johnny Joyce" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Instructions:\n", "\n", "* Complete the problems below, which ask you to write Python scripts.\n", "* Upload your python code directly to gradescope, i.e. upload the `.py` files containing your work. (If you upload a screenshot or other file format, you won't get credit.)\n", "\n", "### Deadline\n", "\n", "This homework assignment must be submitted in Gradescope by **Noon central time on Tuesday 19 April 2022**.\n", "\n", "### Collaboration\n", "\n", "**Collaboration is prohibited**, and you may only access resources (books, online, etc.) listed below.\n", "\n", "### Resources you may consult\n", "\n", "These things might be helpful while working on the problems. Remember that for worksheets, we don't strictly limit what resources you can consult, so these are only suggestions.\n", "\n", "* [Worksheet 13 Solutions](https://www.dumas.io/teaching/2022/spring/mcs275/nbview/worksheets/worksheet13soln.html)\n", "* [Lecture 33 - HTML and CSS](http://dumas.io/teaching/2022/spring/mcs275/slides/lecture33.html)\n", "* [Lecture 34 - Planning our web app](http://dumas.io/teaching/2022/spring/mcs275/slides/lecture34.html)\n", "* [Lecture 35 - Flask](http://dumas.io/teaching/2021/spring/mcs275/slides/lecture35.html)\n", "* Any other lecture slide presentation from this course.\n", "* [w3schools HTML tutorial](https://www.w3schools.com/html/)\n", "* [w3schools CSS tutorial](https://www.w3schools.com/css/)\n", "* [Flask tutorial](https://flask.palletsprojects.com/en/1.1.x/tutorial/)\n", "* [JSFiddle](https://jsfiddle.net/) - place to quickly test HTML+CSS without leaving your browser\n", "* Course sample code:\n", " * [html examples](https://github.com/daviddumas/mcs275spring2022/tree/main/samplecode/html)\n", " * [flask examples](https://github.com/daviddumas/mcs275spring2022/tree/main/samplecode/flask-apps)\n", "\n", "\n", "### Point distribution\n", "\n", "This homework assignment has two problems. The grading breakdown is:\n", "\n", "| Points | Item |\n", "| --- | --- |\n", "| 2 | Autograder |\n", "| 6 | Problem 2 |\n", "| 2 | Problem 3 |\n", "| **14** | Total |\n", "\n", "The part marked \"autograder\" reflects points assigned to your submission based on some simple automated checks for Python syntax, etc.. The result of these checks is shown immediately after you submit.\n", "\n", "### What to do if you're stuck\n", "\n", "Ask your instructor or TA a question by email, in office hours, or on discord." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ( No problem 1 as usual )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 2 - Flask divisors\n", "\n", "Write a Flask application that takes an integer given as part of the URL and returns a page listing the divisors of the integer and whether it is prime or composite.\n", "\n", "Specifically, the app should respond to a request such as\n", "```\n", "/divisors/of/7/\n", "/divisors/of/18/\n", "/divisors/of/832/\n", "```\n", "or anything of the general form `/divisors/of//` where `N` is a positive integer greater than one, and generate an HTML document with:\n", "* A title \"The divisors of N\"\n", "* A top-level heading reading \"The divisors of N\"\n", "* An unordered list whose items are the positive integers that divide N, starting with 1 and ending with N.\n", "* A top-level heading reading \"Classification of N\"\n", "* A paragraph reading either \"The integer N is composite.\" or \"The integer N is prime.\" depending on whether there are more than 2 divisors or exactly 2 divisors, respectively. The last word in the sentence should be given strong emphasis.\n", "Here, each N must of course be replaced by whatever integer is specified as part of the URL.\n", "\n", "For example, running the app and then loading `http://localhost:5000/divisors/of/18/` in a browser (perhaps changing `5000` to the port number Flask reports it is using when it starts up) should produce a page looking something like this:\n", "\n", "![screenshot of HTML document](images/hw13-output-mockup.png)\n", "\n", "### Hints\n", "\n", "The point of the problem is to show you can generate HTML in a Flask application, so here's some advice to make the mathematical side easier.\n", "\n", "You can just iterate through all the integers `k` between 1 and `N` and check whether each one is a divisor of `N` or not. This problem does not require you to use a more efficient method.\n", "\n", "An integer `k` between 1 and `N` is a divisor of `N` exactly when `N%k == 0`.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from flask import Flask\n", "\n", "app = Flask(__name__)\n", "\n", "\n", "@app.route(\"/divisors/of//\")\n", "def divisors(N):\n", " '''Given integer `N`, generates webpage with divisors of N and states whether or not N is prime'''\n", " \n", " N = int(N)\n", " \n", " divisors = []\n", " for k in range(1,N+1): # Get a list of all divisors\n", " if N % k == 0:\n", " divisors.append(k)\n", " # Alternatively: divisors = [k for k in range(1,N+1) if N % k == 0]\n", " \n", " # Create header and opening tags for HTML code \n", " html = '''\n", " \n", " \n", " \n", " The divisors of {0}\n", " \n", " \n", "

The divisors of {0}

\n", " \n", "

Classification of {}

\n", " '''.format(N)\n", " \n", " # Paragraph for if n is prime or composite\n", " if len(divisors) == 2:\n", " html += \"

The integer {} is prime

\".format(N)\n", " else:\n", " html += \"

The integer {} is composite

\".format(N)\n", " \n", " # Close out of any remaining tags\n", " html += ''' \n", " \n", " \n", " '''\n", " \n", " return html\n", "\n", "app.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 3 - Give me an update on your project 4 work\n", "\n", "Please answer these questions in a text file `hwk13prob3.txt`. As long as you give clear answers to all the questions below, you'll receive full credit. I hope answering these might prompt you to ask me questions, and that reviewing your answers may give me a chance to offer helpful advice.\n", "\n", "### A.\n", "What is your project topic? Or if you're not decided, what are your thoughts so far?\n", "\n", "### B.\n", "What resources do you plan to use for your project work? (Books, online documentation, code samples, open source projects, etc.) If you don't know yet, say what you can about what kinds of resources you're looking for.\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.2" } }, "nbformat": 4, "nbformat_minor": 4 }