Introduction to Computer Programming II

CSI32 - D01

Monday,Wednesday, 12:00 - 1:50pm, CPH 320

Date Materials HW assignment
12/12 Topics: Interview questions

Lecture slides: CSI32_Lecture27_InterviewQuestions.pdf
prepare for the Final Exam
12/07 topics: Interview questions

Lecture slides: CSI32_Lecture26_InterviewQuestions.pdf
prepare for the Final Exam
12/05 topics: Section 16.5 Peer-to-peer connection.

Lecture slides: CSI32-Lecture25.pdf
Programs:, (in progress)
no HW assignment
11/30 topics: Section 16.4 Chat room

Lecture slides: CSI32-Lecture24.pdf
HW14 (due date: Wednesday, December 7th):
As you can see our chat server is very clumsy.
One needs to keep pressing Enter to see if anything new popped-up, sees his/her messages sent/(receives them as well, including notification that the person joined the chat)... maybe you noticed something else.
Try to fix it (start with the second one)!
11/28 topics:
Section Section 16.3

Lecture slides: CSI32-Lecture23.pdf
no HW assignment
11/24 - 11/27  
11/23 topics: Chapter 16 Network programming
Sections 16.1and 16.2

Lecture slides: CSI32-Lecture22.pdf
no hw assignment
11/21 topics: Chapter 12 More Python Containers:
12.1 Two Familiar Containers: list and tuple
12.2 Dictionaries
12.3 Containers of containers
12.4 Set and Frozenset
12.5 Arrays

Lecture slides: CSI32-lecture21.pdf
Programs, data :,, data.txt,
HW13 (due date: Wednesday, November 30th):

implement addition of two matrices

- each input matrix is stored in a file
- the new matrix should be displayed and save in a output file

11/16 topics: Chapter 8 Input, Output and Files

Lecture slides: CSI32-lecture20.pdf
Programs, data :,,, data.txt

HW12 (due date: Wednesday, November 23rd):

p. 293-294 / 8.6 and 8.11

Addition for 8.6: if while searching through the file you find that a year is negative, raise a Value Exception, saying that a birth year cannot be negative.

For both of the programs: catch IOError and deal with them as it was dealt in project 18.
11/14 In-class project with event-handling:
We will write a program that can do a bunch of things:
1) Convert from Celsius degrees to degrees of Fahrenheit
2) Convert from Fahrenheit to Celsius degrees
3) Find the distance between two points (mouse clicks)
4) Convert from us pounds to kilograms
5) Convert from kilograms to us pounds

The original source code:

Important comments:
1) when working with "small windows" re-use the exitButtonHandler
2) Button is not working in those small windows. Instead use Rectangle and Text. Attach handler to both rectangle and text.
no hw assignment
11/09 topics: Section 15.4 and some discussion on mouse drag (two issues there)

Lecture slides: CSI32-lecture18.pdf
Programs :,,,
11/07 topics: Section 15.3

Please note that Chaper 15 has a lot of corrections/updates that were done after the book was printed, therefore here is an updated pdf of this chapter: ch15.pdf.

Lecture slides: CSI32-lecture17.pdf

suggested homework assignment (not for grade):
write a program that:
- draws three distinct objects (say a circle, a triangle and a rectangle), then
- user can drag the figure around the screen
- user can scale the figure (re-size)
- user can change the fill color of the figure
- user can remove the figure from the paper

The last three items can be done with keyboard, for example:
if 'm' key is pressed the figure is re-sized with a ratio of 2 (magnified),
if 'z' button is pressed, the figure is re-sized with a ratio of 0.5 (zoomed out),
if 'c' button is pressed, the fill color of the figure is changed,
if 'd' button is pressed, the figure is removed from the paper

All the events should be commented by a text message.

Here is a sketch of the program:
11/02 topics: Chapter 15: Event-driven Programming
Sections 15.1 and 15.2

Lecture slides: CSI32-lecture16.pdf

cs1graphics library: grab from

HW11 (due date: Wednesday, November 9th):

Write a program that draws any two objects filled with some color. When a user clicks on any of them - they change color. Under each object you should create a counter - that will be counting the number of clicks (clicks only) on this object.

Number of clicks: 10   Number of clicks: 6

Don't forget our Exit Button (for user to terminate the program).
Don't forget to close the graphics window at the end of the program.
10/31 topics:
Recursion (sections 11.3, 11.4)

Lecture slides: CSI32-lecture15.pdf,
Programs, data :,

HW10 (due date: Monday, November 7th):

Recall Fibonacci numbers/sequence recursive definition :
F(0)=1 (base case)
F(1)=1 (base case)
F(n)=F(n-1) + F(n-2) for all integers n>1

