In this assignment, you will develop a collection of Python functions. This particular assignment is "optionally collaborative." If you wish, you may form a partnership with one other student in the class and work on this assignment together. Then you'll each submit a copy of your work, but including in the comments in the Python file the name of your partner. Partners are not required for this assignment, and students are fine doing this assignment individually.
Note that we will be working in the Python 3.x family of versions, not Python 2.x. As of August, 2020 the latest stable released version in the 3.x family is Python 3.8.5. That is the version recommended for this assignment. However Python 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, or 3.7 should be OK, too. Do NOT use Python 2.7 in this course. There are some differences in the language syntax and semantics from 2.7 to the 3.x versions that will lead to difficulties such as failure to compile, semantic bugs, or incompatibility with starter code or recommended modules.
After you have installed Python 3.8 on your computer, you can begin this assignment by downloading its starter code. Rename the file such that its name starts with your UWNetID and ends with "_p1.py". For example, my solutions file would be named "tanimoto_p1.py". Then turn that in through Canvas by the deadline. If you wish, you may develop the functions in separate files. However, when the functions are ready, put them all into one file, as if you had edited them all into the starter-code file.
With the exception of the function
find_the_number
in Exercise 8, none of your functions should
use the input
function or the print
function.
However, you may use the print
function during debugging and
then comment out the lines of code having the print function before you
turn in the code.
It's recommended that you test your functions using the
P1 student autograder, as you go along developing them, and make sure they
at least pass these tests before submitting your work. To use the autograder,
first edit its first line so that it imports your file rather than johnnysmith_p1
triple(n)
. It should take one argument and return 3 times
that argument. (This first exercise is actually done for you in the starter code. This
serves as a simple example.)
five_x_squared_plus_3_x_plus_2(x)
. This will accept
a number and return the value of 5x2 + 3x + 2.
please_repeat_that(something)
. It should return a string
that embeds two copies of that something. For example,
please_repeat_that("Hi there!") # --> "Hi there! -- I said, Hi there!" please_repeat_that(2019) # --> "2020 -- I said, 2020"Here the first part of each comment "# -->" just means "evaluates to". Notice that the argument
something
can be a string, but it doesn't have to
be. So you function should "cast" it to a string, using something like this:
something_string = str(something)Then it should create the long string to be retured. You'll use string concatenation for this.
roll_die()
that returns a randomly-chosen number that is either 1, 2, 3, 4, 5, or 6.
To Test your roll_die
function separately, add the following code in your file.
def test_roll_die(n_times): print("Let's roll a die "+str(n_times)+" times:") for i in range(n_times): print(roll_die())
fifth_root
that returns the fifth root of its argument,
which should be a number. Feel free to use the exponentiation operator **.
It should behave like this:
>>> fifth_root(5) 1.379729661461215
makepal
that will make a palindromic sequence
of whatever sequence is given to it, whether it is a list or a string, or a tuple.
You might use slicing to create
the reverse sequence. For example ...
>>> 'abcde'[-1::-1] 'edcba'Your function should be able to do these transformations, for example:
>>> makepal("AB") 'ABBA' >>> makepal([1,2,3]) [1, 2, 3, 3, 2, 1] >>> makepal((1,2,3)) (1, 2, 3, 3, 2, 1)
Movie
and using it
in processing movie information.
Create a class definition for movies, in which each
Movie has a genre (comedy, drama, scifi, horror, monster, western, thriller, documentary, music, animation, film noir, romance),
a title, a director, a release date, a language, a list of leading actors, and a short plot summary.
The class should have an __init__
method, and a __str__
method.
Then create 5 instances for which there are 3 having the same genre.
Finally, write a function that will filter movies by genre. Here is suggestion
for how it would be defined and used.
class Movie: # Your code goes here. def by_genre(movie_list, genre): # Your code goes here. # your examples could be set up here. For example, jane_eyre = Movie('romance', 'Jane Eyre', 'Stevenson',\ '1943', 'English', ['Orson Wells','Joan Fontaine'], \ 'After a harsh childhood, orphan Jane Eyre is hired '+\ 'by Edward Rochester, the brooding lord of a mysterious '+\ 'manor house, to care for his young daughter.') # more examples needed here. >>> monster_list = by_genre([jane_eyre, godzilla, citizen_kane, rodan, alien], 'monster') >>> for m in monster_list: print(m.title) Godzilla Rodan Alien >>> print(jane_eyre) "Movie with genre romance, title 'Jane Eyre', directed by Stevenson, released during 1943 in English, with leading actors Orson Wells, Joan Fontain; plot summary: After a harsh childhood, orphan Jane Eyre is hired by Edward Rochester, the brooding lord of a mysterious manor house, to care for his young daughter."You might like to use the website imdb.com to get some information about some movies to use in your examples. If you wish to review the topic of creating your own classes and objects in Python, see (a) the lecture slides on "Python Class Definitions" and (b) this example using world rivers.
find_the_number()
, where the computer
"thinks" of a number (which should be an integer) and then the user asks questions about the
number. The computer's number will be greater than or equal to 0 and less than 100.
The user's questions will essentially mean
"If I subtract m from your number, is the result divisible by p?"
(Here the rule is that p must be a prime number, and m must be less than p.)
The user can simply enter m and p. The user should also be given the
option to guess the number itself, or quit playing.
As part of this game implementation, you should provide the definition of
a couple of other functions: is_prime(p)
and does_p_divide_n_minus_m(p, n, m)
.
Here is an example of a session your program might generate, with a user playing the game.
>>> find_the_number() Welcome to the Find-the-Number game! I am thinking of an integer between 0 and 99. You may ask me questions about it, or try to guess it. Your score will be 50 minus the number of questions you ask and guesses you make. (A)ask? (G)guess? or (Q)quit? --> A Enter m, p to ask "Is the number (n) minus m divisible by p?" --> 1, 3 No, n - 1 is not divisible by 3. (A)ask? (G)guess? or (Q)quit? --> A Enter m, p to ask "Is the number (n) minus m divisible by p?" --> 0, 2 Yes, n - 0 is divisible by 2. (A)ask? (G)guess? or (Q)quit? --> G What's your guess? --> 18 Sorry, 18 is not my number. (A)ask? (G)guess? or (Q)quit? --> A Enter m, p to ask "Is the number (n) minus m divisible by p?" --> 2, 5 No, n - 2 is not divisible by 5 (A)ask? (G)guess? or (Q)quit? --> G What's your guess? --> 30 Yes, you guessed it! Your score is 45. Thanks for playing. Bye, now. >>>Here is another game session.
>>> find_the_number() Welcome to the Find-the-Number game! I am thinking of an integer between 0 and 199. You may ask me questions about it, or try to guess it. Your score will be 50 minus the number of questions you ask and guesses you make. (A)ask? (G)guess? or (Q)quit? --> Q What, you are quitting? OK. My number was 13. Your score is 0. You'll have to try harder in the future. >>>
Submit your solutions file via Canvas. Due on Wednesday, September 2 at 5:00 PM.