Setup

City University of Hong Kong

CS1302 Introduction to Computer Programming


Course Materials

How to access the programming environment?

The course's programming environment is conveniently accessible via a JupyterHub server, allowing remote access without the need to install any special software, thanks to Project Jupyter. This means you can easily write and run programs on your mobile devices using just a web browser.

Open the url of the JupyterHub server given in the course homepage. The first time you access the url, it opens a login page as shown in Figure 1:

  1. Enter your EID and Password in the fields Username and Password respectively.
  2. Click the Sign in button.
Login box

Figure 1:Login box

To access the Jupyter environment, simply log into the JupyterHub server and select the Default option from the list of available Jupyter servers. Click Start to begin your session. If the server is spawned successfully, the JupyterLab interface should appear. Each student will have their own Jupyter server, providing individual computer resources such as CPU and memory.

How to access the course materials?

Course materials are written in the form of Jupyter Notebooks. Jupyter Notebooks provide an interactive literate programming experience, which is a significant benefit for learners:

  • Jupyter Notebooks allow programs to be mixed with other contents, including figures, videos, and formatted textual explanations.
  • The notebooks can also run in a Jupyter server, which enables users to write and run their programs while adding formatted notes to it.

The recommended way is to follow the links to the notebooks on the course homepage as shown in Figure 2.

The notebooks reside in a GitHub repository. The first time you access the notebook link, the following steps occur:

  • Your Jupyter server is opened.
  • The repository is cloned to the server as a local repository.
  • The desired notebook is opened in the JupyterLab interface.

Afterwards, accessing a notebook link again will

  • git-pulls new or existing files in the repository and
  • merge them with the notebooks stored under the course folder cs1302_xxx in your home directory without overwriting your changes.

Lab Assignments

How to complete a lab assignment?

The notebooks can be edited in JupyterLab to include your notes and answers for submission. If this is your first time using JupyterLab, take a look at the official video tutorial:

Official video tutorial on Jupyter

The Help menu gives more detailed references. For instance:

  • Checkout the menu item Show Keyboard Shortcuts and try some of the shortcuts to see their effect. Jupyter Reference opens the user guide in a new tab.
  • Try the MyST Markdown syntax to format your notes.

In learning a new programming language, the first program to write is often the "Hello, World!" program, which says Hello to the world. As your first lab exercise, you will write such a program in python.

The following code cell is a solution cell. Since you are not expected to know python before, you can simply expand the hint above and copy the solution to the solution cell instead.

def say_hello():
    # YOUR CODE HERE
    raise NotImplementedError()
    
say_hello()

To test your program:

  • Run your program by selecting the solution cell and press Shift+Enter.
  • Then, run the following visible test to check whether your program prints the correct message.
# Run this test cell right after running your "Hello, World!" program.
import io
import sys

old_stdout, sys.stdout = sys.stdout, io.StringIO()
say_hello()
printed = sys.stdout.getvalue()
sys.stdout = old_stdout
assert printed == "Hello, World!\n"

The test returns an assertion error if your program does not print the correct message.

How to submit a notebook

You normally have at least 5 days to work on the lab after your lab session.

  • You can check the due dates of all the labs from the course homepage.
  • You may seek help from us or your classmates. However, you must write your own solution and indicate who your collaborators are by including their EIDs in a code cell:
    COLLABORATORS = ["xfwong2", "mklee3"]

Nbgrader is the tool used for grading notebook assignments. It is well-integrated into the JupyterLab interface, allowing students to submit their notebooks directly through the JupyterHub server. Submitted notebooks can be both auto-graded with pre-defined test cases and manually graded with custom feedback. After grading is complete, you can check your scores and access the feedback using the same interface.

Before you submit, make sure everything runs as expected:

  1. Git-pull the notebooks (optional): Follow any one of the link on the course homepage to a notebook, which will git-pull any updates/corrections to (all) your notebooks.
  2. Save the notebook: Unsaved changes are not exported, even though they are in the memory.
  3. Restart the kernel: Kernel\to Restart Kernel... to have a clean state before running visible tests.
  4. run all cells: Run\to Run All Cells to double check the results of the visible tests are as expected.

To submit your notebook:

  1. Select the menu item Nbgrader\toAssignment List.
  2. Expand the Lab folder and click the validate button next to the notebook(s) to check if all the visible tests pass.
  3. Click Submit to submit your notebook.