Write a program, that for a given non-negative integer n, displays nth Fibonacci number, and
prints all the Fibonacci function calls.

Here is a snapshot of the program's output on Fibonacci(4):
Please, input any non-negative integer:4
Calculating Fibonacci of 4
Calculating Fibonacci of 2
Calculating Fibonacci of 3
Calculating Fibonacci of 1
Calculating Fibonacci of 2
The 4 th Fibonacci number is 3

The sketch of a program is given in lecture slides.

Similar to example with Factorial Function (recursive definition) (slides 6 – 13 ) draw a figure of Fibonacci Function call on n = 5

Suggestion: it is probably worth doing the picture as a tree. See the figure in the lecture slides.
10/26 topics: Recursion (sections 11.1, 4.3)

Lecture slides: CSI32-lecture14.pdf,
Programs :,,,,

Suggested homework (not for grade):
page 391 / 11.5 or 11.6
10/24 topics:
Section 9.4

Lecture slides: CSI32-lecture13.pdf,

Description of Polygon class can be found here
review your midterm exams!
10/19 Midterm Exam  
10/17 topics:
Sections 9.1 Augmentation,
9.2 Specialization, and
9.3 When Should Inheritance (Not) Be Used

Lecture slides: CSI32-lecture12.pdf,
HW9 (due date: Monday, October 24th):

1) pages 327 - 328 / 9.1, 9.2, 9.3

2) Write the functions/methods/behaviors for subtraction, multiplication and negation of mixed numbers

take care of the negative mixed numbers (update all the behaviors to accommodate negative mixed numbers)

Hint: sign can be stored as a separate attribute (-   2   3/5),
or "attached" to the whole part (-2   3/5)
10/06 Thursday, but runs on Monday's schedule

topic: Hangman game

source files to be used:,,,,, dict.txt
no HW assignment was given, but if you are curious, there is a nice example of design and implementation of the MasterMind game in the textbook (Sections 7.1 - 7.3).

Links to videos about the game:
Online game:
10/05 topics:
Set Class (page 234, exercise 6.15)
BinaryNumber Class (page 235, exercise 6.18)

Lecture slides: CSI32-lecture10.pdf
HW8 (due date: Wednesday, October 12th) - change of due date to Monday, 10/17:

Improve our Binary Numbers class:
- fix __lt__ method or update the constructor,
- add __gt__ method,
- add XOR, AND, OR methods (work on strings of the same length)

Here is some info on bitstrings operations XOR, AND, and OR: bitOperations.pdf
10/03 no classes today

The detailed syllabus for our class is updated and posted. csi32syllabus-Fall2016Detailed.pdf

The Midterm Sample is updated to Midterm Sample questions. It has a list of questions that can be on the midterm exam, but not all of them will be on the Midterm Exam. midterm_CSI32sample.pdf
09/28 topic: Section 6.4 Fraction Class

Lecture slides: CSI32-lecture09.pdf,

Please note that there will be no classes on Monday, 10/03 and Tuesday, 10/04
In addition, Thursday, 10/06 will run on Monday's schedule.
HW7 (due date: Wednesday, October 5th):

1) Update/modify our fraction class to accommodate negative numbers

2) problem 6.1 on page 231 (has answer at the end of the book)

3) problem 6.4 on page 232
09/26 topics:
Rotating, Scaling, Flipping, and Cloning (Sections 3.3, 3.4) + drawing graphs of functions

Here you can find the description of classes from cs1graphics library:

Here is Chapter 3 from the book (Separately): ch03.pdf

Lecture slides: CSI32-lecture08.pdf,
suggested homework (due date: as soon as you can) not for grade, do not submit:
1) Use the figure you did in previous assignment. Perform the following actions:
- shrink it,
- stretch it,
- flip it,
- rotate it about some arbitrary point in the graphics window, which is not on/in the figure,
- make few copies of it, with different colors and dimensions (shrink, stretch, rotate...).

2) Draw a rectangular coordinate system, and using line segments (path) draw a "curve" of y=x2-10 for x = [-5, 5]
09/21 topics:
Section 3.1,
Section 3.2

Lecture slides: CSI32-lecture07.pdf,
Graphics library:
Programs:,,,,, tree.png
suggested homework (due date: as soon as you can) not for grade, do not submit:
Write a program that draws a composite object (i.e. not a parallelogram, triangle, or circle) in the middle of the graphics window, then moves this object to the right side of the graphics window, and then to the left side of the graphics window. Movement should be smooth (without jumps). During the movement object should change it's color at least 12 times.

Afterwards in order to notify the user that the program won't do anything else display a text, saying something like "That's it. No more tricks!".

Don't forget to close the graphics window. Use sleep() method !
09/19 topics:
Section 5.2 Functions
Section 5.4 Case Study: computing the square root
Sections 5.5 Error Checking and Exceptions

