CSci 227 Syllabus    Resources    Coursework



Laboratory Exercise 2
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.


String Methods

To start this week's lab, work through the textbook's Chapter 1 which recaps string methods. Test yourself on the interactive code examples.

If you're a bit rusty on string methods, you can also work through the introductory Python textbook's sections (Chapter 9) on strings.

Try these two LeetCode challenges on strings. Work through as much as possible before looking at the hints:

Let's work through the first one:

String Formatting

Python has several way to format strings. In lecture, we briefly covered one way (f-strings). If you didn't work through the textbook's overview of the ways to format strings (in particular the f-strings towards the end of the section are very useful), do so now:

Test your understanding by completing the interactive coding exercises embedded in the textbook chapter.

List Comprehensions and Zips

Python has some nice tools for lists. Work through the quick tutorials on list comprehensions and zips below:

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.

Work through the chapter on File I/0 from the CSCI 127 textbook:

There's a way in Python to combine the opening and closing of a file into a single step, with a with, covered in Section 11.7 above. This approach is very popular, since it handles the "bookkeeping" of accessing files.

For classwork, we sketched an algorithm for double spacing a file. Using that same approach, sketch algorithms to:

  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).

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 2 and Code Review 2 by Thursday, 15 February.

Additional Practice

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

HackerRank: Prepare Python

Work through the challenges on Lists and List Comprehensions.

For more practice on using string methods to solve problems, here are some popular ones from LeetCode where using dictionaries is an efficient approach: