CS 5541: Artificial Intelligence Fall 2003

## Due date: Tuesday, September 16, 2003

For these problems, you need to program solutions in the Python language. Python installations are available on computer science department Sun systems (e.g., csdev01.d.umn.edu, csdev02.d.umn.edu ... csdev24.d.umn.edu), on Mac OS X systems if you have one (from a terminal window), and can be installed for free on your home MS Windows system from http://www.python.org

Documentation for Python can be found at:

UMD Safari

Python web site

Python robotics

1) [3 pts] Given the following value for "testState", show the result (outputs) of executing the following Python statements. Remember: parentheses are important, so only show them only when they are necessary.

testState = [['c11', 'c12', 'c13'], ['c21', 'c22', 'c23'], ['c31', 'c32', 'c33']]

 Statement Evaluated statement print testState[0] print testState[len(testState)] print testState[len(testState)-1] print testState[:] print testState[0:1] print testState[0:1][0] print testState + [['a', 'b', 'c']]

2) [2 pts] Write a Python function called "square" that accepts a single number, squares the number, and returns the squared value as a result of calling the function. Here's an example of calling your function:

square(8) => 64

3) [2 pts] Write a Python function called "listAdd" that accepts two parameters: a value and a list, and places the value (first parameter) at the start of the list. Here's an example of calling your function:

listAdd('single-value', ['a', 'b', 'c']) => ['single-value', 'a', 'b', 'c']

4) [4 pts] Write a Python function called "listAppend" which takes two list arguments, l1 and l2, and appends the list items in the two lists given. Here's an example of calling your function:

listAppend([1,2,3], [4,5, 6]) => [1, 2, 3, 4, 5, 6]

5) [4 pts] Write a Python function called "listReverse", which takes a single list parameter, and reverses the list. Your function must be written recursively, using no iteration techniques other than recursion. Do not use the built in list reverse function in Python. Here's an example of calling your function:

listReverse(['a', 'b', 'c', 1, 2, 3]) => [3, 2, 1, 'c', 'b', 'a']

You must also implement one of the following two functions: (6a) or (6b):

6a) [5 pts] Write a Python function called "traverse" which takes one argument, a list structured as a binary tree, and does an in-order traversal of the binary tree, returning all the non-empty node values in a list. Each node in the tree argument should have the structure:

[left-child-node value right-child-nod]) or it should be the empty list([]).

Here are some example tree definitions and usages of your function, "traverse":

testTree1 = [ [], 'a', [] ]

testTree2 = [ [ [[], 3, []], '*', [[], 4, []]], '+', [[], 2, []]]

traverse(testTree1) => a

traverse(testTree1) => 3*4+2

6b) [5 pts] Write a Python function called replaceAll that takes three parameters, a list L, and two arguments value and R. The function returns a new list constructed by searching the list elements of L for all occurrences of value, and replaces them with item R. If R is the empty list ([]), then all occurrences of value are deleted from the list L. Examples:

print replaceAll(['a', 'b', 'a', 'c'], 'a', 'x') => ['x', 'b', 'x', 'c']

print replaceAll(['a', 'b', 'a', 'c'], 'a', []) => ['b', 'c']

Submission: You need to turn in both hard copy of your program code, and also hard copy of tests of your functions.