A document from MCS 260 Fall 2021, instructor David Dumas. You can also get the notebook file.

# MCS 260 Fall 2021 Homework 6 Solutions¶

• Course Instructor: David Dumas
• Solutions Prepared By: Kylash Viswanathan and David Dumas

## 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 10am CST on Tuesday, October 5, 2021.

### Topic¶

This homework assignment focuses on:

• Command line arguments
• Opening, Reading and writing files
• String formatting

### 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

### What to do if you're stuck¶

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

## ( 1. There's no problem 1 )¶

Gradescope will show the results of the automated syntax check of all submitted files as the score for problem 1.

## 2. Company renaming¶

Suppose a company called NetShareTube has decided to rename itself to SynerGrowthTech. Unfortunately, they have a lot of text files (Python program code, documentation, etc.) that will need to be updated because they refer to the old name.

Write a Python program hwk6prob2.py to assist with the renaming as follows:

• It accepts two command line arguments: an input filename and an output filename (in that order)
• The program opens the input file for reading
• The program opens the output file for writing
• The contents of the input file are read, any instance of NetShareTube is changed to SynerGrowthTech, and the result is written to the output file

For example, if a file mission_statement.txt in the current directory contains this text:

At NetShareTube, we are committed to excellence in all its forms.  We produce
scalable enterprise solutions for delivering excellent customer experiences.

NetShareTube was founded in 2019 by a visionary leader who is no longer with
the company.

and if the following command is run

python3 hwk6prob2.py mission_statement.txt mission_statement_updated.txt

then a new file mission_statement_updated.txt should be created with the following content:

At SynerGrowthTech, we are committed to excellence in all its forms.  We produce
scalable enterprise solutions for delivering excellent customer experiences.

SynerGrowthTech was founded in 2019 by a visionary leader who is no longer with
the company.

## 2. Solution¶

In [ ]:
"""
Replaces instances of SynerGrowthTech with NetShareTube in an output file given from an input file (taken at command line)
"""
import sys

in_filename = sys.argv[1]
out_filename = sys.argv[2]

in_file = open(in_filename, "r", encoding = "UTF=8")
out_file = open(out_filename, "w", encoding = "UTF-8")

for line in in_file:
out_file.write(line.replace("NetShareTube", "SynerGrowthTech"))
out_file.close()


## 3. Dice report¶

Write a program hwk6prob3.py that uses Python's random module to simulate rolling two 6-sided dice (each with the numbers 1, 2, 3, 4, 5, 6 on its faces) 20 times in a row. The program should make note of each time the dice roll totals to 7. The program should display the results on the screen as a table in the following format:

D1 D2 S?    SPct
2  5  Y  100.00
1  3  N   50.00
2  5  Y   66.67
2  6  N   50.00
2  1  N   40.00
2  3  N   33.33
1  4  N   28.57
2  6  N   25.00
4  1  N   22.22
6  6  N   20.00
4  4  N   18.18
3  5  N   16.67
2  1  N   15.38
1  4  N   14.29
1  6  Y   20.00
1  4  N   18.75
2  5  Y   23.53
4  3  Y   27.78
6  2  N   26.32
5  6  N   25.00

Here, column D1 shows the roll of the first die, D2 shows the roll of the second die, and S? shows Y or N to indicate whether or not the dice sum to 7. The final column SPct shows the percentage of rolls so far that sum to 7. Notice that the columns are nicely aligned, with the right edge of each column label matching the right hand side of the column of numbers or values. Also, percentages are shown with 2 digits after the decimal point (so, allowing for 3 digits before the decimal point and the point itself, each percentage is shown in a way that has a total width of 6 characters).

While you can certainly use the solution to the "coin flip report" from Worksheet 6 as a starting point for this problem, make note of a key difference:

• The coin flip report program writes its report to a file (displaying nothing on the screen)
• This program is supposed to write its report to the screen (not creating or using any files)

## 3. Solution¶

In [6]:
"""
Simulates the roll of a 6-sided dice using the random module
Reports the results of 20 rolls, whether the dice sum to 7, and the percentage occurance of 7
"""
import random

dice_values = [1,2,3,4,5,6]
seven_ct = 0 # How many 7s have been rolled so far?

print("D1 D2 S?    SPct")
for i in range(20):
roll_1 = random.choice(dice_values)  # Could also use random.randint(1,6)
roll_2 = random.choice(dice_values)
if roll_1 + roll_2 == 7:
seven_ct = seven_ct + 1
seven_status = "Y"
else:
seven_status = "N"
seven_percent = 100*seven_ct / (i+1)
print(" {}  {}  {}  {:6.2f}".format(
roll_1,
roll_2,
seven_status,
seven_percent
))

D1 D2 S?    SPct
4  6  N    0.00
5  4  N    0.00
2  3  N    0.00
2  5  Y   25.00
5  6  N   20.00
2  5  Y   33.33
4  4  N   28.57
2  3  N   25.00
3  3  N   22.22
6  1  Y   30.00
1  5  N   27.27
4  4  N   25.00
6  2  N   23.08
2  2  N   21.43
3  4  Y   26.67
1  3  N   25.00
6  3  N   23.53
1  2  N   22.22
5  1  N   21.05
6  6  N   20.00


## Revision history¶

• 2021-10-08 Initial release