首页 > > 详细

代做data编程、代写python编程语言

项目预算:   开发周期:  发布时间:   要求地区:
Homework #3
Due: Friday, October 18th at 4:00pm CST
Table of Contents
Homework #3
Getting started
Manual testing
Docstrings
Exercise 1
Representing colors using tuples Exercise 2
Exercise 3
Short-circuiting
A simple lab instrument Exercise 4
Exercise 5
Exercise 6
Exercise 7
Exercise 8
Exercise 9
Grading
Completeness SNU Score
Code Quality
Submission
The purpose of this assignment is to give you experience with conditionals, lists, and loops.
As in HW #2, many of the problems specify requirements related to the constructs that you are allowed to use in your solutions. The purpose of these requirements is to ensure that you get practice with particular programming constructs.
The grader who reviews your assignment will verify that your code follows these restrictions as part of determining your code quality score.

Please note that you will not need to define any new functions to complete these exercises. For all of the tasks, you may assume the parameters have the right types.
Getting started¶
To get started, you will need to pick up the materials for this assignment. The first step is to navigate to your coursework directory:
$ cd ~/cmsc14100-aut-2024/cs141-coursework-GITHUB_USERNAME
Please recall that the $ represents the prompt and is not included in the command. You also need to replace GITHUB_USERNAME with your GitHub user name.
Use git status to make sure that your local copy of your repository is in a clean state. The output should look like this:
If the output does not match, please review the relevant parts of the Introduction to Git (https://uchicago- cs.github.io/student-resource-guide/tutorials/git-intro.html) tutorial. If you run into trouble, please ask for help.
Once you have cleaned up and your repository is in a good state, run the following command:
$ git pull upstream main
This command will likely drop you into an unfamiliar editor window with text similar to the following:
If that happens, type :q! (that is, a colon, followed by the letter q , followed by the symbol ! ) and then hit enter.
This command will pull the materials for this assignment into your local copy of your repository and will create a commit.
You will find the files you need in the hw3 directory. The file README.md contains a description of the files in the directory. Please take a minute to review it before you start work on the assignment.
Manual testing¶
$ git status .
On branch main
Your branch is up to date with 'origin/main'.
Merge branch 'main' of github.com:uchicago-cmsc14100-aut-2024/coursework-upstream into main
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
You should plan to test your code manually in ipython3 .

Run the following at the start of your IPython session to load your code and to set up autoreload so that subsequent changes to the file will be automatically picked up:
If you get an error saying that the import failed, make sure you are running ipython3 from within your hw3 directory.
Make sure you set-up autoreload every time you start-up ipython3 . Docstrings¶
After the first exercise, you will be required to write docstrings for the required functions. A docstring should have:
a brief description of the purpose of the function,
an “Args” section that provides the name, type, and purpose of each input, and
a “Returns” section that provides the type of the return value and a brief description of the return value.
Here is how to write the types that you will use in this assignment, for: an integer, use (int) as the type,
a boolean, use (bool) as the type,
a color tuple use (Tuple[int, int, int]) ,
a list of integers, use (List[int]) , and
a list of color tuples, use (List[Tuple[int, int, int]]) .
We recommend writing the required docstring before you write code for an exercise to solidify your understanding of the purpose of the function, the arguments to the function (including their types), and the return value for the function (including its type).
The grader who reviews your assignment will check your docstrings to ensure that you have included all the required information as part of evaluating the quality of your code. Also, please keep in mind that the 80- character limit on the length of lines applies to your docstrings as well as to your code.
Exercise 1¶
In Homework #2, you wrote a simple function, is_grayscale , to determine whether an RGB color represents a grayscale color (that is, do the red, green, and blue channels of the color have the same value.)
$ ipython3
In [1]: %load_ext autoreload
In [2]: %autoreload 2
In [3]: import hw3

For this exercise, your task is to write again, this time using a different set of constructs than you used in Homework #2. As a reminder, takes three integer values, r , g , and b , for the red, green, and blue channels of a color and returns True , if all three channels have the same value and False , otherwise.
You may assume that all three parameters hold integer values between 0 and 255 inclusive. Here are some sample uses of this function:
is_grayscale
is_grayscale
In [2]: hw3.is_grayscale(255, 255, 255) # White
Out[2]: True
In [3]: hw3.is_grayscale(160, 32, 240)
Out[3]: False
In [4]: hw3.is_grayscale(0, 0, 255)
Out[4]: False
In [5]: hw3.is_grayscale(0, 0, 0)
Out[5]: True
# Purple
# Blue
# Black
Requirements:
You may use conditional statements and the integer equality ( == )/inequality ( != ) operators in your solution
to this task.
You may not use logical operators ( and , or , and not ), nor may you use arithmetic operations ( + , - , etc) in your solution to this task.
Automated Tests
To run the automated tests for this exercise, run the following command at the Linux command-line:
$ py.test -xvk is_gray
We encourage you to open a second terminal window (connected to your assigned Linux server using SSH) for running py.test . Also, remember to run the tests in your hw3 directory.
Representing colors using tuples¶
In Exercise 1, we represented a color using three integer parameters. We can also represent a color using a tuple with three integers that have values between 0 and 255 inclusive. Here are some sample colors:
For the tasks that use color tuples, you may assume that the tuples are valid. That is, you can assume a tuple for a color has three integers with values that range between 0 and 255 inclusive.
We can give names to the individual values in a tuple using Python’s tuple unpacking mechanism, which allows programmers to specify multiple names, separated by commas, on the left side of an assignment statement and
In [6]: WHITE = (255, 255, 255)
In [7]: BLACK = (0, 0, 0)
In [8]: BLUE = (0, 0, 255)
In [9]: MEDIUM_GRAY = (127, 127, 127)

In [10]: r, g, b = (160, 32, 240) # Purple
In [11]: r
Out[11]: 160
In [12]: g
Out[12]: 32
In [13]: b
Out[13]: 240
In [14]: r, g, b = BLUE
In [15]: r
Out[15]: 0
In [16]: g
Out[16]: 0
In [17]: b
Out[17]: 255
is_black_or_white
0, 0)
(255, 255, 255)
False
is_black_or_white
In [20]: hw3.is_black_or_white(BLACK)
Out[20]: True
In [21]: hw3.is_black_or_white(WHITE)
Out[21]: True
In [22]: hw3.is_black_or_white(MEDIUM_GRAY)
Out[22]: False
In [23]: hw3.is_black_or_white(BLUE)
Out[23]: False
In [24]: hw3.is_black_or_white((160, 32, 240))
Out[24]: False
# Purple
an expression that yields a tuple on the right side. Here are some example uses of this mechanism:
Please note when you use tuple unpacking, the number of names on the left side of the assignment statement must equal the number of values in the tuple that is the result of evaluating the right side of the assignment statement. For example, if evaluating the expression on the right side of the assignment statement yields a tuple of length three, then you need to have either one name on the left side of the assignment statement (for the whole tuple) or three names (one per value in the tuple).
We encourage you use this mechanism when you need to work with the components/channels of a color.
Exercise 2¶ Complete the function,
) or white (
Here are some sample uses of
) and
, which takes a color tuple and returns True if the color is black ( (0, , otherwise.
that use the color constants defined above:
Requirements:
You may use conditional statements, tuple unpacking, and integer equality/inequality operators for this task. You may not use logical operators or equality/inequality on tuples in your solution for this task.
Automated Tests

To run the automated tests for this exercise, run the following command at the Linux command-line: $ py.test -xvk is_black
Exercise 3¶
Complete the function count_not_black_or_white , which takes a list of color tuples, and returns a count of the
number of colors in the list that are neither black nor white.
Here are some sample uses of this function that use the color constants defined above:
In [25]: hw3.count_not_black_or_white([])
Out[25]: 0
In [26]: hw3.count_not_black_or_white([WHITE])
Out[26]: 0
In [27]: hw3.count_not_black_or_white([BLUE])
Out[27]: 1
In [31]: hw3.count_not_black_or_white([(160, 32, 240), (100, 52, 200), (0, 0, 0), (4, 30, 100)])
Out[31]: 3
Restrictions
You may not use logical operators or equality/inequality on tuples in your solution to this task. Do not repeat the code for black_or_white in your solution. Use a function call instead.
See the Code Quality section for hints about how to chose the right looping construct.
Automated Tests
To run the automated tests for this exercise, run the following command at the Linux command-line:
$ py.test -xvk count
Short-circuiting¶
As we discussed in class, both the logical and and the logical or operators short-circuit. That is, Python stops evaluating the expression as soon as the answer is known. For logical and , Python does not bother to evaluate the second operand if the first operand evaluates to False . For logical or , Python does not bother to evaluate the second operand if the first operand evaluates to True .
This concept extends to loops as well: a loop can short-circuit using break or a return to stop the computation early.
A simple lab instrument¶
The next three problems concern the same simple lab instrument. During a given run, the instrument generates a sequence of integer values. When the machine is working properly the values it generates will fall between specified lower and upper bounds inclusive. The machine signals failures by generating values that fall outside

the specified bounds; some failures yield values strictly less that the lower bound and some yield values that are strictly greater than the upper bound.
If lower is 0 and upper is 10, then, for example:
0 is normal,
2 is normal,
10 is normal,
-5 signals a lower failure, 20 signals an upper failure.
In these tasks, you will be writing functions to determine whether failures occur and if so, whether a certain failure (the first or the last) is less than the lower bound or greater than the upper bound.
We have defined the following constants to use as return values:
LOWER signals that the failure of interest is less than the lower bound,
UPPER signals that the failure of interest is greater than the upper bound, and NEITHER signals that no failure occurred.
Exercise 4¶
Exercises 4 and 5 require you to implement the same computation using different constructs. Implementing the same computation twice will give you practice with different mechanisms and will help you see the relationship between the two approaches.
Write a function which_comes_first_break that takes a list of integers generated by our instrument in the order they were generated, the lower bound, and the upper bound and returns:
LOWER , if at least one failure occurred and the first failure has a value strictly less than the lower bound, UPPER , if at least one failure occurred and the first failure has a value strictly greater than the upper bound, NEITHER , if no failures occurred.
As noted above, we have defined constants for LOWER , UPPER , and NEITHER for you to use as the return values for your function.
Here are some sample uses of this function:
In [34]: hw3.which_comes_first_break([], -10, 10) # NEITHER
Out[34]: 0
In [35]: hw3.which_comes_first_break([-21], -20, 10) # LOWER
Out[35]: -1
In [36]: hw3.which_comes_first_break([11], -20, 10) # UPPER
Out[36]: 1
In [39]: hw3.which_comes_first_break([10, 20, 15, 0, -1, 1, 12, 13, 21], 0, 20) # LOWER
Out[39]: -1

The first example returns 0 , which is the value of NEITHER , because there are no values in list and thus, no failures in the list.
In the second example, the list contains one value and that value is less than the lower bound, and so the result is -1 , the value of LOWER .
In the third example, the list contains a one value and that value that is greater than the upper bound, and so the result is 1 , the value of UPPER .
In the fourth example, the result is LOWER because the list contains failures ( -1 and 21 ). Since the first failure value ( -1 ) is less than the value specified for the lower bound, the result is LOWER .
In the fifth example, the result is NEITHER because there are no failures: all the values fall between the lower and upper bounds inclusive.
Finally, in the sixth example, the result is UPPER because the list contains failures ( 20 , 0 , and -1 ) and the first failure ( 20 ) is greater than the value specified for the upper bound.
Requirements:
Your function must stop looking values in the list as soon you find a failure value, that is, your function must
short-circuit.
For this exercise, you are required to use break to affect the short-circuiting.
See the Code Quality section for hints about how to chose the right looping construct.
Automated tests
To run the automated tests for this exercise, run the following command at the Linux command-line:
$ py.test -xvk first_break
Exercise 5¶
Your task is to complete the function which_comes_first_return , which implements the same computation as which_comes_first_break using a different method for accomplishing the short circuiting.
Requirements:
Your function must short-circuit.
For this exercise, you are required to use return to affect the short-circuiting.
See the Code Quality section for hints about how to chose the right looping construct.
Automated tests
In [40]: hw3.which_comes_first_break([10, 20, 15, 0, -1, 1, 12, 13, 21], -1, 21) # NEITHER
Out[40]: 0
In [41]: hw3.which_comes_first_break([10, 20, 15, 0, -1, 1, 12, 13], 1, 19) # UPPER
Out[41]: 1

To run the automated tests for this exercise, run the following command at the Linux command-line: $ py.test -xvk first_return
Exercise 6¶
Write a function which_comes_last , which takes a list of integers generated by our instrument, the lower bound, and the upper bound and returns:
LOWER , if at least one failure occurred and the last failure was a value strictly less than the lower bound, UPPER , if at least one failure occurred and the last failure was a value strictly greater than the upper bound, NEITHER , if no failures occurred.
Here are some sample uses of this function:
In [43]: hw3.which_comes_last([], 0, 20) # NEITHER
Out[43]: 0
In [44]: hw3.which_comes_last([-1], 0, 20) # LOWER
Out[44]: -1
In [45]: hw3.which_comes_last([21], 0, 20) # UPPER
Out[45]: 1
In [54]: hw3.which_comes_last([10, 20, 15, 0, -1, 1, 12, 13, 21], 0, 20) # UPPER
Out[54]: 1
In [55]: hw3.which_comes_last([10, 20, 15, 0, -1, 1, 12, 13, 21], 0, 25) # LOWER
Out[55]: -1
In [56]: hw3.which_comes_last([10, 20, 15, 0, -1, 1, 12, 13, 21], -10, 25) # NEITHER
Out[56]: 0
Requirements:
See the Code Quality section for hints about how to chose the right looping construct. Automated tests
To run the automated tests for this exercise, run the following command at the Linux command-line:
$ py.test -xvk last
Exercise 7¶
Complete the function are_all_same , which takes a list of integers and returns True if all the values in the list are the same and False otherwise.
Here are some sample use of this function:
In [57]: hw3.are_all_same([])
Out[57]: True
In [58]: hw3.are_all_same([1])
Out[58]: True

Requirements:
Your function must short-circuit. Note that converting the list to a set would violate this restriction.
Automated tests
To run the automated tests for this exercise, run the following command at the Linux command-line:
$ py.test -xvk same
Exercise 8¶
Complete the function compute_final_score , which takes a list of intermediate scores for a game, a lower bound, an upper bound, and a bonus value and returns a final score. The intermediate scores, the bounds, and the bonus value are all integers. The final score is the sum of the intermediate scores plus, possibly, a bonus value. The bonus value is added to the final score if there are more intermediate scores with values strictly greater than the upper bound than intermediate scores with values that are strictly less than the lower bound.
For example, given the list:
[5, -5, 8, 9, 11, 5, 12]
and a lower bound of 0 , an upper bound of 10 , and a bonus value of 10 , the result would be 55 . The sum of the seven intermediate scores is: 45 . We add in the bonus value of 10 , because there two intermediate scores– 11 and 12 –greater than 10 and only one intermediate score– -5 – lower than 0 .
Here are some sample uses of this function:
In [61]: hw3.compute_final_score([], 0, 10, 10)
Out[61]: 0
In [62]: hw3.compute_final_score([-21], -20, 20, 10)
Out[62]: -21
In [63]: hw3.compute_final_score([21], -20, 20, 10)
Out[63]: 31
In [64]: hw3.compute_final_score([-21, 21], -20, 20, 10)
Out[64]: 0
In [65]: hw3.compute_final_score([-21, 21, 22], -20, 20, 10)
Out[65]: 32
In [66]: hw3.compute_final_score([-21, 21, -22], -20, 20, 10)
Out[66]: -22
In [67]: hw3.compute_final_score([8, 10], 0, 10, 10)
Out[67]: 18
Requirements:
See the Code Quality section for hints about how to chose the right looping construct.
In [59]: hw3.are_all_same([1, 1, 1, 1])
Out[59]: True
In [60]: hw3.are_all_same([1, 1, 1, 1, 2])
Out[60]: False

Automated tests
To run the automated tests for this exercise, run the following command at the Linux command-line:
$ py.test -xvk final
Exercise 9¶
Complete the function get_first_bw_idx , which takes a list of color tuples, and returns the index of the first element that is either black or white. The function should return None if neither black nor white occur in the list. The return type for this function can be written as (int | None) .
Here are some sample uses of this function:
In [69]: hw3.get_first_bw_idx([]) is None
Out[69]: True
In [70]: hw3.get_first_bw_idx([WHITE])
Out[70]: 0
In [72]: hw3.get_first_bw_idx([BLUE, (0, 255, 0), BLUE, BLUE, BLACK, (255, 0, 0), MEDIUM_GRAY, WHITE, BLAC
K])
Out[72]: 4
Requirements:
Your function must short-circuit once it finds the first occurrence of black or white.
Your solution may not repeat the code for determining whether a color tuple is black or white.
Your solution may not use the list in operator or the list index method. (Please note that you may use the required in keyword in a for loop.)
Your solution may not use the range or len functions.
See the Code Quality section for hints about how to chose the right looping construct. Automated tests
To run the automated tests for this exercise, run the following command at the Linux command-line:
$ py.test -xvk idx
Grading¶
Recall that you will receive two S/N/U scores for this assignment: one for completeness and one for code quality.
Completeness SNU Score¶
Your completeness score will be determined solely on the basis of the automated tests, which provide a measure of how many of the tasks you have completed and the complexity of those tasks.

Grade Weighted test score
Satisfactory at least 90% Needs Improvement at least 50% Unsatisfactory less than 50%
For example, if your implementation has a weighted test score of 92%, you will earn a S (Satisfactory) score for completeness.
To determine your weighted test score, run the following at the Linux command-line (within your hw3 directory):
or you can run the one line version:
$ py.test -v; python3 grader.py
Code Quality¶
You are expected to adhere to the class Python Style Guide (https://uchicago-cs.github.io/student-resource- guide/style-guide/python.html#python-style-guide) . The guide covers many parts of Python that are not relevant for this assignment.
Here are some common code quality mistakes that we will be targeting in this assignment. Remember to review the code quality hints from HW #2 as well.
Requirements
Review your code to verify that you followed the requirements/restrictions listed with the questions.
Defined constants
Use the constants defined for LOWER , UPPER , etc instead of the associated integer value. Conditionals
Do not include conditional branches that have no computational effect. Do not write:
$ py.test -v
$ python3 grader.py
# Not good
if :
x=x+1 else:
x=x+0
or this:
# Not good
if :
x=x+1

(Recall that we use angle brackets to signal a placeholder. For example, should be replaced with an actual boolean expression.)
Write this code instead:
Similarly, do not use pass in the if branch:
Write this code instead:
Don’t use continue in this context either.
Choice of Loop
Make sure to choose the right loop style for your problem.
If you are working with the values in a list, write your code like this:
If you need to do a task that requires both the index and the value of a list element, write code like this:
If you need to do a task that requires both the index and the value of a list element, code like this is usually considered poor style in Python (even though it will feel normal to those of you with Java or C experience):
There is no reason to use a while loop for this assignment. Docstrings
# Good
if :
x=x+1
# Not good
if :
pass else:
x=x+1
# Good
if not :
x=x+1
# Good
for val in lst:

# Great
for i, val in enumerate(lst):

# Not good
for i in range(len(lst)):
val = lst[i]

else: pass

Make sure you have provided a complete and accurate docstring for every function. A complete docstring contains a brief description of the purpose of the function, a description of the each input (including its type) and a description of the return value, including its type (assuming the function returns a value).
The two most common mistakes are not writing a docstring at all and writing a docstring that does not include the types of the inputs or the type of the return value.
Overly long or complex code
Avoid writing code that has more cases than needed.
Avoid repeating code
Reuse earlier functions when appropriate. Note that it is OK to use the same loop header multiple times, but not the same loop body.
While these are the main things we care about in this assignment, please remember that it is not possible for us to give you an exhaustive list of every single thing that could affect your code quality score (and that thinking in those terms is generally counterproductive to learning how to program; see our Mistakes are an essential part of learning (https://canvas.uchicago.edu/courses/58316/pages/mistakes-are-an-essential-part-of-learning) page for more details).
Submission¶
Once you have completed the required updates to hw3.py , you must submit your work through Gradescope under Homework #3 (https://www.gradescope.com/courses/841256/assignments/4849836/submissions) . Gradescope will upload your files directly from your GitHub repository, so it is important that you remember to commit and push your work!
(If you have trouble with the link, you can find a link for Gradescope in the list of applications on the left side of our Canvas site. Follow the link and then click on Homework #3.)
Under “Repository”, make sure to select your cmsc14100-aut-2024/cs141-coursework-GITHUB_USERNAME repository. Under “Branch”, select main .
Make sure to check the results of the autograder on Gradescope. If your score is not what you expect, check to make sure you pushed your work to the server! If you pushed your work to the server and the score is still not what you expect, check your code to look for problems that you might have introduced as part of your final code quality check.

软件开发、广告设计客服
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 9951568
© 2021 www.rj363.com
软件定制开发网!