代写 html math python parallel Introduction

Introduction
ISTA 131 Hw1
Due: Thursday 9/5/2019 at 23:59:59 MST Python Review + Nested Loops
Version 1.0
This homework is intended to review/reinforce working with for loops, nested for loops, lists, lists of lists, and more.
Some of the function descriptions refer to a “matrix”, a list of lists where all “rows” have the same number of values, all of which are numeric, and rows are never empty. See slide 1.39 (lecture 1, slide 39) for an example. In a square matrix the number of rows and columns is equal.
Instructions
Create a source file named hw1.py with definitions for the functions described below. When done, submit hw1.py to the D2L dropbox for this assignment. It’s fine to submit multiple versions; we’ll grade the last one you submit before the deadline.
Don’t Post Your Code on Piazza!
Remember: If you have a question about your code, DO NOT post it on Piazza! Instead, mail it to ista131-questions.
It is fine to post Piazza questions that include code from hw1_test.py.
If a Piazza question you post seems to mysteriously disappeared, DO NOT REPOST IT! Instead, mail to ista131-questions. It may be the case that the question accidentally revealed part of a solution and was deleted by an SL or me.
If undecided about posting on Piazza or using mail, use mail!
Scoring
Point values are shown for each problem. There are a total of 90 points of problems. If you respond to our survey by its deadline (see slide 23 in lecture 1), you’ll get ten points for it, too.
Pay Attention to Restrictions
The specification for some problems includes restrictions, like not being able to use the sorted() built-in function. Just like many exercises in the gym focus on various muscle groups, I want you to build certain programming muscles when solving those problems. If your code violates a restriction, you’ll get no points for that problem.
We’re always willing to take a look at your code before it’s due to see if there are any violations. Just mail it to ista131-questions. Such inspections are done as time permits and available time decreases as the deadline nears.
Testing
Download the test module, hw1_test.py, and put it in the same directory as hw1.py.

Note: Lecture 2 has a section that talks about the unit testing machinery in some detail. Review it.
To test only one function, the practice I recommend when developing, use the -k option, which specifies to only run tests with names that contain the given string. Example:
python -m unittest hw1_test.py -k is_diagonal
To test all the functions use this command:
python -m unittest hw1_test.py
The unit tests in hw1_test.py are considered part of the spec; they provide examples of usage of the functions. We may use additional tests when grading but if you pass all supplied tests, your score will likely be no worse than 75 out of the 90 problem-based points.
Documentation
Your hw1.py does not need to include a comment with your name, NetID, section leader, or so forth!
This assignment is simply writing a number of functions that are described in this write-up, so there seems to be no good reason to require you to duplicate or paraphrase those descriptions for these functions. Therefore, no documentation for the functions is required. However, if you write any helper functions, those helper functions should contain a concise docstring explaining what the function does, citing any parameters used and any value returned.
Grading
Your module will be graded on correctness, documentation (although none may be needed), and good use of Python. Code should be clear and concise. Even if your code passes all tests, we may ask you to rewrite code that shows evidence of fundamental misunderstandings.
Resources
The Python Tutorial: https://docs.python.org/3/tutorial/index.html
String methods: https://docs.python.org/3/library/stdtypes.html – string-methods List methods: https://docs.python.org/3/tutorial/datastructures.html – more-on-lists The standard library: https://docs.python.org/3/library/index.html
Built-in functions: https://docs.python.org/3/library/functions.html
No cheating!
A number of students will find this to be a tough assignment. If you find yourself tempted to cheat, take another look at slides 17-21 in the first lecture to remind yourself of the severe consequences of being caught.
Remember: You’ve got six people ready to help you with this assignment! Take advantage of office hours, email, Piazza, and IM. Never hesitate to ask for help–that’s what you’re paying us for! And don’t wait to get started on this assignment!

Function specifications
middle: (2 points) This function returns the middle element of its only argument, which is assumed to be a three-element list. RESTRICTION: Your solution must not contain any square brackets ([ and ]) or periods.
around: (3 points) This function can be called be called with one or two arguments. If called with one argument, n, it returns a three tuple with the values n-1, n, and n+1 (the integers “around” n). The second, optional, argument can be used to specify a non-negative “interval” other than 1.
second_biggest: (6 points) This function returns the second largest value in its sole argument, a list (not a matrix!) containing at least two elements. If the largest value occurs more than once, it counts as the second largest also. RESTRICTION: You may not use the sorted() function or any other means of sorting.
contains: (5 points) This Boolean function takes a matrix and a value as its arguments. It returns Trueifthevalueisinthematrix,Falseotherwise. Thisisamembershiptest.
get_column: (6 points) This function returns a list representing a column of its first argument, a matrix. The second argument is an integer that specifies the column to get, using Python-style indices (e.g., 1 is the second column; -2 is the next to last column). The third argument is an optional Boolean. If not specified, or False, the result’s values are in top-to-bottom order; if True, the values are in reversed order, bottom-to-top.
is_diagonal: (8 points) This Boolean function takes a matrix as its sole argument and returns True if the argument is a diagonal matrix, False otherwise. In diagonal matrices, all off-diagonal elements arezero. (Seehw1_test.pyforexamplesofcallstois_diagonalandthecorrectcorresponding return values.) Examples of diagonal matrices:
http://www.sosmath.com/matrix/matrix3/img11.gif
is_upper_triangular: (7 points) This Boolean function takes a square, nonempty matrix as its soleargumentandreturnsTrueiftheargumentisanuppertriangularmatrix,Falseotherwise. In upper triangular matrices, all elements below the diagonal are zero. Examples:
http://www.sosmath.com/matrix/matrix3/img3.gif

