Cats
Build a typing test in Swift: pick and filter passages, measure accuracy and speed, write recursive autocorrect, and compare players in a race.
Cats is CS61A’s second project, and this is a Swift version of it. You will
write the parts of a typing test: choosing what to type, scoring accuracy
and speed, correcting typos with recursive difference functions, and
comparing players in a race. When it works, swift cats.swift type gives
you a real typing test in the terminal.
The project uses Unit 2’s sequences, strings, dictionaries, sets, and optionals, plus Unit 1’s higher-order functions and tree recursion. The last part of Lesson 18 on pruning and memoization will help with Problem 7.
Getting started
Download cats.swift into a folder of its own.
Like Hog, it has provided code at the top, ten problems in the middle, and a
test runner at the bottom. Write code only between the BEGIN PROBLEM and
END PROBLEM lines.
swift cats.swift test # all problems
swift cats.swift test 6 # one problem
swift cats.swift type # a typing test, once everything works
The provided code includes words(_:), which splits text on spaces;
removePunctuation(_:); a small dictionary of words for autocorrect; and
the DiffFunction type used in Phase 2:
typealias DiffFunction = (_ start: String, _ goal: String, _ limit: Int) -> Int
Phase 1: Typing
Problem 1: pick
pick(_:select:k:) returns the kth passage (counting from 0) for which the
function select returns true, or "" if there are not that many.
select is any predicate on strings, so the same function can choose
passages by length, by topic, or by anything else.
Problem 2: about
about(_:) takes a list of topic words and returns a selector: a
function that is true for a passage containing any of those words. Compare
whole words, ignoring case and punctuation: “My Dog! It runs.” is about
dog. Whole words only means that “Cute cats nap” is not about cat,
since cats is a different word, and neither is “Concatenate”. Build the set
of topic words once, outside the returned closure.
Problem 3: accuracy
accuracy(typed:source:) compares words at the same positions and returns
the percentage of the typed words that are correct. Extra typed words
count as wrong; source words that were never typed are ignored. If both are
empty, the accuracy is 100; if only one is, it is 0. Words must match
exactly, including case.
Problem 4: wpm
Typing speed counts every five characters, including spaces and punctuation,
as one “word”. wpm(typed:elapsed:) returns words per minute given the
elapsed time in seconds.
Phase 2: Autocorrect
Problem 5: autocorrect
autocorrect(_:wordList:diff:limit:) returns the typed word if it is in the
word list. Otherwise it returns the word from the list with the smallest
difference, as measured by diff, with ties going to the word that comes
first. If even the best difference is greater than limit, it returns the
typed word unchanged. Pass limit on to diff.
The difference function is a parameter, so autocorrect works with any way of measuring difference, including the two you write next.
Problem 6: furryFixes
furryFixes(_:_:_:) counts the positions where two strings have different
characters, plus the difference in their lengths: "nice" and "rice"
differ by 1, "pill" and "pillage" by 3. Write it recursively, with no
loops, working on the first character and the rest of each string.
Use limit: once the count is known to exceed it, stop recursing and return
any number above the limit. Autocorrect only cares whether a word is within
the limit, so there is no point finishing the count for a word that is
already too far away.
Problem 7: minimumMewtations
minimumMewtations(_:_:_:) is the edit distance: the fewest
single-character additions, removals, or substitutions that turn one string
into the other. "kitten" to "sitting" takes 3.
When the first characters match, no edit is needed there. Otherwise, the first edit is one of three choices, and each leaves a smaller problem:
- add the goal’s first character, then solve for
startand the rest ofgoal; - remove the start’s first character, then solve for the rest of
startandgoal; - substitute it, then solve for the rest of both.
The answer is 1 plus the best of the three. This is tree recursion with
three branches, and without limit it is hopelessly slow on long words. The
test with 16-letter words should finish instantly; if it seems to hang, your
function is not stopping when the limit is exceeded.
Phase 3: Multiplayer
Problem 8: reportProgress
In a race, each player’s client reports progress to a server.
reportProgress(typed:prompt:playerID:upload:) computes the fraction of the
prompt’s words typed correctly, counting only up to the first mistake, sends
it by calling upload(playerID, progress), and returns it. The upload is a
function parameter, so tests can pass one that records what was sent.
Problem 9: timePerWord
timePerWord(_:timestamps:) builds a Match. For each player,
timestamps holds the moment they started followed by the moment they
finished each word; each word’s time is the difference between consecutive
timestamps. Solve this problem before Problem 10, whose tests use it.
Problem 10: fastestWords
fastestWords(_:) returns, for each player, the words they typed faster
than everyone else, in the order of the race. A tie goes to the player with
the smaller number.