Assignment 1: Functioning with Python |
CSE 415: Introduction to Artificial Intelligence The University of Washington, Seattle, Winter 2008 |
[Updated Jan. 9] The reading for this assignment is Chapters 1-4 in Introduction to Python for Artificial Intelligence. |
Due Friday, January 18 through
Catalyst Collect-It
at 12:00 noon.
You can use a combination of input and output together with
comment lines (beginning with a pound sign) to show that you
have verified each of your answers on the computer.
For each function, provide the definition, a demonstration on the same example shown on this page,
and 3 additional examples: one shorter (or smaller),
one longer (or larger) and one more interesting
case (use your imagination).
Write Python function definitions for the following requirements (worth 10 points each). You should be able to infer what each function should do by a combination of reading its name and examining the relationship between its input and ouput on the given examples. Note that the functions that accept lists as arguments must be able to handle lists of any length. For this assignment your functions do not have to validate the types of their inputs. That is, you may assume that they will be called with arguments of the proper types. 1. two_x_plus_3(5) -> 13 2. sub_cipher("abc Iz this Secure?", 2) -> "cde Kb vjku Ugewtg?" 3. pair_off_and_add([2, 5, 1.5, 100, 3, 8, 7]) -> [7, 101.5, 11, 14] 4. plus_i([7, 13, 3, 2.2, 5, 100]) -> [7, 14, 5, 5.2, 9, 105] (Don't worry about number formatting.) (Note: There was an error in the first version of the assignment that has now been fixed: the 5.6 has been changed to 5.2.) 5. triple_con_cat([1, "ha", 7, "yeah! ", 13.5]) -> [3, "hahaha', 21, "yeah! yeah! yeah! ", 40.5] |
Optional for 5 points extra credit.
Write a Python program that makes up new poems in the
Haiku form. The Haiku form is as follows: each poem consists
of three lines. The first line has 5 syllables. The second
line has 7 syllables, and the third line has 5 syllables.
Rhyming is not usual in this form. Here's an example:
Artificial smarts Wires, chips, disks, and batteries Seems it's just hardwareYour program should use random choices from lists of words to make up its poems. Try to have the program's poems incorporate one or more themes of your choice. If you can, try to have the third line of each poem seem ironic or clever. Show five different Haiku produced by your program. |
Optional for 5 points extra credit.
Write a definition for a Python function permutations
that returns a list of all permutations of its
argument, which is a list.
permutations([0, 1, 2]) -> [[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]]Test your permutation function on range(4) and on ['red', 'green', 'blue'] as well as three more examples as per the instructions above. |