Schedule

Unless specified otherwise, weekly readings and media are due before class on:

The due date will be specified by T-date1/W-date2, where date-1 is for TTh classes and date-2 is for MWF classes. For example, by T-Sep 1/W-Sep 2 means that the reading is due before class on Sep 1 for TTh classes and before class on Sep 2 for MWF classes.

Weeks are listed newest first, so the current one is always at the top.

Week 12: Revisiting conditionals and learning more about loops

Due: by T-Nov 17/W-Nov 18

Estimated time to complete: 2.5–3.5 hours

From the class of Nov 12/13: The Patriot Missile Failure

Essentials

Dig Deeper

find_char_ind

Look back at the for-loop with break at the beginning of IPyP 5.4.

Create a function find_char_ind that takes a string and a character (i.e., a single-character string) as arguments and returns the index of the first occurrence of the character in the string, or -1 if the character does not appear in the string. Do not use break and do not use built-in string/list functions.

In the main program, read a string and a character from the user, call find_char_ind with them as arguments, and print the result (either Found at index: <index> or Not found).

Post your solution (or like a similar solution) on Discord, in the [W12] find_char_ind question.

Sequence multiplication

Write a program that reads numbers from the user until the user enters “done” and outputs the product of the numbers.

Consider the following questions:

  • What is the minimal number of user inputs to consider?
  • What type should be used for the product value?
  • What is the initial value of the product?
  • What should happen if the user enters a wrong input?

Post your thoughts and solution on Discord, in the [W12] sequence multiplication question.

Week 11: Revisiting program directory and using Python Tutor

Due: by T-Nov 10/W-Nov 11

Estimated time to complete: 1.5–2.5 hours

Essentials

  • Revisit the following:

  • Read DCIC 9.3.1 until (not including) the paragraph that starts with “Going back to our Runners”.

  • Go to pythontutor.com and do the following:

    • Copy and paste courses = ["CS111", "ENGN90", "VISA100"] to the code editor.

    • Select show strings and numbers as objects in the Python options box below the code editor.

    • Press Visualize Execution and step through this line of code.
      Note the correspondence between Prog Directory/Memory (DCIC) and Frame/Objects (Python Tutor).

    • Copy and paste the following program to the code editor and step through it as well.

      courses = ["CS111", "ENGN90", "VISA100"]
      courses_alias = courses
      courses_extended = courses + ["CMSC115"]
      
      c2 = courses[2]
      courses[2] = "assigned in courses"
      c2_upd = courses[2]
      c2 = "newly assigned to c2"
      
      courses_alias.append("WRIT103")
      courses.append("ENGL211")

      Do you understand what is going on here? If not, think about the program directory, and if needed, ask on Discord or in class.

    • Revisit DCIC 9.1.8.3 Using For Loops in Functions that Produce Lists.

    • Copy and paste the following program to Python Tutor and step through it.

      def all_z_words(wordlist : list) -> list:
          """produce list of words from the input that contain z"""
          zlist = [] # start with an empty list
          for wd in wordlist:
              if "z" in wd:
                  zlist = [wd] + zlist
          return(zlist)
      
      zwords = all_z_words(["abc", "xyz", "coala", "zebra"])
    • Revisit 3.3.2.1 How Functions Evaluate and 3.4.6.2 Function Composition and the Directory

    • Repeat the exercise with all_z_words and, if you are not sure what’s going on there, ask on Discord or in class.

  • Watch and/or read this talk from PyCon 2015: Facts and Myths about Python names and values by Ned Batchelder.

Dig deeper

Define a function add_chars that takes a list of strings and returns a list of strings that has all the original elements in the same order, as well as all single-character strings repeated at the end of the list. For example, add_chars(["a", "bc", "d", "cat"]) should return ["a", "bc", "d", "cat", "a", "d"].

Implement two versions of this function:

  1. add_chars that does not mutate the input list and returns a new list.
  2. add_chars_in_place that extends the input list (you can use extend method on lists, e.g., lst1.extend(lst2)) and returns it.

Write a program that uses both add_chars and add_chars_in_place functions and demonstrates the difference in their behavior. Run the program and then step through it in Python Tutor to check your understanding.

When done, post your solution (or the part of it that is different from previously posted solutions) on Discord in [W11] add_chars question and like previous posts with the same solution.
Additionally, if you have encountered any surprises when working on this problem, post them on Discord in the same question discussion.

Week 10: Python basics

Due: by T-Nov 5/W-Nov 6

Estimated time to complete: 1.5–2.5 hours

Make effort to complete some exercises in the book, especially for the concepts that are new to you, for example, conversions between numeric types.

  • IPyP Chapter 1
    Note. There are helpful videos embedded in the text, under Checkpoint.

    Many concepts here should be familiar to you, but there are some new concepts (such as input/output) and Python specifics, such as numeric types and error messages.

    • Python does not have a type that’s analogous to Pyret’s Number; int and float values are all within Pyret’s Number.
    • Python’s int is a subset of Pyret’s Exactnum.
    • Numbers that are typically represented in Python as float fall under either Pyret’s Roughnum or Exactnum types, depending on the number.

    Pay particular attention to:

  • IPyP Chapter 2
    Pay particular attention to IPyP 2.4 Floating-point errors.

  • Sections 3.1 and 3.2 from IPyP Chapter 3

