Assignment 1: Python Warm-up
CSE 415: Introduction to Artificial Intelligence
The University of Washington, Seattle, Autumn 2023
The reading for this assignment is Python as a Second Language.
Due Friday, October 6 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 a1_exercises.py is a starting template. Edit this file to develop your solutions for Part 1. The file test_functions.py contains a few simple tests that will help you in debugging your solutions. To run the tests, in bash or other Linux environment, type:

python -m unittest test_functions.py
Part 1: Defining Functions (30 points).
 
Complete the definitions of the following functions...

def is_triple(n):
    """Return True if n is a multiple of 3; 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 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

Pat 2: Generative AI (10 points).
 
In this part of the assignment, we'll explore the use of classes and other Python constructs in automatically generating text messages using a small probabilistic language model.

Here, you will work with existing code to complete a program that can perform a simple type of generative AI. It will generate text strings in the style of short text messages. It will use a simple and small language model. The model consists of some lists of words and a probabilistic context-free grammar.

An example of a message is

  "slowly avoided crazy boss lol. boss ranted omg."

You are given 3 files:

  generate.py
  vocabulary.py
  grammar.py

You'll make the following modifications to these files.

  • generate.py:
    Fill in the missing code in the two incomplete functions: gen_text and gen_overall_message. Then test your code by executing on the command line:
       python generate.py
    
    Debug as needed, so that the program generates random text messages based on the original vocabulary.
  • vocabulary.py:
    Change the word list for NOUN to a list of breeds of dogs and cats, plus a few items relevant to them. The first word in the list must be "husky".
    Add a new word list ANIMAL_SOUND and give it 5 to 10 members. The first item in this list must be "arf".
  • grammar.py:
    Add a new rule for EVENT that allows an ANIMAL_SOUND to be part of the event description. Put this rule after the other EVENT-headed rules.

If you need to add any other new word lists to vocabulary.py, you may do this, and if you need to add any additional rules to grammar.py to make your adjusted generation program work better, you may do this. However, the first rule in each existing group should remain as the first rule in the group, and the first word in each existing word list remains the first, so that with CHOICE_MODE equal to "first" in generate.py, the output of the program is not affected by your changes to grammar.py and vocabulary.py.

Turn in your updated three files as part of the overall assignment.

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, 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
  1. Q. Will we be graded on style?
    A. See the above.
  2. 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.
Turn-In Instructions Turn in your four Python files, named as a1_exercises.py, generate.py, grammar.py, and vocabulary.py. Turn them in via GradeScope.
Updates and Corrections
 

If needed, updates and corrections will be posted here, and/or in ED. (Note: in the starter code, the file test_functions.py was last updated at 8:30 PM on Sept. 26.)