Assignment 1: Python Warm-up |
CSE 415: Introduction to Artificial Intelligence The University of Washington, Seattle, Winter 2023 |
The reading for this assignment is
Python as a Second Language.
|
Due Friday, January 13
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.11 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). The first choice, IDLE, is simplest and adequate for CSE 415. |
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_4(n): """Return True if n is a multiple of 4; 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 new_quadratic_function(a, b, c): """Create and return a new, anonymous function (for example using a lambda expression) that takes one argument x and returns the value of ax^2 + bx + c.""" 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 list_of_3_times_elts_plus_1(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 and then has 1 added to it.""" pass def quadruple_vowels(text): """Return a new version of text, with all the vowels quadrupled. For example: "The *BIG BAD* wolf!" => "Theeee *BIIIIG BAAAAD* woooolf!". 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 |
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.Each instance should have a number of sides. It should have a list of the lengths of sides; however, the value of this instance variable is allowed to be None, which is also the default value. It should have a list of angles (in degrees); its value is also allowed to be None. If the value of the angles list or the lengths list is None, that means that no (for example) lengths (or angles) have been given explicitly, but they might still be derivable from other information available. 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) 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. |