CSci 227 Syllabus    Resources    Coursework



Laboratory Exercise 3
CSci 227: Programming Methods
Department of Computer Science
Hunter College, City University of New York
Spring 2024


Learning Objectives:

Software Tools Needed: Web browser to access textbook and an IDE (on-line or on computer) with core Python 3.6+ loaded.

Lab Notes: Download the outline and focus questions to guide you while working through this lab. These are a useful tool for note taking and as well as studying for the quizzes and final exam.


File I/O

Last week, we introduced file I/O. Working with files follows a similar format in most programming languages:

  1. Open the file and create a file descriptor variable that keeps track of where you are in the file.
  2. Read or write to the file (via the file descriptor).
  3. When done, close the file.

If you didn't finish last week, work through the chapter on File I/0 from the CSCI 127 textbook:

For today's lab, take your sketches of algorithms from last week and turn them into Python programs:

  1. Copy the first letter of each line of the old file to the new file.
  2. Copy the first word of each line of the old file to the new file.
  3. Copy the longest word of each line of the old file to the new file (see Program 6).

Running Time Complexity

In Lecture, we highlighted Chapter 2 (algorithm analysis) of the textbook. For this lab, we're focusing on first sections of the chapter:

  1. Sections 2.1 & 2.2: Benchmarking Programs
  2. Sections 2.3: Big O Notation
  3. Sections 2.4: Anagram Detection

1. Benchmarking Programs

Work through the interactive examples in Sections 2.1 & 2.2: Benchmarking Programs. As you work through, make sure to note how you can measure the time a program takes to execute (the chapter has highlights two different approaches: time library in this section and timeit library later in the chapter in Section 2.5).

2. Big O Notation

Sections 2.3 introduces Big O Notation. We sketched the main ideas in lecture. Reread the section now and then test your understanding by completing Problems 1-5 of the Chapter 2 Exercises.

3. Anagrams

For Classwork 3, we sketched algorithms for anagram detection. Sections 2.4 gives several different solutions and analyses the running time. Work through the chapter, and then try Program 9.

Quizzes and Code Reviews

There are weekly written quizzes and code reviews in 1001G, 11:30-5:30, Mondays-Fridays when classes are in session. No appointments are needed for CSCI 227 students. Make sure to complete Quiz 3 and Code Review 3 by Thursday, 22 February.

Additional Practice

For additional practice on core Python, see the HackerRank prepare series:

HackerRank: Prepare Python

Click the Easy option on the right hand menu, and work through their Python challenges.

For more practice on using dictionaries to solve problems, here are some popular ones on anagrams and using dictionaries as an efficient approach: