CS 120 02/Zelle Fall 2021

Intro to Computers and Programming

Dates and Times

All due date/times are given in Central Time (US). Please make sure you translate appropriately for your locale.

Submitting work

Items listed with due dates are to be turned in on Socrates (see link at the top of this page). For most assignments you will be attaching one or more files. It is up to you to double check that you have properly submitted your work. You can override a previous submission by simply submitting the file(s) again.

Acceptable File Types

Howework answers can be submitted as electronic documents; PDF is prefered, but MS Office formats (.docx), and plain text (.txt) are fine. You can also write answers on paper and submit a picture file. Any standard image format is fine (PDF, jpg, png, gif, etc). Using an app like "camScanner" and collecting multiple pages into a single PDF document is strongly encouraged.

For projects, you should always submit the code (module) file. This will be a plain text file containing the Python program code. It should have a ".py" at the end of the name, although your computer may not show you the extension. DO NOT submit output from a Python "Shell" Window. If the file you are submitting contains shell prompts (indicated with ">>>"), then it is the wrong file.

Unit 1

Reading: Chapter 1

Start reading Chapter 1 of the textbook.

(due 9/6, in-class) Chapter 1 Review Questions

Finish reading Chapter 1 and do the T/F and Multiple Choice Review Questions (pages 21-23) ON PAPER suitable for hand-in in class on Monday.

Reading: Chapter 2

Start reading Chapter 2.

(due 9/10, midnight) Program: chaosmods.py

Modify the chaos.py program from Chapter 1 as described in Programming Exercise #5 on page 25. Then do the experiment described in Exercise #6. Part a) is the original formula, and parts b) and c) give algebraically equivalent expressions. Try all three versions with THE SAME INPUT VALUE and look at the last 10 numbers that are printed. Do they look the same?

