Assignment 1: Python Warm-up |
CSE 415: Introduction to Artificial Intelligence The University of Washington, Seattle, Winter 2021 |
The reading for this assignment is
Python as a Second Language.
|
Due Friday, January 15
at 23:59 PM, via GradeScope.
|
Overview: This assignment consists of several Python exercises designed to help you get more familiar with the language. If you are new to Python, do the associated reading. Otherwise, this is mainly a refresher with a little problem-solving thrown in. Use Python 3.8 for this assignment. |
Optional: Installing Python and Conda: You should use one of the following three choices for a Python installation for CSE 415: (1) IDLE (simplest to set up and to learn, but does not have as many features as the other choices, but it is sufficient for CSE 415); (2) a Conda-managed installation of command-line Python, under Linux, Darwin, or Cygwin (advanced Python developers do this especially if they need to support alternative Python versions and package installations); (3) PyCharm (a commercially-developed, state-of-the-art Python development environment). You may find these videos helpful as you install Python software for this assignment: Installing Conda; Using Conda with PyCharm (Note: Using PyCharm is optional). |
Starter Code: Starter code is available here: a1_starter_code.zip.
The file python3 -m unittest test_classes.pyand python3 -m unittest test_functions.py |
Defining Functions (30 points).
Complete the definition of the following functions... def is_multiple_of_9(n): """Return True if n is a multiple of 9; False otherwise.""" pass def next_prime(m): """Return the first prime number p that is greater than m. You might wish to define a helper function for this. You may assume m is a positive integer.""" pass def quadratic_roots(a, b, c): """Return the roots of a quadratic equation (real cases only). Return results in tuple-of-floats form, e.g., (-7.0, 3.0) Return "complex" if real roots do not exist.""" pass def perfect_shuffle(even_list): """Assume even_list is a list of an even number of elements. Return a new list that is the perfect-shuffle of the input. For example, [0, 1, 2, 3, 4, 5, 6, 7] => [0, 4, 1, 5, 2, 6, 3, 7]""" pass def triples_list(input_list): """Assume a list of numbers is input. Using a list comprehension, return a new list in which each input element has been multiplied by 3.""" pass def double_consonants(text): """Return a new version of text, with all the consonants doubled. For example: "The *BIG BAD* wolf!" => "TThhe *BBIGG BBADD* wwollff!" For this exercise assume the consonants are all letters OTHER THAN A,E,I,O, and U (and a,e,i,o, and u). Maintain the case of the characters.""" pass def count_words(text): """Return a dictionary having the words in the text as keys, and the numbers of occurrences of the words as values. Assume a word is a substring of letters and digits and the characters '-', '+', *', '/', '@', '#', '%', and "'" separated by whitespace, newlines, and/or punctuation (characters like . , ; ! ? & ( ) [ ] ). Convert all the letters to lower-case before the counting.""" pass def make_cubic_evaluator(a, b, c, d): """When called with 4 numbers, returns a function of one variable (x) that evaluates the cubic polynomial a x^3 + b x^2 + c x + d. For this exercise Your function definition for make_cubic_evaluator should contain a lambda expression.""" pass |
Classes (20 points).
In this exercise we'll explore the use of classes in keeping track of a set of polygons. Create a class called Polygon. (Note: The following line of code has been corrected, and the line beginning with "def" has been added as of Jan 12.) class Polygon: def __init__(self, n_sides, lengths=None, angles=None): # Additional code will go here.It should have a number of sides. It should have a list of length of sides, except its default value is None It should have a list of angles (in degrees), except its default value is None. Write methods ... def is_rectangle(self): """ returns True if the polygon is a rectangle, False if it is definitely not a rectangle, and None if the angle list is unknown (None).""" pass def is_rhombus(self): pass def is_square(self): pass def is_regular_hexagon(self): pass def is_isosceles_triangle(self): pass def is_equilateral_triangle(self): pass def is_scalene_triangle(self): passHere are some examples of how this class might be used: my_tri = Polygon(3, [3,4,5]) my_tri.is_rectangle() # False my_tri.is_isosceles_triangle() # False my_tri.is_scalene_triangle() # True your_tri = Polygon(3, angles=[60, 60, 60]) your_tri.is_equilateral_triangle() # True your_tri.is_isosceles_triangle() # True your_tri.is_scalene_triangle() # False my_rect = Polygon(4, angles=[90, 90, 90, 90]) my_rect.is_rectangle() # True my_rect.is_square() # None (unknown) # Note: the angle list below has been corrected as of April 1 at 1:12 PM. your_diamond = Polygon(4, [1, 1, 1, 1], [114, 66, 114, 66]) your_diamond.is_rectangle() # False your_diamond.is_rhombus() # True your_diamond.is_square() # False |
Testing For Valid Inputs In this assignment, you may assume that your Polygon class methods will always be called with valid arguments. In other words, your code does not have to test the arguments for the proper type, range of values, etc. |
About Using Python Libraries Unless otherwise stated in a CSE 415 assignment, you may use Python functions imported from the standard library, but you may not use external libraries (such as sympy). For example, you must define your own function for primality testing in this assignment, and you are not permitted to import some module that does that for you. |
About Style In Assignment 1, we do not plan to grade for coding style. However, we encourage students to become aware of PEP 8 -- Style Guide for Python Code, and code accordingly. The code we use in this course does not necessarily conform, but it is a good idea to be aware of these suggestions. |
Turn-In Instructions
Turn in your Python file, named as a1.py .
Turn it in via GradeScope. (Before the deadline, each
registered student should expect to receive a message from
GradeScope that confirms your registration in the course there.)
|
Updates and Corrections
A misleading code example was discovered and replaced for class Polygon
on Jan. 12.
If needed, additional updates and corrections will be posted here, and/or in ED. |