biggest: (8 points) This function returns the largest value in its sole argument, a matrix. indices_biggest: (7 points) This function returns a list of 2-tuples that contain the indices of all
occurrences of the largest value in its sole argument, a nonempty matrix.
Feel free to be lazy as I am on this one and call your biggest function in your solution for
indices_biggest.
indices_divisible_by_3: (6 points) This function returns a list containing every element in its sole argument, a matrix, that has indices whose sum is divisible by three. Traverse by row on your outer loop, column on your inner to get the elements in the correct order.
create_matrix: (10 points) This function creates a matrix based on the contents of its sole argument, a string. Example:
>>> create_matrix(“1 2 3/ 40 3 1/ 3 1 3”) [[1, 2, 3], [40, 3, 1], [3, 1, 3]]
As you hopefully see, rows are separated by slashes; values in a row are separated by spaces. Think about first splitting the whole string on slashes and then loop through those pieces (which represent the rows), and split each piece on blanks.
Remember that the built-in int(s) function converts a string into an integer.
Assume the string is well-formed. You won’t see strings like “1 / / 2″ or ” x /y /z”, for
example. When grading we won’t test with any cases that aren’t in hw1_test.py.
corners: (2 points) This function returns a 4-tuple (a four-element tuple) with the corner values of its
argument, a matrix. The values are in this order: upper-left, upper-right, lower-right, lower-left.
print_nzp: (12 points) This function takes a matrix as its only argument and prints, repeat, prints a representation of where negative, zero, and positive values appear in the matrix. Example:
>>> m1 = [[0, 4, 0],
… [-3,0,-4],
… [5,-2, 3]]
>>> print_nzp(m1)
+—–+
|z p z|
|n z n|
|p n p|
+—–+
>>>
Note: The unit test for this function will display output in “diff” format, which is a lesson in itself. Be sure to test by hand before you try the unit tests.
sort_int_string: (8 points) This function takes one parameter: a string with a series of integers separated by one or more spaces, such as ” 7 -45 28 1 2482432 “. The function returns a

string with the argument¡¯s integers separated by one space and in sorted order. If the string is empty or contains no integers, the empty string is returned. Assume the string is well-formed; you won’t see something like “75-18 190” or “1 a 2 3″.
Example:
>>> sort_int_string(” 7 -45 28 1 2482432 “) ‘-45 1 7 28 2482432’
Extra Credit: Hours and Observations
(1) I always want to have some idea of how much time students are spending on assignments. For two points of extra credit include a function named estimated_hours that simply returns your best estimate of how many hours you spent working on the assignment. Example:
def estimated_hours():
return 12.34 # must be an int or a float!
There’s no ability to add any comment to it, like “I did this while watching TV”, so if you spent ten hours on it in front of the TV, then you might figure that translates to four or five hours of undistracted work.
(2) Cite an interesting course-related observation (or observations) that you made while working on the assignment. The observation should have at least a little bit of depth. Feedback and comments about the assignment are welcome, too. Was it too long, too hard, too detailed? Speak up! I appreciate all feedback, favorable or not.
For up to ten points of extra credit include a function named observations that returns a string with
your observations, feedback, whatever. You can use a triple-quoted string to preserve formatting.
Example:
def observations(): return “””
I didn’t touch Python at all over the summer so I was really rusty but I finally got my Python legs back after a few problems. (No legs on a Python–haha!)
I read on the web that parallel assignment is sometimes
called “destructuring assignment”. I found that this works, too:
>>> a,*b,c,d = [10,20,30,40,50,60] >>> b
[20, 30, 40]
Question: Why don’t we talk more about Python IDEs? “””
Feel free to write as much you want, but it’s quality, not quantity that will earn points. The above would probably be worth two or three points, but its an imprecise business.
Note that we’re asking you to report hours and observations using functions rather than comments so
that we can easily extract that information. Just to reinforce the idea, given a hw1.py with the
functions above, I can do this:
% python -i hw1.py >>> estimated_hours()

12.34
>>> observations()
‘\nI didn\’t touch Python at all over the summer so I was\nreally rusty but I finally got my Python legs back after\na few problems. (No legs on a Python–haha!)\n\nI read on the web that parallel assignment is sometimes\ncalled “destructuring assignment”. I found that this works, too:\n >>> a,*b,c,d = [10,20,30,40,50,60]\n >>> b\n [20, 30, 40]\n\nQuestion: Why don\’t we talk more about Python IDEs?\n’
Miscellaneous
For what it’s worth, here are line counts for my current solutions:
middle: 3
around: 2
second_biggest: 11
contains: 6
get_column: 8
is_diagonal: 7
is_upper_triangular: 7
biggest: 8
indices_biggest: 9
indices_divisible_by_3: 8
sort_int_string: 10
create_matrix: 10
corners: 2
print_nzp: 16
For those who are curious about such things, and I wish everybody was, here are the Bash commands I gave to produce the counts above:
csplit -k hw1.py “/^def /” {*}
for i in x*
do
echo $(grep ^def $i | xfield -d” (” 2): $(wc -l < $i) done