Artificial Intelligence

Homework #2

Python Warm Up Programs

Due: January 31, 2006 by 12:29:59

Email your solution programs to me by the due date.  Click here to create an email with the appropriate mail subject.  Please turn in a printed listing of your source code.

  1. Write a complete Python program that reverses the elements in a list.  Prompt the user for the number of elements in the list first, and then prompt the user for each element. Write a function myreverse(x) that reverses the elements in list x in place.  Echo the list before and after the reverse function.  You may not use the list reverse() method.  Name your file
  2. myreverse.py.

  3. Write a complete Python program that creates a list of 20 random integers and sorts them using your own insertion sort function: insertsort(x).  Insertion sort works by keeping the initial part of the list sorted, say x[0:i].  (Remember, this means all the items from 0 up to but not including i.)  At the beginning, the single item in x[0:1] is sorted,  The item x[i] is moved down to its proper place by repeatedly exchanging it with its predecessors until it is in place, at which point x[0:i+1] is sorted.  The process continues until all items have been moved to their proper places.  Echo out the unsorted and sorted lists.
  4.   You may not use the list sort() method.  Name your file myinsertsort.py.

  5. Write a complete Python program that prompts the user for an input string of letters in the alphabet {a, b, c} and determines if the string is of the form an1cabn2 (ni can be non-negative).  The program should have a function called strmatch(s) and you must implement a recursive solution for this problem.  The function should return True or False appropriately.  Echo the input string and display a message if it "recognizes" this string.  NOTE: you may not use any of the built-in regular expression facilities in Python to solve this problem.  Name your file mystrmatch.py.