Write a program that does the following:

  • asks the user for a radius of a circle,

  • computes the area of the circle (this computation should be written in a function), and

  • prints the area of the circle in some nice format, for example:

    The area of a circle with radius 2.5 is 19.63.

Week 9: Getting started with mutation and Python

Due: by T-Oct 27/W-Oct 28

Estimated time to complete: 1.5–2.5 hours

Essentials

Dig Deeper

Week 8: Structured data

Due: by T-Oct 20/W-Oct 21

Estimated time to complete: 60–100 min

First, read about the destruction of Mariner 1 and think about the importance of testing the code.

Essentials

Before you do the readings this week, revisit the guess-what-this-program-does activity from class. Run the code below, read it, experiment with it. Try to guess what is happening there. (The documentation and test cases are intentionally omitted.)

fun two-stripe-flag-vert(
    flag-width :: NumNonNegative, flag-height :: NumNonNegative,
    color-left :: String, color-right :: String)
  -> Image:
  stripe-width = flag-width / 2
  beside(
    rectangle(stripe-width, flag-height, "solid", color-left),
    rectangle(stripe-width, flag-height, "solid", color-right)
    )
end

two-stripe-flag-vert(100, 60, "red", "green")

fun two-stripe-flag-horz(
    flag-width :: NumNonNegative, flag-height :: NumNonNegative,
    color-top :: String, color-bot :: String)
  -> Image:
  stripe-height = flag-height / 2
  above(
    rectangle(flag-width, stripe-height, "solid", color-top),
    rectangle(flag-width, stripe-height, "solid", color-bot)
    )
end

two-stripe-flag-horz(100, 60, "blue", "yellow")


data FlagSize: size(width :: NumNonNegative, height :: NumNonNegative) end

flg-sz = size(100, 60)
flg-sz

data StripeOrientation:
  | Vertical
  | Horizontal
end

data FlagStructure:
  | solid(color :: String)
  | two-stripes(orient :: StripeOrientation, color1 :: String, color2 :: String)
end

flg-ukr-struct = two-stripes(Horizontal, "blue", "yellow")
flg-ukr-struct

fun flag-has-color(flag-struct :: FlagStructure, color :: String) -> Boolean:
  cases (FlagStructure) flag-struct:
    | solid(clr) => color == clr
    | two-stripes(_, clr1, clr2) => 
      (color == clr1) or (color == clr2)
  end
end

flag-has-color(flg-ukr-struct, "red")
flag-has-color(flg-ukr-struct, "blue")


fun flag-image(flag-size :: FlagSize, flag-struct :: FlagStructure) -> Image:
  cases (FlagStructure) flag-struct:
    | solid(color) => 
      rectangle(flag-size.width, flag-size.height, "solid", color)
    | two-stripes(orient, color1, color2) =>
      flag-function = 
        cases (StripeOrientation) orient:
          | Vertical => two-stripe-flag-vert
          | Horizontal => two-stripe-flag-horz
        end
      flag-function(flag-size.width, flag-size.height, color1, color2)
  end
end

flag-ukraine = flag-image(flg-sz, flg-ukr-struct)
flag-ukraine-small = flag-image(size(10, 6), flg-ukr-struct)

flag-ukraine
flag-ukraine-small

Read and practice Do Now! and Exercise from the following DCIC sections:

Dig Deeper

Read and practice Do Now! and Exercise from the following DCIC sections:

Week 7: More lists

Due: by Oct 14 (missing Oct 13 due to the Fall break)

Estimated time to complete: 40–80 minutes

This week is light on the new material due to the Fall break. Use this opportunity to review previous materials if needed.

NEW: Starting this week, both class prep and assignments will have two sections, Essentials and Dig Deeper. Doing Essentials should be enough to understand the core material and earn a solid B grade. There might also be an Optional section, which is not required and not assessed for credit.

Remember to reflect on your understanding of the material as you are working through the prep materials.

Essentials

Read and practice Do Now! and Exercise from the following DCIC sections:

Complete or revisit the following in-class programming activities:

  • Count positive.

    Define a function count-pos that takes a list of numbers and returns the number of positive elements in the list. Do not use any built-in list functions except for link, empty, .first, and .rest.

  • Apply twice.

    • In this DCIC section, find a paragraph that starts with “The main difference between our original expression” and look at the type signature of the filter function.
    • Using filter as an example, create a template of a function named app-twice, which takes two arguments (function f that takes a number and returns a number, and a number x) and returns a number. “Template” means you can write ... in place of the function implementation. Make sure your current program is accepted by Pyret.
    • Terminology alert: applying function g to argument a is the same as calling function g with argument a.
    • Implement app-twice: it should return the result of applying f to the result of applying f to x; that is, it should apply f to x twice. For example, app-twice(num-sqr, 2) should return 16.

