Tutorial outline

Unit 1

  1. 01Getting started
  2. 02Names and functions
  3. 03Control
  4. 04Higher-order functions
  5. 05Environments and closures
  6. 06Functional abstraction
  7. 07Recursion
  8. 08Tree recursion
  1. D0Getting started
  2. D1Control and environment diagrams
  3. HW1Functions and control
  4. D2Higher-order functions
  5. HW2Higher-order functions
  6. P1Hog
  7. D3Recursion
  8. D4Tree recursion
  9. HW3Recursion

Unit 2

  1. 09Sequences
  2. 10Dictionaries, sets, and tuples
  3. 11Optionals
  4. 12Structures and enumerations
  5. 13Linked lists and trees
  6. 14Mutation and value semantics
  7. 15Classes and inheritance
  8. 16Protocols and generics
  9. 17Iterators and lazy sequences
  10. 18Efficiency and memoization
  1. HW4Sequences and containers
  2. P2Cats
  3. D5Trees
  4. HW5Recursive data and mutation
  5. D6Objects and classes
  6. HW6Classes and interfaces
  7. P3Ants vs. SomeBees
  8. D7Iterators and efficiency
  9. HW7Lazy sequences and efficiency

Unit 3

  1. 19Algebraic data types and pattern matching
  2. 20Writing an interpreter
  3. 21Error handling
  4. 22Concurrency with async and await
  5. 23Testing
  6. 24Building a Swift package
  1. D8Algebraic data types
  2. HW8Pattern matching
  3. D9Interpreters
  4. HW9Interpreters and errors
  5. P4Scheme
  6. D10Concurrency
  7. D11Testing
  8. HW10Concurrency and testing

Unit 4

  1. 25References, identity, and memory
  2. 26Drawing with SwiftUI
  3. 27Testing data structures
  4. 28IntLists and SLLists
  5. 29Doubly linked lists and deques
  6. 30Arrays from scratch
  7. 31Interface and implementation inheritance
  8. 32Iteration, equality, and ordering
  9. 33Copy-on-write collections
  1. D12Swift review
  2. D13References, classes, and arrays
  3. HW11Swift warm-up
  4. P5Particle Simulator
  5. D14IntLists and SLLists
  6. HW12IntLists
  7. P6LinkedListDeque
  8. D15DLLists, default implementations, and array lists
  9. P7ArrayDeque
  10. D16Comparables and iterators
  11. HW13Review I

Unit 5

  1. 34Asymptotic analysis
  2. 35Disjoint sets
  3. 36Analyzing recursion
  4. 37Amortized analysis and the limits of big O
  5. 38Sets, maps, and binary search trees
  6. 39B-trees
  7. 40Left-leaning red-black trees
  8. 41Heaps and priority queues
  1. D17Asymptotics and disjoint sets
  2. P8Percolation
  3. D18Asymptotics II
  4. HW14Asymptotic analysis
  5. D19BSTs, B-trees, and LLRBs
  6. HW15BSTMap

Unit 6

  1. 42Data-driven apps with SwiftUI
  2. 43Tree and graph traversals
  3. 44Graph implementations
  4. 45Shortest paths: Dijkstra and A*
  5. 46Minimum spanning trees
  6. 47DAGs, topological sort, and reductions
  1. D20Tree traversals and graphs
  2. P9NGordNet
  3. D21Shortest paths and MSTs

Unit 7

  1. 48Hash tables
  2. 49Hash functions
  3. 50Tries and prefix operations
  4. 51Software engineering: managing complexity
  5. 52Games with SwiftUI
  6. 53Designing a world
  7. 54Basic sorts
  8. 55Quicksort and selection
  9. 56The limits of sorting
  10. 57Radix sorts
  11. 58Compression and complexity
  12. 59Computing, society, and what comes next
  1. D22Hashing and tries
  2. HW16Review II
  3. D23Hashing and tries, revisited
  4. P10Build Your Own World
  5. D24Sorting
  6. HW17HashMap
  7. D25Sorting II
  8. HW18LLRB set
  9. HW19Final review

