CPSC 415 - Artificial Intelligence - Fall 2023

Program #0 — Python practice

Possible experience: +20XP

Due: Fri, Sept 1st, midnight

Overview

In this initial programming assignment, you'll get a little practice (or refresher) programming in Python.

Installing Python

If you already have Python 3.10 or higher installed, you can use it. (Type "python --version" at a command line to find out. Btw, "command line" likely means "Power Shell" if you're a Windows user, "Terminal" if you're a MacOS user, and "command line" or "terminal" if you're a Linux user like me.)

If your Python is out of date or non-existent, you can download it for your operating system and install it. I also heartily encourage the use of the enhanced IPython interactive shell.

Learning Python

If you have already learned the non-object-oriented parts of Python, and feel comfortable developing in it, you'll just need to learn how the OO aspects work. That's sections 9.3 and 9.5 (and all subsections 9.3.*, 9.5.*) of the tutorial, below.

If you've never used Python before, or you did long ago and have forgotten everything, don't despair: it's a snap. Anyone who has made it through CPSC 110, 220, and 240, all with a C or better, is more than capable of picking up Python as an additional language. (You'll do this picking-up-languages thing often in your career, btw.)

There are tons of Python tutorials out there, but let's use the official one (python.org) for which I'll give you some specific topics to focus on.

First, go to the tutorial page, and make sure that the drop-down box at the top says at least "3.10". Then, scroll down and read the following sections:

("Read" means "actually pay attention and read for understanding." You may not want to attempt this all in one go. Injecting caffeine halfway through (or at the ⅓ and ⅔ marks) can help. "Browse/skim" means "get a feel for what's in there.")

Btw, I'm not discouraging you from reading any of the other tutorial sections as well if you're interested. You could even make use of that material in various projects this semester. The above is kind of a "must have" list, though. If you know those sections, I think that's a minimally sufficient set and you'll be good to go.

Supporting files

The file pythonpractice.py contains a test scaffolding for this project. It, together with the solution file you write yourself, will combine to verify all your code and get you points.

Your mission

Your job in this assignment is to create a Python file called yourumwid_pythonpractice.py in which you will create various variables, classes, and functions as described in the Requirements section below. These can appear in pretty much any order. Read all the instructions carefully as you work.

Getting started

Begin by copying the pythonpractice.py file to the directory/folder you'd like to use for this homework. You can do that by clicking/copying/pasting from the above, or by telling your browser to save the file in the appropriate place, or, if you're using a decent command line, by typing this command while in the directory you want to use:

$ wget http://stephendavies.org/cpsc415/pythonpractice.py

Then, use vim, Notepad++, Spyder, Eclipse, IDLE, or whatever other editor you choose to create a file in your directory/folder (the one that has pythonpractice.py in it) named yourumwid_pythonpractice.py. (Please do not name your file anything else. Please do not omit the suffix, or change the capitalization, or add/remove underscores, or do any other clever or creative things. Please call it exactly yourumwid_pythonpractice.py, with your actual (lowercase) UMW Net ID substituted for "yourumwid". For instance, "jsmith19_pythonpractice.py" is a correct name.)

Now, get the test framework to run. If you're running at the command line, type:

$ python3 pythonpractice.py yourumwnetid

If you're running in an IDE, execute whatever the "run" command is on that IDE, making sure to specify yourumwnetid as a command-line argument. (On Spyder, I happen to know you access this through "Configuration per file..." from the "Run" menu, then check "Command-line option" and type the argument in the little text box.)

Either way, make sure you get an output that looks something like this:

Variables incomplete or incorrect.
module 'yourumwid_pythonpractice' has no attribute 'YourumwidPythonPractice'
You've earned 0/20 points!

You are now ready to begin coding.

Requirements

