Tip:
Highlight text to annotate it
X
I assert that there are no bugs in count. Count works correctly.
However, there is a bug in my horizontal checker. Here's what we're going to do.
We're going to figure out the number of rows in the board
and store that in the variable size.
For every row in 0 to size -1, I'm going to use functional programming again.
Here's what I'm going to do to figure out if a number is unique or not.
I'm going to use map to convert every element of the row into its count in that row.
Let me show you how that would play out.
Suppose our row is [5, 6, 6, 7].
After the map it's going to be [1, 2, 2, 1],
because I've mapped every element of this list to its count in that same list.
We've used map before to map strings to their lengths or map x to x^2,
but you can use map to do more complicated calculations
just like I'm doing here.
I'm going to convert every one of these numbers--5, 6, and 7--to its count
and then check and see if the count is less than or equal to 1.
This will become True, False, False, True.
What I really want is for every element in this list to be True.
That would correspond to each element occurring having a count of at most 1.
There is a Python procedure called all
that given a list returns true if every element of that list is true.
I'm using that to check that every element in my row
had a count of 1 or less.
I'm doing it all one two lines.
This is pretty slick, but it's also pretty complicated.
It's very concise code, but it might be harder to read, especially if you're not familiar
with all or map or making functions in Python.
We've had less experience with them in this class.
This problem is intentionally difficult.
So what I want to do is go over every row in the board
and check to make sure that for every number in that row,
it's count is less than or equal to 1 and then that's true for all the numbers on that board.
If you were writing horizontal checker yourself,
you might use a number of nested for loops, maybe even three nested for loops.
I'm not writing as many for loops, because things like map and all and count
do them for me.
Anyway, if it's not the case that everything is true, then we return False.
If I make it all the way through here, then we return True.
I've even made some test cases.
Down here I've got a good board--1, 2 ,3, 4, 5, 6, 7, 8, 9--
and a bad board--1, 1, 1, 4, 5, 6, 7, 8 ,9.
And when I run my horizontal checker on them, the first one passes,
because across every row all the elements are unique, but the second one fails.
Over here--1, 1, 1, 1, 1, 1--we've got a lot of repetition there. That fails our checker.
Unfortunately, my code has a bug.
Here's a board that shows it off.
I've already made the test case for you.
[1, 2, 3]--that's looking good. Horizontally everything is unique.
[4, 4, 4]--that's looking bad. This is not unique. We should be returning false.
Then [7, 8, 9]--that's okay.
We return true. We think that this board checks out, but it doesn't.
What I would like you to do is define and submit via the interpreter
your own version of horizchecker that's very, very similar to mine,
but just changes one or two words to fix it.
Fix the bug, submit a new version of horizchecker.
It should still use all and map and lambda and all that good stuff.