.py
files containing your work. (If you upload a screenshot or other file format, you won't get credit.)This homework assignment must be submitted in Gradescope by Noon central time on Tuesday 19 April 2022.
Collaboration is prohibited, and you may only access resources (books, online, etc.) listed below.
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.
This homework assignment has two problems. The grading breakdown is:
Points | Item |
---|---|
2 | Autograder |
6 | Problem 2 |
2 | Problem 3 |
14 | Total |
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.
Ask your instructor or TA a question by email, in office hours, or on discord.
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.
Specifically, the app should respond to a request such as
/divisors/of/7/
/divisors/of/18/
/divisors/of/832/
or anything of the general form /divisors/of/<N>/
where N
is a positive integer greater than one, and generate an HTML document with:
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:
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.
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.
An integer k
between 1 and N
is a divisor of N
exactly when N%k == 0
.
from flask import Flask
app = Flask(__name__)
@app.route("/divisors/of/<N>/")
def divisors(N):
'''Given integer `N`, generates webpage with divisors of N and states whether or not N is prime'''
N = int(N)
divisors = []
for k in range(1,N+1): # Get a list of all divisors
if N % k == 0:
divisors.append(k)
# Alternatively: divisors = [k for k in range(1,N+1) if N % k == 0]
# Create header and opening tags for HTML code
html = '''
<!doctype HTML>
<html>
<head>
<title>The divisors of {0}</title>
</head>
<body>
<h1>The divisors of {0}</h1>
<ul>
'''.format(N)
# Put each divisor in an `li` tag
for divisor in divisors:
html += "<li>{}</li>".format(divisor)
# End unordered list and put a header for next section
html += '''
</ul>
<h1>Classification of {}</h1>
'''.format(N)
# Paragraph for if n is prime or composite
if len(divisors) == 2:
html += "<p>The integer {} is <b>prime</b></p>".format(N)
else:
html += "<p>The integer {} is <b>composite</b></p>".format(N)
# Close out of any remaining tags
html += '''
</body>
</html>
'''
return html
app.run()
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.
What is your project topic? Or if you're not decided, what are your thoughts so far?
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.