Here is a general outline of the key concepts (arranged by topic) that you should know for Exam 02.

The exam will have the following format:

  1. Python Scripting (multiple-choice, fill-in-the-blank)

  2. Data Processing (multiple-choice)

  3. Functional Programming (multiple-choice)

  4. List Comprehensions (multiple-choice)

  5. Generators (multiple-choice)

  6. Concurrency and Parallelism (multiple-choice)

  7. Translations: Convert Unix pipelines to Python code (coding on the computer)

Parts 1 through 6 are to be done first on paper. Once that portion is completed and submitted, you will do part 7 on your own computer and submit code to your assignments repository.

Representative, but not Exhaustive

This check list is meant to be representative, rather than exhaustive (ie. there may be questions that show up on the exam that are not shown below).


Logistics

Exam 02 will take place during our normal lecture session on Wednesday, March 20 from 12:50 PM - 1:40 PM in 102 DeBartolo Hall.

As noted above, the first portion of the exam with be on paper, while the remaining component with require coding and submitting work to your assignments GitHub repository.

During the paper portion of the exam, students will only be allowed to use one cheatsheet. This paper component of the exam must be submitted before students can access the coding section.

For the coding portion, students will use their own computer, and thus are permitted access to any material in their textbooks, notes, assignments, and the Internet.

Honor Code Violations

Although students are allowed to use their computers and the Internet during the coding portion of the exam, students may not communicate with others or solicit answers from others. Nor may they use AI tools such as ChatGPT or Co-Pilot. Students caught sharing solutions or violating any portion of the honor code will receive a zero on the exam.

Furthermore, students are not allowed to consult, copy, or share material from previous sections of this course.


Python Scripting / Data Processing

Concepts

  1. How is Python different from the Bourne shell? How is it similar?

  2. How do we manage control flow in Python? How do we utilize these constructs?

    • Conditionals

    • Loops

    • Exceptions

    • Functions

  3. What data structures do we have in Python? What are their basic operations? When would we want to use each type of data structure?

    • Lists / Tuples

    • Dictionaries / Sets

  4. How do we do the following in Python?

    • Process command-line arguments

    • Read standard input (line-by-line)

    • Read input from files

    • Access environment variables

    • Change the case of a string

    • Split a string, combine a list of strings

    • Slice a list

    • Execute an external command

    • Process JSON data from the web

    • Count with dictionaries

    • Sort lists and dictionaries

Sample Questions

Code Snippets

Write Python code snippets to perform the following tasks (don't worry about she-bangs or imports):

  1. Print out each element of a list (one element per line).

  2. Print the contents of stdin (line-by-line).

  3. Print the first 10 items of a list.

  4. Print the length of a list.

  5. Print the largest and smallest elements of a list.

  6. Print the first and last words in the string "Let this promise in me start, Like an anthem in my heart".

  7. Print the results of the ps aux command.

  8. Print the contents of a specific webpage.

Code Evaluation

Given the following data:

comics = {
    "marvel": ["deadpool", "spider-man", "wolverine"],
    "dc"    : ["batman", "superman"]
}

For each of the following expressions, state the output value:

  1. len(comics)

  2. list(comics.keys())[0]

  3. list(comics.values())[-1]

  4. comics['marvel'][1]

  5. comics['dc'][-1][-3:]

Translation

Given the following Unix pipelines, write Python code that accomplishes the same task.

Note: No credit will be given for simply calling os.system on the given pipeline. You may use os.popen to read the output of one command, but you may not use a pipeline.

  1. cat /etc/passwd | cut -d : -f 1 | grep d$ | wc -l

  2. cat /etc/passwd | cut -d : -f 3 | grep -E '^[0-9]{2}$' | sort | uniq

  3. curl -sL http://yld.me/raw/lmz | cut -d , -f 1 | grep -Eo '^[^jfs].*'

  4. curl -sL http://yld.me/raw/lmz | cut -d , -f 2 | grep -Eo '^B.*' | sort

  5. who | sed -rn 's|.*\((.*)\).*|\1|p' | sort | uniq

  6. ls -l /etc | awk '{print $2}' | sort | uniq -c

Functional Programming

Concepts

  1. What is the difference between imperative and functional programming?

  2. How do we use map, filter, and lambda to do functional programming in Python?

List Comprehensions

Concepts

  1. How do we use list comprehensions in Python?

Generators

Concepts

  1. What is an iterator and how is it different from a list?

  2. What is a generator and how it is different from a list?

  3. How do we use common iterators and generators such as:

Sample Questions

Translate the following imperative code:

def find_squares(start, end):
    squares = []
    for number in range(start, end):
        if math.sqrt(number) == int(math.sqrt(number)):
            squares.append(number)
    return squares
  1. To using map, filter, and lambda.
  2. To using a list comprehension.
  3. To using a generator with yield.
  4. To using a generator expression.

Concurrency and Parallelism

Concepts

  1. What is the difference between concurrency and parallelism?

Sample Questions

Identify whether the statements below are True or False and explain why.

  1. The bench.py class example exhibits data parallelism.
  2. The bench.py class example exhibits task parallelism.
  3. The bench.py class example is embarrassingly parallel.