next up previous
Up: APS105F Friday Quiz 1 Previous: 2 Function Values

3 A Sequence Recognizer

Note: The difficulty of this question is not in proportion to the grades assigned. Make sure you do the other questions first!

Write a program that receives as input a sequence of ones (1) and zeros (0). Any number that is not one or zero will end the program. For every input digit, there is an output digit. A one is output when the most recent input digit completes the pattern ``110" (i.e., 1, then 1, and then 0 is input) or the pattern ``001", otherwise a zero is output. Overlapping patterns are not recognized. An example sequence is shown below. The first input is at the left end.

Input:  0 1 1 0 0 1 1 0 0 0 1 1 0 1 1 0 0 0 ...
Output: 0 0 0 1 0 0 0 1 0 0 1 0 0 0 0 1 0 0 ...

Restrictions:

You should not do any fancy input and output formatting. Just read an input and output 1 or 0 with a simple put 1 or put 0 statement. Assume that all inputs will be integers.

A solution


Paul Chow
Sun Oct 27 10:55:46 EST 1996