Sequences

Arrays, ranges, and strings as sequences: length, element selection, membership, slicing, and the processing patterns that work on all of them.

CS61A
Sequences · Composing Programs 2.1–2.3
Documentation
Array · String

Unit 1 was about functions. Unit 2 is about data: how to put many values together, and how to build new kinds of values of your own. It starts with the most common way to combine values, putting them in order.

A sequence is an ordered collection of values. CS61A describes the sequence abstraction by what you can do with one, not by how it is stored:

  • Length: how many elements it has.
  • Element selection: get the element at a position.
  • Membership: ask whether a value is in it.
  • Slicing: take a contiguous part of it.

Arrays, ranges, and strings are all sequences in Swift, and all of them support these operations.

Arrays

An array holds values of one type, in order. [Int] is the type of an array of integers:

let primes = [2, 3, 5, 7, 11]
print(primes.count)          // 5, the length
print(primes[0], primes[4])  // 2 11, element selection
print(primes.contains(9))    // false, membership
print(primes.first!, primes.isEmpty)   // 2 false

Positions start at 0, and asking for a position that does not exist stops the program, as Lesson 1 showed. + joins two arrays into a new one:

print([1, 2] + [3])   // [1, 2, 3]

An array bound with var can grow and change:

var numbers = [3, 1]
numbers.append(4)          // [3, 1, 4]
numbers.insert(0, at: 0)   // [0, 3, 1, 4]
numbers += [5]             // [0, 3, 1, 4, 5]
print(numbers, numbers.firstIndex(of: 4)!)   // [0, 3, 1, 4, 5] 3

The exclamation mark after first and firstIndex(of:) is there because both might have no answer: an empty array has no first element. Lesson 11 is about how Swift represents “no answer”.

Iterating

A for-in loop visits the elements in order. enumerated() gives each element with its position, and zip walks two sequences side by side:

for (index, name) in ["Ada", "Grace"].enumerated() {
    print(index, name)
}
for (number, letter) in zip([1, 2, 3], ["a", "b", "c"]) {
    print(number, letter)
}

Slicing

A slice is a contiguous part of a sequence, written with a range:

let values = [10, 20, 30, 40]
print(values[1...2])   // [20, 30]
print(values[..<2])    // [10, 20]
print(values.prefix(2), values.suffix(1), values.dropFirst())

Swift’s slices come with a surprise. A slice of an array is an ArraySlice, a view of the original array that keeps the original positions. It does not renumber from zero.

What would Swift print?

let values = [10, 20, 30, 40]
let middle = values[1...2]
print(middle[1], middle.startIndex)

Type Error if running it would crash. For several lines of output, put each on its own line.

Show answer and explanation

Answer20 1

middle holds 20 and 30 at their original positions, 1 and 2. So middle[1] is 20, not 30, and its first valid index is 1. Wrap it in Array(middle) to get a fresh array numbered from 0.

Strings

A string is a sequence of Characters. It has a length, membership, and iteration like any sequence:

let word = "hello"
print(word.count, word.first!, word.contains("e"))   // 5 h true
for letter in word {
    print(letter, terminator: "-")
}
print()

What a string does not allow is selecting a character by an integer.

What would Swift print?

let word = "hello"
print(word[0])

Type Error if running it would crash. For several lines of output, put each on its own line.

Show answer and explanation

AnswerError

Swift rejects word[0] at compile time: “cannot subscript String with an Int”. Characters can take different amounts of memory (think of an emoji or an accented letter), so jumping to position k is not a constant-time operation, and Swift will not pretend it is. Use word.first, word.prefix(3), or convert with Array(word) when you need positions.

reversed() gives the characters in reverse order; String(...) turns them back into a string:

print(String("stressed".reversed()))   // desserts

Splitting and joining

