CSci 227 Syllabus    Resources    Coursework



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


Learning Objectives: Students will be able to:

Software Tools Needed: Web browser to access textbook and an IDE (on-line or on own machine) 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.


Submitting & Style-Checking Tools

To start this week's lab, we will write the canonical Hello World program and successfully pass the style and correctness tests on Gradescope.

Linting

Good style accounts for 30% of the program grade. We are following the standard PEP 8 style guide for Python code. As part of the autograder scripts, your program is run through a static code analyser (aka a "linter").

Let's write the first program (see Program 1) which asks that you print:

Hello, World!
to the screen.

Open up your favorite IDE, and write the line of code that will print the message. Run your program to make sure it produces the correct output.

Next, let's check how the program does for style by running pylint:

We ran the pylint program from the command line, but you can also invoke it inside most IDE's. The message says we are missing the documentation string (e.g. the introductory comment) from the start of the file.

Add a multi-line string comment that includes the information required by the autograder: your name, email, and resources used. For example, for the student, Thomas Hunter, the opening comment of his first program might be:


  """
  Name:  Thomas Hunter
  Email: thomas.hunter1870@hunter.cuny.edu
  Resources:  Used python.org as a reminder of Python 3 print statements.
  """
  

When we run pylint again, the message says the code is rated a perfect score (10.0/10).

Now that we have tested it both for correctness and good style, we are ready to submit to Gradescope.

Gradescope

We will be using the Gradescope platform for submitting programs. An account for this course will be set up automatically for all students registered on 24 January (check your spam folder, since myhunter email often moves it there). If you registered after that date, this course is not appearing in your Gradescope course list, or you would like to use a different email address (we use the one in Blackboard), send email to csci227@hunter.cuny.edu with your full name, EmplID, and preferred email and access will be added manually.

To submit the first program:

Using LeetCode & HackerRank

A course goal is mastery of core data structures and algorithms (ds&a) so that students can successfully complete technical challenges at the summer internship skills level. Throughout the semester, we will use two industry standards, LeetCode and HackerRank, as sources for problems in classwork and for coding reviews.

HackerRank

If you didn't do so in lecture, work through the first 7 challenges from HackerRank: Prepare Python that recap Python learned in CSCI 127:

list of first 7 HR Python Challenges

LeetCode

We didn't have a chance to introduce LeetCode in lecture, but it is similar to HackerRank in that it has a bank of questions to prepare (and often used) in technical interviews. HackerRank is primarily used for interviewing and has fewer questions to practice. LeetCode has thousands of questions for practice. When trying them, make sure to choose Python 3 as the language. Here's a couple to try now:

Note that the template in LeetCode, for both of these challenges, uses annotations, or hints as to what the types of the parameters and return values are. For example, for Fizz Buzz:

the function has been set up inside a class. We will talk more about setting up classes in a few weeks, but for now, we will ignore the first argument (self) to the function, and look at the second (n). It is annotated with int which means that its expected type is an integer. The end of the line has the annotation, List[str], which means that the expected return type of the function is a list of strings.

Collections Data Types

Next, work through the following section of the textbook:

Before going on, work through the outline and focus questions on collection data types.

We will focus today dictionaries. Dictionaries, often referred to as lookup tables, maps, hashmaps, or associative arrays, are incredibly useful for efficient storage and retrieval.

Let's use a dictionary to approach the design challenge from classwork in Lecture 1:

Are there two students with the same first name?

Let's use a dictionary to store how many times we have seen a name. For example, if we have a list of names, names, we can loop through the name list. If it's the first time the name has occurred, we can add it to our dictionary. If it has already occurred, we can up the count. Let's first write the pseudocode:

  1. Initialize the dictionary: seen = {}.
  2. For each name in names:
  3.     if name is in the seen dictionary:
  4.         seen[name] += 1
  5.     else:
  6.         seen[name] = 1

When this loop finishes, the dictionary, seen, will contain all the names and the number of times they have occurred.

Next, think about the following:

When you have solved the above, see Program 3.

Quizzes and Code Reviews

There are weekly written quizzes and code reviews in 1001G. Make sure to sign up for Quiz 1 and Code Review 1 which need to be completed by Thursday, 8 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 from LeetCode where using dictionaries is an efficient approach: