Assignment 1: Python Warm-up |
CSE 415: Introduction to Artificial Intelligence The University of Washington, Seattle, Spring 2021 |
The reading for this assignment is
Python as a Second Language.
|
Due Friday, April 9
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.9 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_11(n): """Return True if n is a multiple of 11; False otherwise.""" pass def last_prime(m): """Return the largest prime number p that is less than or equal to 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 five_times_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 5.""" pass def triple_vowels(text): """Return a new version of text, with all the vowels tripled. For example: "The *BIG BAD* wolf!" => "Theee *BIIIG BAAAD* wooolf!". For this exercise assume the vowels are the characters 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_quartic_evaluator(a, b, c, d, e): """When called with 5 numbers, returns a function of one variable (x) that evaluates the quartic polynomial a x^4 + b x^3 + c x^2 + d x + e. For this exercise Your function definition for make_quartic_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.
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. |
Frequently Asked Questions
|
Turn-In Instructions
Turn in your Python file, named as a1.py .
Turn it in via GradeScope.
|
Updates and Corrections
If needed, updates and corrections will be posted here, and/or in ED. (Note that this page was updated at 1:30 PM on March 29. Any earlier versions should be considered obsolete.) |