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 a1.py is a starting template. Edit this file to develop your solutions. The two files test_classes.py and test_functions.py contain a few simple tests that will help you in debugging your solutions. These have been developed by our TA, Leo Du. To run the tests, in bash:

python3 -m unittest test_classes.py
and
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):
    pass
Here 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
  1. Q. Will we be graded on style?
    A. The staff reserves the right to deduct points for sloppy formatting or lack of comments or comments that are inconsistent, misleading, irrelevant or very unclear in their context.
  2. Q. In the polygon classes example, can we assume that we will be dealing with valid polygons?
    A. Yes.
  3. Q. When I submit my code, will additional test cases be run on my functions, other than those distributed with the starter code?
    A. In general, yes. Also, you are encouraged to develop test cases of your own, to help you debug your code's behavior on possible edge cases.
  4. Q. How should my code in the Polygon class know when to return True, False or None, for example with is_square?
    A. When it is possible to determine with certainty that the requirements (e.g., for a square) are satisfied, then return True. If some constraint is violated (e.g., when testing for a square, two sides have different lengths, or there exists an angle that is definitely not 90 degrees) then return False. Otherwise, return None.
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.)