split(separator:) breaks a string into parts, and joined(separator:) glues an array of strings back together:

let sentence = "the quick brown fox"
let words = sentence.split(separator: " ").map(String.init)
print(words)                          // ["the", "quick", "brown", "fox"]
print(words.joined(separator: "-"))   // the-quick-brown-fox

split returns Substrings, lightweight views into the original string; map(String.init) turns each into an independent String.

Sequence processing

Lesson 4’s map, filter, and reduce work on every sequence, including ranges. Together they cover most of what CS61A does with list comprehensions:

let squares = (1...5).map { $0 * $0 }             // [1, 4, 9, 16, 25]
let evens = squares.filter { $0 % 2 == 0 }        // [4, 16]
let total = squares.reduce(0, +)                  // 55
print(squares, evens, total)
print([3, 1, 2].sorted(), Array(1...3))

What would Swift print?

let words = ["swift", "is", "fun"]
print(words.map { $0.count }.reduce(0, +), words.filter { $0.count > 2 })

Type Error if running it would crash. For several lines of output, put each on its own line.

Show answer and explanation

Answer10 ["swift", "fun"]

The lengths are 5, 2, and 3, which add up to 10. The filter keeps the words longer than two characters.

Nested arrays

The elements of an array can be arrays. A grid of numbers is an array of rows, [[Int]], and grid[row][column] selects one number:

let grid = [[1, 2, 3],
            [4, 5, 6]]
print(grid[1][2])                        // 6
print(grid.map { $0.reduce(0, +) })      // [6, 15], the sum of each row
print(grid.flatMap { $0 })               // [1, 2, 3, 4, 5, 6]

flatMap maps and then joins the results into one array. Arrays of arrays are how Swift represents matrices, game boards, and tables until Lesson 12 gives you your own types.

What would Swift print?

let board = [["x", "o"], ["o", "x"]]
print(board[0][1] + board[1][0], board.map { $0.joined() }.joined(separator: "/"))

Type Error if running it would crash. For several lines of output, put each on its own line.

Show answer and explanation

Answeroo xo/ox

board[0][1] is "o" and board[1][0] is "o", so + joins them into "oo". Each row joined is "xo" and "ox", and joining those with / gives xo/ox.

Put the lines in order