Add the following variables, classes, and functions in your yourumwid_pythonpractice.py file, in any order:

  1. A variable called HAL whose value is the integer 9000.
  2. A variable named BB whose value is the string containing only the digit 8.
  3. A variable called R2 whose value is a list with elements D and 2, both strings.
  4. A variable called L whose value is a list with the element 3, an integer.
  5. A set called K which contains only the string 2SO.
  6. A list called C which contains only the string 3PO.
  7. A variable named WALL which is a list of one element. That element is a set. The set contains one element: the string with the letter E.
  8. A set called Poppins that contains all the unique lower-case letters from the string "supercalifragilisticexpialidocious". (You should do this intelligently, without sifting through the word and manually computing all the duplicates by hand yourself.)
  9. A dictionary Galactica that contains the following key/value pairs (all strings):
  10. KeyValue
    KarlHelo
    KaraStarbuck
    LeeApollo
  11. After creating your Galactica dictionary with the above three key/value pairs, add code using the [] notation that adds the following additional three key/value pairs to it:
  12. KeyValue
    SharonBoomer
    MargeRacetrack
    LouanneKat
  13. A variable Germanna_levels that contains a list of the two strings Freshman and Sophomore.
  14. A variable UMW_levels that contains a list of the four strings Freshman, Sophomore, Junior, and Senior.
  15. A variable MaryWash_levels which refers to the same list that UMW_levels refers to.
  16. A variable CNU_levels which, although referring to a list with the identical contents as the UMW_levels list, is nevertheless a different list object.
  17. A class named YourumwidPythonPractice (with your actual (first-letter-capitalized) UMW ID substituted, of course) that contains the following methods:
    1. A one-argument method called plus2 that takes a numeric argument and returns that argument added to the number 2.
    2. A method called gimme_dat_set that takes no arguments and returns a set containing the strings Pris, Leon, and Roy. Each time gimme_dat_set() is called, it should return the same set object.
    3. A method called gimme_set_like_dat that takes no arguments and returns a set containing the strings Neo, Morpheus, and Trinity. Unlike gimme_dat_set(), every time gimme_set_like_dat() is called it should return a different set object (with the same contents).
    4. A method center that takes one argument, a string. If that string is the empty string, center should return None. If the argument has an odd number of letters, the method should return the one in the exact middle. If the argument has an even number of letters (greater than 0), the method should a string with the two middle ones. (For example, calling the method with the value "Optimus" should return the string "i", whereas calling it with "Terminator" should return the value "in".)
    5. A method nuke_last which takes two arguments, a list and an item. The method should modify the passed list in place to remove the last entry matching the element. If the item passed is not in the list, the method should do nothing. (For example, if a variable "droids" has the value [ 'R', 2, 'D', 2, 'C', 3, 'P', 'O' ]", then after calling the method with droids and 2, the variable should have the contents [ 'R', 2, 'D', 'C', 3, 'P', 'O' ]".) The method need return nothing.
  18. A function middlest that takes three (numeric) arguments, and returns the value of the middle one in numerical order. (For example, middlest(5,7,2) should return 5. In case of ties, do what's obvious.)
  19. A function tack_on_end that takes three arguments, the last one optional. Argument #1 is a list. Argument #2 may be either a list or an atomic type (like a string or integer). The function should add either the elements of argument #2, in order (if argument #2 is a list), or argument #2 itself (if argument #2 is anything else) to the end of argument #1. It should do this a number of times equal to the third argument, whose default value should be 1. (For example, suppose a variable "colonies" has the value ['Caprica','Scorpia','Gemenon']. If tack_on_end(colonies,['Aerelon','Libris']) is called, colonies should then have the value ['Caprica','Scorpia','Gemenon','Aerelon','Libris']. Then, if tack_on_end(colonies,'Aquaria',4]) is called, colonies should have the value ['Caprica','Scorpia','Gemenon','Aerelon','Libris','Aquaria','Aquaria','Aquaria','Aquaria'].) The function need return nothing.
  20. A function wondrous_count that takes one integer, and returns the number of steps required to reach 1 from that number in a Collatz sequence. The Collatz Conjecture (never yet proven) postulates the following. Take any positive integer. If it is even, divide it in half. If it's odd, triple it and add one. Keep doing this until you get the number 1. The Collatz Conjecture hypothesizes that every number will eventually reach 1 this way. (Examples: wondrous_count(1) should return 0, wondrous_count(3) should return 7, and wondrous_count(16) should return 4.)
  21. A function unique_vals that takes a dictionary and returns a list (in any order) of the unique values of that dictionary. For instance, unique_vals({'Boomer':'cylon','Helo':'human','Apollo':'human'}) should return the list ['cylon','human'] (or ['human','cylon']).
  22. Lastly, a class named ProbabilityEngine that can compute the probability of various events (sets of possible outcomes in an uncertain situation) and generate simulated outcomes. This class should operate as follows: To help you understand how ProbabilityEngine is supposed to work, walk through the following sample session:
  23. >>> pe = ProbabilityEngine() >>> pe.add_outcome('Democrat',31) >>> pe.add_outcome('Republican',29) >>> pe.add_outcome('Independent',36) >>> pe.add_outcome('Green',2) >>> pe.compute_prob(['Democrat','Green']) 0.33673469387755106 >>> pe.compute_prob(['Republican','Democrat']) 0.6122448979591837 >>> for _ in range(12): >>> print(pe.generate_outcome()) Republican Independent Independent Democrat Green Independent Republican Independent Independent Democrat Democrat Republican

When you run "python3 pythonpractice.py yourumwnetid", your score will appear at the bottom of the output.

Turning it in

For this assignment, I just want your single Python file attached to an email with subject line "CPSC 415 Program #0 turnin". Double-check to make sure it's really attached!!

Getting help

Come to office hours, or send me email with subject line "CPSC 415 Program #0 help!!"