Add comments (lines starting with #) at the bottom of your program file that describes what you discover. Can you explain the results? Turn in your the final version of your program with the explanatory comments at the bottom.

(due 9/13, midnight) Program: avg3.py

Do the following:

  1. Download the code for the avg2.py for the textbook supporting pages (see link at the top of this page)
  2. Open avg2.py in IDLE.
  3. Select "file/save as" to create a copy called avg3.py.
  4. Modify the program so that it works for 3 scores instead of 2.
  5. Add a "pause" at the bottom that waits for the user to press the Enter key before quitting. Hint: we did this for the convert.py program in class on 9/10.
  6. Make sure your name is in the comments at the top of the file.

Turn your program in on Socrates.

(due 9/15, midnight) Program: lightning.py

Write a complete program to help a user calculate the distance to a lightning strike. The input to the program is the number of seconds between seeing the lightning and hearing the thunder. The output is the distance to the lightning in miles. For this problem use 1125 ft/sec for the speed of sound. There are 5280 feet in a mile. You should use these to numeric literals in your program and let Python do all the necessary calculations.

Make sure your program has a "nice" user interface including a short introduction and a clear prompt. A pause at the bottom is also a good touch.

Don't forget to put your name in the comments at the top of the program and turn it in on Socrates.

(due 9/15, class time) Chapter 2 Review Questions

Finish reading Chapter 2 and do the True/False and Multiple Choice Reviews Questions at the end of the Chapter. Have your answers on paper ready for hand-in at class time.

(due 9/17, midnight) futval2.py

Usually interest on a bank account or loan is compounded more than once a year. For example, it may be compounded monthly or quarterly. Let apr be the annual interest rate and periods be the number of compounding periods per year. In each compounding period, the amount of interest earned is calculated as principal * apr/periods.

Write a program that prompts the user to enter the initial amount of the investment (principal), the yearly interest rate as a decimal (apr), and the number of years for the investment (years). The program then prints out the value of the investment in years years.

Hints: Your loop will have to "go around" years*periods times, since that is how many times the interest is compounded. Don't forget that the rate used each time will be apr/periods.

As usual, turn your program (the .py file with your code) in on Socrates.

(due 9/20, class time) Chapter 3 Review Questions

Finish reading Chapter 3 and do the True/False and Multiple Choice Reviews Questions at the end of the Chapter. Have your answers on paper ready for hand-in at class time.

(due 9/22, midnight) pi.py

As described in class today, your assignment is to write a program to estimate the value of pi by summing terms of the sequence: 4/1 - 4/3 + 4/5 - 4/7 ...

The input to the program is, n, the number of terms of the sequence to sum. The outputs are the estimate of pi (the final sum) and the amount of error in that estimate (difference from the value of math.pi).

Notes: You should follow the hints that I gave at the end of class today (9/20). Use these variables: estimate (the accumulator) initialized to 0, numerator initialized to 4, and denominator is the loop variable (over the sequence [1, 3, 5, 7, ...]).

(due 9/27, midnight) pi.py extension

As discussed in class today, if you would like to resubmit your pi.py program, you have until midnight on Monday for that. Just use the original assignment link on Socrates to turn in any update.

Start reading Chapter 4 (sections4.1--4.4)

(due 9/29, midnight) picture.py

Write a program that uses graphics.py to draw a simple picture of something that is meaningful to you. You can include some Text objects, if necessary, to help explain your picture. It doesn't have to be elaborate.

Make sure to put a getMouse() at the end to pause for a mouse click so that I can admire your drawing.

Reminder: Exam 1 is 10/1

It will cover Chapters 1--4.

(due 9/29, classtime) Chapter 4 Review Questions

Finish reading Chapter 4 and do the True/False and Multiple Choice Reviews Questions at the end of the Chapter. Have your answers on paper ready for hand-in at class time.

(due 10/11, midnight) house.py

Do Programming Exercise #11 (Five-click House) on page 128.

Start reading Chapter 5

We'll start Chapter 5 on Friday.

(due 10/25 classtime) Chapter 5 Review Questions

Finish reading Chapter 5 and do the T/F and Multiple Choice questions at the end of the chapter. Have your answers on paper ready for hand-in at class time.

(due 10/27, midnight) cipher.py

Your assignment is to create a program to do word-wise "encryption" of messages using a simple circular shifting of letters (a Caesar cipher). The input to the program will be a message string consisting only of lowercase letters and spaces along with a "key," which is an integer. The output is the message with each character in the word shifted in the alphabet by the value of the key.

For example if the message is: "dr zelle rides at dawn" and the key is 3, then the encoded message will be "gu chooh ulghv dw gdzq". To decode a message, we can just run the program again with the negative of the key. When the input message is "gu chooh ulghv dw gdzq" and the key is -3, the output will be "dr zelle rides at dawn".

This program will require a more sophisticated algorithm than our programs so far. The basic idea is to go through the input message word by word and accumulate a list of encoded words. Each word will be encoded character by character. Here is pseudocode for the heart of the algorithm:

	   words = []  # a list to accumulate the new words
	   for each word in the message:
	       newword = ""   # a string to accumulate shifted
	       for each letter in word:
	           compute the shifted letter
	           add shifted letter to newword
	       append newword to words
	   join words with a space between to form newmessage
	   print newmessage

Notice how the two loops are nested. The ourter loop is looping over the words in the message, and the inner loop goes through the letters of each word.

Hints: create a string, alphabet, that contains the letters "abcde...z". Then you can use this sequence of statements to compute the newletter from letter:

	   pos = alphabet.find(letter)
	   newpos = (pos+key) % len(alphabet)
	   newletter = alphabet[newpos]

(due 11/1, classtime) Chapter 6 Reading

Read Sections 6.1--6.5. Be prepared to ask questions and apply this material in class.

(due 11/3 classtime) Chapter 6 Review Questions

Do the True/False and Multiple Choice Questions at the end of Chapter 6. Have your answers on paper ready for hand-in at the start of class.

ANNOUNCEMENT: Exam 2 is 11/10

Our second exam was scheduled for 11/8, but I am pushing it back a class day to 11/10.

(due 11/5, midnight) cipherfile.py

Write a batch-mode (see section 5.9.3) version of the cipher program. Your program must make use of the following three functions:

	 def encoded_word(word, key):
	     # word (string) is a single word to encode
	     # key (int) is the amount to shift each letter
	     # returns a string which is the shifted version of word

	 def encoded_line(line, key):
	     # line (string) is a line of lowercase letters and spaces
	     # key (int) is the amount to shift each letter
	     # returns a string which is the shifted version of line with
	     #         a single space between successive words

	 def encode_file(infile_name, outfile_name, key):
	     # infile_name is the name of a file containing a message
	     # outfile_name is the name of the file for the shifted text
	     # effect: the ouput file gets shifted text from the input file

	 def main():
	     # prints an introduction
	     # gets file names and key from user
	     # calls encode file to produce the output file
	     # prints a message when done

You should write your program one function at a time starting at the top of this list. Debug each function be testing it interactively in the IDLE shell before you move on to the next function. For example:

	 >>> encoded_word("hello", 3)
	 'khoor'
	 >>> encoded_line("hello cipherfile program", 3)
	 'khoor flskhuiloh surjudp'
	 >>> 

To test the last two functions (encode_file and main), you will need to create a testing file that contains a message to encode. You can create this file using IDLE; name it something like "test_data.txt". Your test data should have a message comprising multiple lines of text. When you run your program, you will have to supply a file name for the output. Make sure to choose a DIFFERENT file name so as not to wipe out your test data.

Read Chapter 7

You may safely skip section 7.4 for now.

(due 11/8, classtime) Chapter 8 Reading

Study sections 8.1--8.4, and 8.6.

Practice Exam Posted

Our second exam is coming up on Wednesday (11/10). I posted a "practice exam" under handouts/notices on Socrates. We will be going over parts of this exam in class tomorrow for review.

(due 11/17, midnight) bounce.py

Complete the ball bouncing ball animation that we started in class today (11/12). You will need to add if statements that check when the ball touches a Window edge. For example, when the center of the ball is within radius distance of the left or right edge of the window, then the sign of the x velocity needs to be reversed (velx = -velx). For the top and bottom edges, the y velocity needs to be reversed. Note, in any given iteration, both velocities may need to be reversed, so treat them as independent checks.

As per the code in class, the program should continue running until the user hits the "q" key on the keyboard.

Read Chapter 9

Read Chapter 9.

(due 11/23, midnight) pingpong.py

Modify the racquetball simulation program so that it simulates games of ping pong (aka table tennis). You should look up the modern rules of table tennis. Here is a quick summary:

The conversion from racquetball to ping pong should be mostly straightforward, but the service change rules are a little tricky. Here is a function that returns True when the service should switch based on the current scores:

	   def serviceShouldSwitch(scoreA, scoreB):
	       totalscore = scoreA + scoreB
	       return totalscore % 2 == 0 or totalscore >= 20 

It simply returns True when the total score is even (happens every 2 points) or when the game as been extended beyond 10-10. You can use it after awarding a point to check whether the service should change and then take action (or not) appropriately.

Another hint: the end of section 8.4 in the textbook has some hints on how you might handle the "must win by 2" rule.

(due 11/19) Read Chapter 10

Be prepared to ask questions and put the material into action in class on Monday!

Have a great break!

(12/13) Final Project

For our final project we are recreating a simple version of a classic Atari arcade game, Breakout. The project is divided into 4 phases that must be completed in order. The project is worth 40 points. Your overall grade for the project will be based on how many phases you complete and the quality of those phases. You must turn in each phase on Socrates as you complete it. For example, I will not grade your phase 4 project if you have not turned in a working phase 3 project. I encourage you to team up with a classmate or two. Three is the maximum team size.