| Assignment 1: Python Warm-up |
|
CSE 415: Introduction to Artificial Intelligence The University of Washington, Seattle, Winter 2025 |
|
The reading for this assignment is
Python as a Second Language.
|
|
Due Friday, January 17
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. We recommend Python 3.13 for this assignment. |
| Installing Python: You should use one of the following three choices for a Python installation for CSE 415:
|
| Starter Code: Starter code is available here: a1-starter-code.zip.
The file python -m unittest test_functions.py |
| Part 1: Defining Miscellaneous Functions (30 points).
Complete the definitions of the following functions...
def is_quintuple(n):
"""Return True if n is a multiple of 5; 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 the string "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_5_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 5 and then has 1 added to it."""
pass
def double_vowels(text):
"""Return a new version of text, with all the vowels doubled.
For example: "The *BIG BAD* wolf!" => "Thee *BIIG BAAD* woolf!".
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
|
| Part 2: Defining Methods for States and Operators (20 points).
In this part of the assignment, you'll write methods for classes that deal with state-space search. These methods are not implementations of search algorithms; those will come in the second assignment. Rather, they deal with the basic infrastructure needed for problem formulation. In problem solving, a state is a data object that represents the components of a possible solution or partial construction in attempting to find a solution, in one particular configuration. For example, in Tic-Tac-Toe, a state is a representation of the board at one particular point in a game. At the beginning of the game, there is the initial state which represents the empty 3-by-3 grid. Each time a move is made in the game, one state is used to construct another. Consider the following class definition for states of Tic-Tac-Toe. class TTT_State: def __init__(self): '''Create an instance. This happens to represent the initial state for Tic-Tac-Toe.''' self.board = [[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]] self.whose_move = 'X'Define the following methods for this class.
def __str__(self):
'''Return a string representation of the
state that show the Tic-Tac-Toe board as a 2-D ASCII display.'''
pass
def __deepcopy__(self):
'''Return a new instance with the same board arrangement
and player to move.'''
pass
def __eq__(self, other):
'''Return True iff two states are equal.'''
pass
Consider the following class definition for operators of Tic-Tac-Toe.
class TTT_Operator:
def __init__(self, who, row, col):
self.who = who
self.row = row
self.col = col
Define the following methods for this class of operators.
def is_applicable(self, state):
'''Return True iff it would be legal to apply
this operator to the given state.'''
pass
def apply(self, state):
'''Return a new state object that represents the
result of applying this operator to the given state.'''
pass
|
| About Style
In Assignment 1, we do not plan to grade for coding style.
However, 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. Also, 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 one Python file, named as a1_exercises.py.
Turn them in via GradeScope.
|
| Updates and Corrections
If needed, updates and corrections will be posted here, and/or in ED. |