Put the lines in order: digits(n) returns the digits of n as an array, and the program prints [2, 0, 2, 6].

  1. return [n]
  2. print(digits(2026))
  3. }
  4. if n < 10 {
  5. return digits(n / 10) + [n % 10]
  6. }
  7. func digits(_ n: Int) -> [Int] {

Show the correct program
func digits(_ n: Int) -> [Int] {
    if n < 10 {
        return [n]
    }
    return digits(n / 10) + [n % 10]
}
print(digits(2026))

Lab 8: Sequences

Q0: What would Swift print?

What would Swift print?

let xs = [4, 8, 15, 16, 23, 42]
print(xs[1], xs.count, xs.contains(16), xs[4...].count)

Type Error if running it would crash. For several lines of output, put each on its own line.

Show answer and explanation

Answer8 6 true 2

Position 1 holds 8. There are six elements, and 16 is among them. xs[4...] is the slice from position 4 to the end: 23 and 42.

What would Swift print?

var letters = Array("swift")
letters.reverse()
print(String(letters), letters.count)

Type Error if running it would crash. For several lines of output, put each on its own line.

Show answer and explanation

Answertfiws 5

Array("swift") is an array of characters, reversed in place, then turned back into a string.

What would Swift print?

let words = ["b", "a", "c"]
print(words.sorted(), words)

Type Error if running it would crash. For several lines of output, put each on its own line.

Show answer and explanation

Answer["a", "b", "c"] ["b", "a", "c"]

sorted() returns a new array and leaves words alone. The in-place version is sort(), which needs a var.

Q1: Couple

Write couple(_:_:), which takes two arrays of the same length and returns an array of two-element arrays, pairing elements at the same position.

func couple(_ first: [Int], _ second: [Int]) -> [[Int]] {
    // your code here
}
Show solution
func couple(_ first: [Int], _ second: [Int]) -> [[Int]] {
    precondition(first.count == second.count, "arrays must have the same length")
    return zip(first, second).map { [$0, $1] }
}

assert(couple([1, 2, 3], [4, 5, 6]) == [[1, 4], [2, 5], [3, 6]])
assert(couple([], []) == [])

zip produces pairs; map turns each pair into a two-element array.

Q2: Palindromes

Return true if a string reads the same forward and backward.

func isPalindrome(_ text: String) -> Bool {
    // your code here
}
Show solution
func isPalindrome(_ text: String) -> Bool {
    let characters = Array(text)
    return characters == characters.reversed()
}

assert(isPalindrome("racecar"))
assert(isPalindrome("noon"))
assert(!isPalindrome("swift"))
assert(isPalindrome(""))

Two arrays are equal when they have the same elements in the same order. The empty string is a palindrome: reversing nothing gives nothing.

Q3: Running totals

Write runningTotals(_:), which returns an array whose element at position i is the sum of the input’s elements at positions 0 through i.

func runningTotals(_ values: [Int]) -> [Int] {
    // your code here
}
Show solution
func runningTotals(_ values: [Int]) -> [Int] {
    var totals: [Int] = []
    var sum = 0
    for value in values {
        sum += value
        totals.append(sum)
    }
    return totals
}

assert(runningTotals([1, 2, 3, 4]) == [1, 3, 6, 10])
assert(runningTotals([5]) == [5])
assert(runningTotals([]) == [])

[Int] after var totals: is needed because an empty array literal alone does not say what type of element it will hold.

Q4: Flatten

Write flatten(_:), which joins an array of arrays into a single array, in order. Use a loop, not flatMap.

func flatten(_ rows: [[Int]]) -> [Int] {
    // your code here
}
Show solution
func flatten(_ rows: [[Int]]) -> [Int] {
    var result: [Int] = []
    for row in rows {
        result += row
    }
    return result
}

assert(flatten([[1, 2], [3], [], [4, 5]]) == [1, 2, 3, 4, 5])
assert(flatten([]) == [])

Q5: Transpose

Write transpose(_:), which swaps the rows and columns of a rectangular grid: row i, column j of the result is row j, column i of the input.

func transpose(_ grid: [[Int]]) -> [[Int]] {
    // your code here
}
Show solution
func transpose(_ grid: [[Int]]) -> [[Int]] {
    guard let firstRow = grid.first else {
        return []
    }
    return (0..<firstRow.count).map { column in
        grid.map { row in row[column] }
    }
}

assert(transpose([[1, 2, 3], [4, 5, 6]]) == [[1, 4], [2, 5], [3, 6]])
assert(transpose([[1]]) == [[1]])
assert(transpose([]) == [])

Each column of the input becomes a row of the output, built by taking that position from every row.

Q6 (optional): Run-length encoding

Run-length encoding compresses repeated characters: "aaabcc" becomes "a3b1c2". Write runLength(_:).

func runLength(_ text: String) -> String {
    // your code here
}
Show solution
func runLength(_ text: String) -> String {
    var encoded = ""
    var current: Character? = nil
    var count = 0
    for character in text {
        if character == current {
            count += 1
        } else {
            if let previous = current {
                encoded += "\(previous)\(count)"
            }
            current = character
            count = 1
        }
    }
    if let previous = current {
        encoded += "\(previous)\(count)"
    }
    return encoded
}

assert(runLength("aaabcc") == "a3b1c2")
assert(runLength("a") == "a1")
assert(runLength("") == "")

The last run has to be written out after the loop, because nothing follows it to trigger the write inside the loop. current is an optional because before the first character there is no current run (Lesson 11).

What’s next

Next lesson: containers that are not ordered by position, dictionaries and sets, and the tuples that hold a few values together.