CPSC/DATA 350 — Applications of Databases — Fall 2024
Assignment #0 — Python practice
Possible experience: +25XP
Due: Mon
Sep 9th Wed Sep 11, midnight
Overview
This assignment simply guides you towards getting up to speed in Python so
you can perform competently in CPSC/DATA 350.
Installing Python
Ian has installed Python on the cpsc.umw.edu server. Make sure you
can connect to the server, as you did in CPSC 225 (see Ian's
notes if you need a refresher on how to do that). When you're logged in,
type this on the command line:
$ python3
and make sure you get some output followed by a prompt with a triple-wakka:
">>>". If you don't, email me right away with whatever error
message you get. Otherwise, quit the Python console by typing "CTRL-D".
Learning Python
If you're new to Python (or even if you're not) go to the official Python tutorial,
and make sure that the version number in the drop-down box at the top
matches what's on the cpsc server (3.10 as of this writing).
Then, grab a coffee, and actually read the following sections:
- 3
- 4—4.5, 4.7—4.8.5
- 5 (you can skip 5.1.3 and 5.1.4)
- 7—7.1.1
- skim 10
(I'm not discouraging you from reading any of the other sections as well if
you're interested. You could even make use of that material in various projects
this semester. The above is 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
for this semester.)
Getting some practice
Setting up the pythonpractice.py test script
The file pythonpractice.py
contains some little practice exercises. Copy it to your cpsc.umw.edu
account by typing this command at the cpsc prompt:
$ wget http://stephendavies.org/pythonpractice.py
and verify that it downloaded a copy of this file into your cpsc
server directory.
Now use vim to create a file in the same directory called
"yourumwnetid_pythonpractice.py" with nothing in it but a single
comment line. (Please do not call the file anything else. Please do not get
creative by using a shorter name, or a longer one, or changing the
capitalization, or omitting the underscore, or using a dash instead of an
underscore, or anything else. Thanks. Also, in case it's not clear, you
shouldn't literally call the file
"yourumwnetid_pythonpractice.py". Instead, you should substitute your
UMW Net ID. For instance, "jbieber2_pythonpractice.py" would be a
correct filename.)
Now run things with the command:
$ python3 pythonpractice.py yourumwnetid
(Again, just to be super clear, you need to substitute your UMW Net ID for
the word yourumwnetid in the command above. So "python3
pythonpractice.py jbieber2" would be a reasonable thing to type.)
Verify that the output you get is:
You've earned 0/25 XP!
Then take a break and stretch your legs.
Writing your practice code
Okay, now open up your yourumwnetid_pythonpractice.py file in vim,
and add the following variables and functions:
- A variable called HAL whose value is the integer 9000.
- A variable called L whose value is a list containing the integer 3.
- A variable named BB whose value is the string containing
only the digit 8.
- A variable called R2 whose value is a list with elements
D and 2, both strings.
- 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.
- A variable named K which is a set with one element: the string
2SO.
- A variable named C which is a list with one element. That
element is a set whose only element is the string "3PO".
- A set called Poppins that contains all the unique lower-case
letters from the string "supercalifragilisticexpialidocious". (You
should be able to do this without having to sift through the word and manually
compute all the duplicates by hand yourself.)
- A dictionary Galactica that contains the following key/value
pairs (all strings):
Key | Value |
Kara | Starbuck |
Lee | Apollo |
Louanne | Kat |
- 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:
Key | Value |
Karl | Helo |
Sharon | Boomer |
Marge | Racetrack |
- Then, after adding those three additional key/value pairs to it,
delete the pilot with call sign "Kat" (real name Louanne) from the
dictionary.(
- A variable Wheel which is a dictionary with strings as keys and
lists as values. Begin by adding these keys and values to it:
Key | Values (in list) |
Blue | Moiraine, Siuan |
Green | Alanna |
Purple | (nothing) |
White | Sarene |
Yellow | (nothing) |
Plaid | (nothing) |
Red | Elaida, Liandrin |
- Then, after creating your Wheel dictionary with the above values,
write code to make the following changes to it:
- Add a new key value pair: the key should be Brown and the
value should be a list with Verin, Saerin, and
Janya, in that order.
- Append the string Egweyne to the end of the Green
list.
- Remove Liandrin from the Red list.
- Add Nynaeve to the Yellow list.
- Delete the Plaid key/value pair altogether.
- A variable Germanna_levels that contains a list of the two strings
Freshman and Sophomore.
- A variable UMW_levels that contains a list of the four strings
Freshman, Sophomore, Junior, and
Senior.
- A variable MaryWash_levels which refers to the same list
that UMW_levels refers to.
- 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.
- A one-argument function called plus2 that takes a numeric
argument and returns that argument added to the number 2.
- A function 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.
- A function 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).
- 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 function should return the one in
the exact middle. If the argument has an even number of letters (greater than
0), the function should a string with the two middle ones. (For example, calling
the function with the value "Optimus" should return the string
"i", whereas calling it with "Terminator" should return the
value "in".)
- A function nuke_last which takes two arguments, a list and an
item. The function 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 function
should do nothing. (For example, if a variable "droids" has the value
[ 'R', 2, 'D', 2, 'C', 3, 'P', 'O' ]", then after calling the function
with droids and 2, the variable should have the contents
[ 'R', 2, 'D', 'C', 3, 'P', 'O' ]".) The function need return
nothing.
- 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.)
- 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.
- 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, wondrous_count(26) should
return 10, and wondrous_count(27) should return 111.)
- 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']).
Scoring yourself
Just run the program (even if you're not all the way done) and read the
output to see which problems you've gotten right and which ones you still need
to work on.
Turning it in
For this assignment, send me an email with subject line "CPSC/DATA 350 Python
Practice turn-in" and with your yourumwnetid_pythonpractice.py
file as an attachment. Note that this will require you to first download
your Python file from cpsc.umw.edu to your own personal machine.
Getting help
I'm happy to answer questions and give hints! Come to office hours, or send
me email with subject line "CPSC/DATA 350 Python practice help!!"