.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.
This homework assignment focuses on:
Collaboration is prohibited, and you may only access resources (books, online, etc.) listed below.
The course materials you may refer to for this homework are:
This homework assignment has 2 problems, numbered 2 and 3. The grading breakdown is:
Points | Item |
---|---|
2 | Autograder |
4 | Problem 2 |
4 | Problem 3 |
10 | Total |
Ask your instructor or TA a question by email, in office hours, or on discord.
Gradescope will show the results of the automated syntax check of all submitted files as the score for problem 1.
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:
NetShareTube
is changed to SynerGrowthTech
, and the result is written to the output fileFor 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.
"""
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()
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:
"""
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
))