{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# MCS 260 Fall 2021 Homework 9\n",
"\n",
"* Course instructor: Emily Dumas"
]
},
{
"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",
"This homework assignment must be submitted in Gradescope by 10am CST on Tuesday, October 19, 2021.\n",
"\n",
"### Topic\n",
"This homework focuses **dispatch tables**, **operators on iterables** (e.g. any(), all()), and **modules**.\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",
"The course materials you may refer to for this homework are:\n",
"\n",
"* [Worksheet 9 Solutions](https://www.dumas.io/teaching/2021/fall/mcs260/nbview/worksheets/worksheet9soln.html)\n",
"* [Sample programs](https://github.com/emilydumas/mcs260fall2021/tree/main/samplecode) (especially the ones in the `modules/` subdirectory; the project 1 solution may also be helpful)\n",
"* [Lecture 21 - Dispatch tables](https://www.dumas.io/teaching/2021/fall/mcs260/slides/lecture21.html)\n",
"* [Lecture 22 - Operators on iterables](https://www.dumas.io/teaching/2021/fall/mcs260/slides/lecture22.html)\n",
"* [Lecture 23 - Modules](https://www.dumas.io/teaching/2021/fall/mcs260/slides/lecture23.html)\n",
"* Slides from any other lecture of MCS 260, Fall 2021.\n",
" * Since `any()` and `all()` are often used with list comprehensions, you may want to consult: [Lecture 8 - list methods and list comprehensions](https://www.dumas.io/teaching/2021/fall/mcs260/slides/lecture8.html#/)\n",
"* [Downey's book](https://greenteapress.com/thinkpython2/html/)\n",
"\n",
"### Point distribution\n",
"\n",
"This homework assignment has 2 problems, numbered 2 and 3. **Problem 2 is a bit longer than usual, so it gets 8 points this time.** Thus the grading breakdown is:\n",
"\n",
"| Points | Item |\n",
"| --- | --- |\n",
"| 2 | Autograder |\n",
"| 8 | Problem 2 |\n",
"| 4 | Problem 3 |\n",
"| **14** | Total |\n",
"\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": [
"## ( 1. There's no problem 1 )\n",
"\n",
"Gradescope will show the results of the automated syntax check of all submitted files as the score for problem 1."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Module for testing integers for digit properties\n",
"\n",
"Write a module `digitprop` (contained in a file `digitprop.py`) that contains two functions. The function definitions and docstrings you should use are shown below. Write the functions to do what the docstrings describe. Include a module docstring as well.\n",
"\n",
"**Restricted methods note: For full credit you must use `any()` or `all()` in an appropriate way in each function.** You may need to use `enumerate()`, too.\n",
"\n",
"The module shouldn't do anything other than define functions when it is imported.\n",
"\n",
"Checklist for grading:\n",
"* Is the module in a file called `digitprop.py`?\n",
"* Does the module have a file-level docstring that describes what it contains?\n",
"* Does it use the same function definitions and function docstrings as provided in Homework 9?\n",
"* Do the functions do what was requested?\n",
"* Does the module contain *only* function definitions, without doing anything else when it is imported?\n",
"* Does each function use either `all()` or `any()` in an essential way?\n",
"\n",
"**Advice:** The function bodies, excluding the docstrings, can be just one or two lines each."
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"# Function definitions and docstrings to copy into your own `digitprop.py`\n",
"# Then, add function bodies!\n",
"\n",
"def has_small_digits(n,maxdigit):\n",
" \"\"\"\n",
" Determines whether or not the digits of `n` are all between 0 and `maxdigit` (inclusive).\n",
" Returns `True` or `False` accordingly.\n",
" e.g.\n",
" has_small_digits(1021,1) returns False - third digit is larger than 1\n",
" has_small_digits(1021,2) returns True - all digits between 0 and 2\n",
" has_small_digits(1021,5) returns True - all digits between 0 and 5\n",
" has_small_digits(351622,5) returns False - fourth digits if larger than 5\n",
" has_small_digits(351622,6) returns True - all digits between 0 and 6\n",
" \"\"\"\n",
" \n",
"def is_antipalindrome(n):\n",
" \"\"\"\n",
" Takes a positive integer `n`.\n",
" Returns `True` if reversing the order of the digits in `n` gives the same\n",
" result as replacing each digit d of `n` with 9-d (\"flipping\" the digits).\n",
" Otherwise, returns `False`.\n",
" (A number `n` for which this function returns `True` might be called an *antipalindrome*.)\n",
" e.g.\n",
" is_antipalindrome(5128) returns False, because reversing order gives 8215 while flipping digits gives 4871.\n",
" is_antipalindrome(4815) returns True, because reversing order or flipping digits each gives 5184.\n",
" \"\"\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Special powers\n",
"\n",
"For the purposes of this problem, let's say that an integer is *special* if it is either an antipalindrome (in the sense of problem 2) **or** if it only uses the digits 0, 1, 2.\n",
"\n",
"For example, 1012, 212000, and 4815 are all special, while 3012 and 5128 are not special.\n",
"\n",
"Write a program `hwk9prob3.py` that imports the `digitprop` module you wrote in problem 2 and uses it to answer the following:\n",
"\n",
">**Question.**\n",
">\n",
">Is `d**k` ever special when\n",
">* `d` is a non-special number between 100 and 350 **and**\n",
">* `k` is between 2 and 350\n",
">?\n",
"\n",
"In this problem, \"between\" has the inclusive meaning, so e.g. `k` is allowed to be `2` or `350`.\n",
"\n",
"If the program finds any of these, it should print `d`, `k`, and `d**k`. After running the program, paste the results you found as comments at the bottom of `hwk9prob3.py`.\n",
"\n",
"Make sure the program has a file-level docstring.\n",
"\n",
"Checklist for grading grading:\n",
"* Does the program have a docstring that describes what it does?\n",
"* Does the program use the module `digitprop` rather than replicating any of its features?\n",
"* Does the program do what was requested?\n",
"* Are the results produced by the program (`d`,`k`,`d**k`) included in comments at the bottom of the program?\n",
"\n",
"**Note:** A correct program will take a some time to run, since it will need to examine about 81,000 integer powers."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Revision history\n",
"\n",
"* 2021-10-21 Initial release"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
}