Dig Deeper

Read and practice Do Now! and Exercise from DCIC Section 5.2.8 Structural Problems with Accumulators.

Complete or revisit the following in-class programming activity: my map.

  • Define a function my-map-str-int that takes a function fn (which takes a string and returns a number) and a list of strings, and returns a list of numbers, where each number in the output list is the result of applying fn to the corresponding string in the input list.
  • Using my-map-str-int, find the list of lengths of strings in the list [list: "a", "cat", "isn't", "", "a", "dog"].
  • Now find the list of lengths using only built-in Pyret functions.
  • Define and a test a function my-map with two type variables, F and T (for “from” and “to”). It should be similar to my-map-str-int, but work with types F and T instead of being specific to strings and numbers.

Optional

Read and practice Do Now! and Exercise from DCIC Section 5.2.9 Dealing with Multiple Answers.

Using the idea of accumulators and using a list to represent multiple values, implement my-avg-single-pass that computes the average of a list of numbers in a single pass, that is, without traversing the list twice to compute the sum and length separately.

If you want, submit your solution here.

Week 6: Lists continued

Due: by T-Oct 6/W-Oct 7

Estimated time to complete: 1.5–2.5 hours

  • Revisit the Python vs Pyret activity:

    • Work through todo items in sign-text.py.
    • For the Pyret version of the same program, try to make the same mistakes as in Python.
      Click here for a hint To mimic the no-return mistake, turn n > 0 into a name definition.
      Submit your observations into the class journal.
  • Read Section 5.2 from DCIC until section 5.2.6 (not including it) and complete the built-in exercises and activities.
    Note: this week’s topic is a lot more complex than everything we have done so far, so be prepared to think carefully and re-read the material if something does not click.

    • Practice reflecting on your understanding of the material. If something in the prep materials feels shaky, pause, make a note, and try to turn that into a question. Does a particular word seem unfamiliar? Are you not sure what the phrase refers to? Do you not see why the book says a certain expression is equivalent to another? Write the question down. Think about it briefly. If you figured it out, great! If not, even better—post the question on Discord. Do this exercise at least once.

    • When you get to the implementation of my-len in 5.2.3.3 From Examples to Code, stare at it for a moment. Is there anything unusual about it?

      Click here for a hint

      In the past, we wrote functions that would call other functions, but what is happening here?

      Submit your observations into the class journal.

    • When you get to the end of 5.2.4.1 my-doubles: Examples and Code, pause. Think back to the previous week’s materials and assignments. How would you have implemented my-doubles using only the material from last week?

      Click here for a hint

      Consider the assignment task of defining list-sqrts.

      Now that you’ve seen the new material, how do you think the function you used last week might be implemented? Submit your thoughts into the class journal.

Week 5: Introduction to lists and review

Due: by T-Sep 29/W-Sep 30

Estimated time to complete: 1 hour for the new material, plus additional time for review as needed.

This week is light on new material due to the upcoming progress exam. The exam will not have any questions on lists.

  • Review previous assignments and quizzes; sample solution and quiz answers are available on Discord.
  • Think about the following concepts (and if you are not sure about them, review previous chapters of DCIC as needed):
    • values
    • expressions
    • evaluation of expressions
    • statements (so far we have seen only one example of a statement, which is a name definition)
    • names
    • functions
    • examples (test cases)
    • type annotations
    • evaluation of functions
    • conditional (if-expression)
    • evaluation of conditionals
    • tables

For now, we will skip Section 4.2 on processing tables. When doing the book exercises, when you need to work with a table, use the code snippet from here:

include gdrive-sheets
include data-source

ssid = "1Ks4ll5_8wyYK1zyXMm_21KORhagSMZ59dcr7i3qY6T4"
cleaned-data =
  load-table: name, email, tickcount, discount, delivery, zip
    source: load-spreadsheet(ssid).sheet-by-name("Cleaned", true)
    sanitize name using string-sanitizer
    sanitize email using string-sanitizer
    sanitize tickcount using num-sanitizer
    sanitize discount using string-sanitizer
    sanitize delivery using string-sanitizer
    sanitize zip using string-sanitizer
  end

Look at the value of cleaned-data to understand its structure before proceeding with the prep materials.

Read Section 5.1 from DCIC and complete the built-in exercises and activities.

Week 4: Review previous materials

There are no new prep materials this week. Use this time to review previous materials and catch up.

Week 3: Introduction to tabular data

Due: by T-Sep 15/W-Sep 16

Estimated time to complete: 1.5–2 hours

Read DCIC Section 4.1, paying careful attention to and completing built-in exercises and activities.

Week 2: Functions and conditionals

Due: by T-Sep 8/W-Sep 9

Estimated time to complete: 1–1.5 hours

Week 1: Setup and first programs

Due: by T-Sep 1/W-Sep 2

Estimated time to complete: 2.5 hours

Note: if you are running behind and need to prioritize, start with the video and DCIC Section 3—with at least some exercises.

Week 0: Intro to the Course

Due: by T-Aug 27/W-Aug 26

Read the syllabus.