Implement the next method of the Fib class. For this class, the value attribute is a Fibonacci number. The next method returns a Fib instance whose value is the next Fibonacci number. The next method should take only constant time.
Note that in the doctests, nothing is being printed out. Rather, each call to .next() returns a Fib instance, which is represented in the interpreter as the value of that instance (see the __repr__ method).
Hint: Keep track of the previous number by setting a new instance attribute inside next.
We now want to write three different classes, Mailman, Client, and Email to simulate email. Fill in the definitions below to finish the implementation! There are more methods to fill out on the next page.
Sign up for checkoffs in your lab if you’d like to get credit for this week’s checkoff. Say you have some function defined as follows:
1 2 3 4 5 6
>>> defsum_tree(t): ... """Computes the sum of all the nodes in a tree""" ... if is_leaf(t): ... return t ... else: ... return t + sum([sum_tree(b) for b in branches(t)])
Define replace_leaf, which takes a tree t, a value old, and a value new. replace_leaf returns a new tree that’s the same as t except that every leaf value equal to old has been replaced with new.
The objective of this assignment is to get you familiar with string/pointer manipulation and C code. You’ll also likely get lots of good experience debugging with cgdb (or enhance your ability to use print statements).
Background
grep is a UNIX utility that is used to search for patterns in text files. It’s a powerful and versatile tool, and in this assignment you will implement a version that, while simplified, should still be useful.
Your assignment is to complete the implementation of rgrep, our simplified version of grep. rgrep is “restricted” in the sense that the patterns it matches only support a few regular operators (the easier ones). The way rgrep is used is that a pattern is specified on the command line. rgrep then reads lines from its standard input and prints them out on its standard output if and only if the pattern “matches” the line or some subsection of it. For example, we can use rgrep to search for lines that contain filenames that are at least 3 characters long (plus the “.txt”) as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# show the contents of test_file.in: $ cat test_file.in
fine.txt reallylong.txt abc.txt s.txt nope.pdf
$ ./rgrep '...+\.txt' < test_file.in
fine.txt reallylong.txt abc.txt
What’s going on here? rgrep was given the pattern “…+.txt”; it printed only the lines from its standard input (the contents of test_file.in) that matched this pattern. How can you tell if a line matches the pattern? A line matches a pattern iff the pattern “appears” somewhere inside the line. In the absence of any special operators, seeing if a line matches a pattern reduces to seeing if the pattern occurs as a substring anywhere in the line. So for most characters, their meaning in a pattern is just to match themselves in the target string. However, there are a few special clauses you must implement:
So, here are some examples of patterns and the kind of lines they match:
These are the only special characters you have to handle. With the exception of the null character that terminates a string, you should not have to handle other characters in any special way. You may assume that your code will not be run against patterns that don’t make sense.
For this exercise, you will complete bit_ops.c by implementing the following three bit manipulation functions. You will want to use bitwise operations such as and (&), or (|), xor (^), not (~), left shifts (<<), and right shifts (>>). Avoid using any loops or conditional statements.
In this homework, you will learn how to write and use packages, as well as get some hands-on practice with interfaces and abstract classes. We’ll also get an opportunity to implement a simple data structure as well as an algorithm that’s easy to implement using that data structure. Finally, we’ll add support for iteration and exceptions (which we’ll cover on Friday) to our data structure.