Lecture slides: CSI32-lecture06.pdf,

HW6 (due date: Wednesday, September 28th):
  1. page 200/ 5.30

  2. Write a program that finds a distance between two points (x1,y1) and (x2,y2), such that:
    - x1, x2, y1, y2 are integers,
    - x1,x2 ∈ [0,30],
    - y1,y2 ∈ [0,20].

    Do type checking (TypeError) and value checking (Value Error).
    You can raise errors or force the user to enter the correct values.
09/14 topics:
More examples of work with strings: Exercise 2.37 DNA mutation (pages 86-87)
Section 4.5 More about lists: List Comprehension
Section 2.6 Calling Functions
Section 2.7 Python Modules
Section 2.8 Expressions

Lecture slides: CSI32-lecture05.pdf,
HW5 (due date: Wednesday, September 21st):
  1. Exercise 4.35 on page 156

  2. Exercise 2.27 on page 85

  3. Draw an evaluation tree for the following statements:
        (a) 23 + 10 ( 22 * 3 - √64)
        (b) exercise 2.29 on page 85

  4. Do exercise 2.36 on page 86
09/12 topics:
Chapter 7 - Good Software Practices
Section 2.2 list Class (review)
Section 2.3 str and tupple
Section 2.4 Numeric types: int, float and long (review)
Section 2.5 Type Conversions

Lecture slides: CSI32-lecture04.pdf
HW4 (due date: Monday, September 19th):
1. page 84 / 2.24

2. Write a program that takes a string of words separated by a space from the user;
converts is to a list of strings, using whitespace as the separator;
then if an element of the list is a string of digits, the program converts it to a whole number (positive integers), and replaces the corresponding element of the list with this number.
The conversion should be done for all such elements (that are whole numbers, written as a strings).

Example of input-output:
Input (from the user): Hello buy mind 5 abc 12 0 1243 thank

Output: ['Hello','buy','mind',5,'abc',12,0,1243,'thank']

Your program must be well commented!
Overwise 0.5 points will be taken off

3. Write a program that takes a list of "words" as an input (from a user, from the keyboard; "words" are separated by commas), echoes it to the display/screen and then allows the user to remove all occurrences of an item provided by user from the list.

Comments: the user will provide something of this kind as an input: mother,hello,1823,buy,thank you,you are welcome.
Your program will take it as a string. In order to generate a list from it you can use method split.
Example: listWords=text.split(',')

Example of a program's run: - added on 10/05

Input a list of words, separated by commas, no spaces please: mother,hello,1823,buy,hello,thank,you,welcome,hello

Here is the list of words you entered: ["mother","hello", "1823","buy","hello","thank","you","welcome","hello"]

Type in the word to be removed: hello

Result: ["mother","1823","buy","thank","you","welcome"]
09/07 topics:
1.4 Object-Oriented Paradigm, and
few UML diagrams: activity, sequence and class diagrams.

Slides: CSI32-lecture03.pdf

Orientation Meeting for CS major students: tomorrow, Thursday, 09/08
12:00 pm - 1:50pm at Begrisch Hall 226.
More details: click here
HW3 (due date: Wednesday, September 14th):

page 29 / 1.19
page 30/ 1.20, 1.22, 1.27, 1.31

For 1.20: The book has an answer for 1.20, but try to do it on your own first, and then, if needed, refer to the solution. Please, note that we did not create Schedule class and hence didn't use it in our scenarios, so you shouldn't use it either.
- this comment was only in the lecture slides originally, and today (10/02) I am putting it here as well.
08/31 topics:
4.1 For Loops,
4.2 Case Study: DNA to RNA Transcription, and
5.1 While loops

Lecture slides: CSI32-lecture02.pdf

Please note that on Labor Day (Monday, 09/05) the college is closed. There will be no classes!
HW2 (due date: Monday, September 12th):

Exercise 1: Give a flowchart portraying the algorithm you wrote for problem 1.5 or 1.8,

page 155 / exercise 4.29

page 196 / exercise 5.6
08/29 Welcome to CSI 32!

topics :
1.1 Data and Types
1.2 Operations, Functions, and Algorithms, and
4.4 Conditional Statements

Lecture slides: CSI32-lecture01.pdf

HW1 (due date: Wednesday, September 7th):

1) Exercise 1: Implement both algorithms for finding gcd (it can be done as one program)

Input: from keyboard

Python operators you can use: a%b returns the remainder of the division ( in math: a mod b )

Modifications (not mandatory):
1. Input from a file
2. Ability to run the algorithms on as many input pairs as the user wants

2) Other exercises:
p. 28 № 1.4,
p. 29 № 1.5 or 1.8 (your choice), and
p.154 № 4.22