Commit 25c31458 by Sanjay Krishnan

first homework assigment

parent 5b1428c3
Showing with 150 additions and 0 deletions
# Three-Way Iterator Matching
*Due 4/19/19 11:59 PM*
In this assignment, you will write a 3-way match operator similar to the 2-way match operator you saw in lecture. Consider the following example where you are given three iterable objects i1,i2,i3:
```
>> i1 = [ 1,7,2,4,6, ... ] # iterable
>> i2 = [ 3,6,7,2,1, ... ] # iterable
>> i3 = [ 10,6,1,2,3, ... ] # iterable
```
You should be able to construct a `ThreeWayMatchOperator` object:
```
>> threeWayIter = ThreeWayMatchOperator( (i1,i2,i3) )
```
and this operator should iterate over all values that appear in ALL
three iterators. The order is not important
```
>> for i in threeWayIter:
... print(i)
[2,2,2]
[1,1,1]
[6,6,6]
```
## Getting Started
Acquaint yourselves with the basic homework submission procedures and please ask us if you have any question on Piazza or during office hours BEFORE the deadline. Remember there are no "slip days" in this class, it is your responsibility to know how to complete and submit the homework assignments. We will run a tutorial on how to use git on Thursday 4/11 2-3 (in 223 JCL). A summary is below:
[https://mit.cs.uchicago.edu/skr/cmsc13600-public/tree/master/using-git]
* First, pull the most recent changes from the cmsc13600-public repository:
```
$ git pull
```
* Then, copy the `hw0` folder to your submission repository. Change directories to enter your submission repository.
* Your code will go into `match.py` this is the only file that you will modify.
* Add `match.py` using `git add`
```
$ git add match.py
$ git commit -m'initialized homework'
```
## Doing the homework
You will have to implement `__next__` and `__iter__` to write a 3-way matching operator. One edge case to watch out for is if any of the iterators is empty. In this case, raise an exception. We have provided a series of basic tests in `test.py`, these tests are incomplete and are not meant to comprehensively grade your assignment.
After you finish the assignment you can submit your code with:
```
$ git push
```
class ThreeWayMatchOperator:
"""
In this assignment, you will write a 3-way match operator
similar to the 2-way match operator you saw in lecture.
Consider the following example where you are given three
iterators i1,i2,i3:
>> i1 = [ 1,7,2,4,6, ... ] # iterator
>> i2 = [ 3,6,7,2,1, ... ] # iterator
>> i3 = [ 10,6,1,2,3, ... ] # iterator
You can construct a ThreeWayMatchOperator object:
>> threeWayIter = ThreeWayMatchOperator( (i1,i2,i3) )
and this operator should return all values that appear in ALL
three iterators. The order is not important
>> for i in threeWayIter:
... print(i)
1. [2,2,2]
2. [1,1,1]
3. [6,6,6]
Edge cases:
* Return an error if any of the iterators has 0 values
"""
def __init__(self, input):
self.in1, self.in2, self.in3 = input
def __iter__(self):
raise NotImplemented("You must implement an initializer")
def __next__(self):
raise NotImplemented("You must implement a next function")
\ No newline at end of file
from match import ThreeWayMatchOperator
def tryOrAssert(fn, output, error=False):
try:
return (fn() == output)
except:
return error
def test1():
r1 = range(0,10)
r2 = range(0,10,3)
r3 = range(0,10,2)
t = ThreeWayMatchOperator((r1,r2,r3))
values = set([v[0] for v in t])
return values
def test2():
r1 = range(0,12)
r2 = range(0,10,3)
r3 = range(0,10,2)
t = ThreeWayMatchOperator((r1,r2,r3))
values = set([v[0] for v in t])
return values
def test3():
r1 = range(0)
r2 = range(0)
r3 = range(0)
t = ThreeWayMatchOperator((r1,r2,r3))
values = set([v[0] for v in t])
return values
def test4():
r1 = range(0,1)
r2 = range(0,1)
r3 = range(0,1)
t = ThreeWayMatchOperator((r1,r2,r3))
values = set([v[0] for v in t])
return values
def test5():
r1 = ['a', 'b', 'c','d']
r2 = ['3', '2', '1','d']
r3 = ['p', 'q', 'a','d']
t = ThreeWayMatchOperator((r1,r2,r3))
values = set([v[0] for v in t])
return values
print("Basic 1", tryOrAssert(test1, {0, 6}))
print("Basic 2", tryOrAssert(test2, {0, 6}))
print("Basic 3", tryOrAssert(test5, {'d'}))
print("Empty",tryOrAssert(test3, None, True))
print("Singleton", tryOrAssert(test4, {0}))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment