DATA 420 — Modeling and Simulation — Spring 2024

Python workout

Possible experience: +14XP

Due: Mon, Jan 22nd, midnight

Overview

In this initial coding 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. If you're using an IDE (Spyder, IDLE, PyCharm, etc) there should be some menu you can find that will tell you your Python version. If you're on the command line, you can type "python --version" 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

CPSC students: 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.)

DATA students: although it won't be until spring break, you'll eventually need to learn how the OO ("object-oriented") aspects of Python work. That's sections 9.3 and 9.5 (and all subsections 9.3.*, 9.5.*) of the tutorial, below. Go ahead and read through those sections now just as a preview of what you're up against.

Whether or not this is your first Python experience, go through the following sections of the official Python tutorial (make sure that the drop-down box at the top says at least "3.10"):

Supporting files

The file pythonworkout.py contains a test scaffolding for this project. The best way to get this file is to clone the class github repo somewhere on your computer, and then you'll be able to easily get updates for the class all semester. The not-so-good way is to copy/paste the text from that page into a file in your editor/IDE, and save the file with the appropriate name. Choose the second way only if you want to delay as long as possible getting familiar with the tools and techniques actually used in the real world.

You'll be adding your own code to this file in order to practice your Python and to earn XP, as described in the Requirements section below. You can run it at any point and it will tell you how many XP you've earned so far.

Your mission

After obtaining the file named pythonworkout.py via any of the above strategies, make a copy of it named yourumwid_pythonworkout.py. You can use a copy command, OS right-click operation, "Save as..." operation in your IDE, or any other way you can think of to make this copy. (Please do not name your copied 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_pythonworkout.py, with your actual (lowercase) UMW Net ID substituted for "yourumwid". For instance, "jsmith19_pythonworkout.py" is a correct name.)

You'll write all your code in the newly-created yourumwid_pythonworkout.py file. The original pythonworkout.py file can and should stay put.

Now, run your yourumwid_pythonworkout.py file. (In an IDE, it might be a little green arrow button, or the F5 key. On the command line, it will be the command "python yourumwid_pythonworkout.py".)

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:

Some variables incomplete or incorrect.
(plus2() incomplete.)
(center() incomplete.)
(nuke_last() incomplete.)
(middlest() incomplete.)
(tack_on_end() incomplete.)
(wondrous_count() incomplete.)
(unique_vals() incomplete.)
You've earned 0/14 XP!

You are now ready to begin coding.

Requirements

In your yourumwid_pythonworkout.py file, locate the section that says "(Add your code here.)" This is where you will add your code. Create the following variables and functions, in any order you choose:

  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 list called C which contains only the string 3PO.
  5. A variable named WALL which is a list of one element. That element is also a list. That (internal) list contains one element: the string with the letter E.
  6. A dictionary Galactica that contains the following key/value pairs (all strings):
  7. KeyValue
    KarlHelo
    KaraStarbuck
    LeeApollo
  8. 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:
  9. KeyValue
    SharonBoomer
    MargeRacetrack
    LouanneKat
  10. A variable Germanna_levels that contains a list of the two strings Freshman and Sophomore.
  11. A variable UMW_levels that contains a list of the four strings Freshman, Sophomore, Junior, and Senior.
  12. A variable MaryWash_levels which refers to the same list that UMW_levels refers to.
  13. 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.
  14. A one-argument function called plus2 that takes a numeric argument and returns that argument added to the number 2.
  15. A function 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".)
  16. A function 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.
  17. 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.)
  18. 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.
  19. 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.)
  20. 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']).

Whenever you run the code, your score will appear at the bottom of the output.

Turning it in

For this assignment, send an email to data420submissions@gmail.com with subject line "DATA 420 Python workout turnin", with your yourumwid_pythonworkout.py file attached. (Double-check to make sure it's really attached!)

Getting help

Come to office hours, or send me email with subject line "DATA 420 Python workout help!!"