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

# MCS 275 Spring 2022 Homework 1 Solutions¶

• Course Instructor: David 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.)

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

(Note:That's a deviation from the usual schedule where homework is due on Tuesdays.)

### Collaboration¶

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

### Resources you may consult¶

The course materials you may refer to for this homework are:

### Point distribution¶

This homework assignment has 2 problems, numbered 2 and 3. The grading breakdown is:

Points Item
4 Problem 2
4 Problem 3
10 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.

## Problem 1 doesn't exist¶

In Gradescope, the score assigned to your homework submission by the autograder (checking for syntax and docstrings) will be recorded as "Problem 1". Therefore, the numbering of the actual problems begins with 2.

## Problem 2: Filled fancy box¶

In worksheet 1, you made a program to draw a box using unicode box-drawing characters. An 9x6 box of that type looks like this:

╔═══════╗
║       ║
║       ║
║       ║
║       ║
╚═══════╝

Modify that program (calling the new one hwk1prob2.py) so that it generates a "fancy box" with decorative inset corners and plus signs, like this:

+╔═════╗+
╔╝     ╚╗
║       ║
║       ║
╚╗     ╔╝
+╚═════╝+

and so that each empty space inside the box gets filled with a copy of a specified character, e.g.

+╔══════════════╗+
╔╝FFFFFFFFFFFFFF╚╗
║FFFFFFFFFFFFFFFF║
╚╗FFFFFFFFFFFFFF╔╝
+╚══════════════╝+

Specifically:

• The output filename will be given as the first command line argument
• The total width of the box, in characters, is the second command line argument (will be at least 5)
• The total height of the box, in lines, is the third command line argument (will be at least 5)
• The fill character ("F" in the example above) is the fourth command line argument

Examples:

• If called as

python3 hwk1prob2.py out1.txt 9 6 j

the output file out1.txt is created and the following is written to it:

+╔═════╗+
╔╝jjjjj╚╗
║jjjjjjj║
║jjjjjjj║
╚╗jjjjj╔╝
+╚═════╝+
• If called as

python3 hwk1prob2.py out2.txt 15 5 @

the output file out2.txt is created and the following is written to it:

+╔═══════════╗+
╔╝@@@@@@@@@@@╚╗
║@@@@@@@@@@@@@║
╚╗@@@@@@@@@@@╔╝
+╚═══════════╝+

# Solution:¶

In [14]:
import sys

outfile = sys.argv[1] # e.g. "out.txt"
width = int(sys.argv[2]) # e.g. 10
height = int(sys.argv[3]) # e.g. 20
fill_char = sys.argv[4] # e.g. "F"

def write_box_to_file(f):
'''Given an already-opened file f, writes the fancy box to the file'''

# Create the top of the box
# We subtract 4 from the width to account for the corners
f.write("+╔" + "═"*(width-4) + "╗+")                  # Looks like e.g.:  +╔═════════╗+
f.write("\n") # Move to a new line
f.write("╔╝" + fill_char*(width-4) + "╚╗")            # Looks like e.g.:  ╔╝FFFFFFFFF╚╗
f.write("\n") # Move to a new line

# Create the left and right sides of the box
# Subtract 4 from height to account for top two and bottom two lines
for i in range(height-4):
f.write("║" + fill_char*(width-2) + "║")          # Looks like e.g.:  ║FFFFFFFFFFF║
f.write("\n") # Move to a new line

# Create the bottom of the box
f.write("╚╗" + fill_char*(width-4) + "╔╝")             # Looks like e.g.: ╚╗FFFFFFFFF╔╝
f.write("\n") # Move to a new line
f.write("+╚" + "═"*(width-4) + "╝+")                   # Looks like e.g.:  ╚═════════╝
f.write("\n") # Move to a new line

# By setting encoding to UTF-8, we can use the Unicode characters as specified
with open(outfile, "w", encoding="UTF-8") as f:
write_box_to_file(f)


## Problem 3: Finding 260 in powers of 275¶

The large integer shown below is $275^{50}$ (or in Python syntax, 275**50):

92605054467343067837474447744797179339681065959901609623272419896243561247711783591951562044641832471825182437896728515625

If you look near the beginning, you'll see that the three digits 260 appear inside this number (contiguously). Those digits are shown in bold.

There are other powers of 275 that contain 260 within the decimal digits, and the one above isn't even the smallest power that works.

Write a program to find the four smallest powers of 275 that contain 260 in their decimal digits. Have it print just the exponents (e.g. 50 in the example above), rather than the actual huge integer.

Save your program as hwk1prob3.py and submit it.

Note: We're not looking for 2, 6, and 0 to appear in arbitrary places. They must be next to one another and in the order 260. So, for example, $275^3 = 20796875$ wouldn't count.

# Solution:¶

Note:

The solutions are:

• 275 ^ 26 = 2646366800746415709389164929721**260**932637960650026798248291015625

• 275 ^ 29 = 550361595592731142062027893977968484584550878935**260**69819927215576171875

• 275 ^ 30 = 151349438788001064067057670843941333**260**75149170719669200479984283447265625

• 275 ^ 37 = 18001540493**260**55593052190123655761154309235783327163005207438573052058927714824676513671875

In [2]:
power = 0 # The power to which we will raise the number 275

times_printed = 0 # Keep track of how many times we have printed a number

while times_printed < 4:

power += 1

number_as_string = str(275**power) # Convert to str so we can check individual digits

if "260" in number_as_string:
print(power)
times_printed += 1


26
29
30
37

In [ ]: