A document from MCS 275 Spring 2022, instructor Emily Dumas. You can also get the notebook file.

MCS 275 Spring 2022 Homework 13 Solutions

  • Course Instructor: Emily Dumas
  • Solutions prepared by: Johnny Joyce

Instructions:

  • Complete the problems below, which ask you to write Python scripts.
  • 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.)

Deadline

This homework assignment must be submitted in Gradescope by Noon central time on Tuesday 19 April 2022.

Collaboration

Collaboration is prohibited, and you may only access resources (books, online, etc.) listed below.

Resources you may consult

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.

Point distribution

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.

What to do if you're stuck

Ask your instructor or TA a question by email, in office hours, or on discord.

( No problem 1 as usual )

Problem 2 - Flask divisors

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:

  • A title "The divisors of N"
  • A top-level heading reading "The divisors of N"
  • An unordered list whose items are the positive integers that divide N, starting with 1 and ending with N.
  • A top-level heading reading "Classification of 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. Here, each N must of course be replaced by whatever integer is specified as part of the URL.

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:

screenshot of HTML document

Hints

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.

In [ ]:
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()

Problem 3 - Give me an update on your project 4 work

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.

A.

What is your project topic? Or if you're not decided, what are your thoughts so far?

B.

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.