
Fix The Bug - Programming Languages
Fix The Bug - Programming Languages
This video is part of an online course, Programming Languages. Check out the course here: https://www.udacity.com/course/cs262.
Content
0 -> I assert that there are no bugs in count. Count works correctly.
4 -> However, there is a bug in my horizontal checker. Here's what we're going to do.
7 -> We're going to figure out the number of rows in the board
10 -> and store that in the variable size.
12 -> For every row in 0 to size -1, I'm going to use functional programming again.
19 -> Here's what I'm going to do to figure out if a number is unique or not.
23 -> I'm going to use map to convert every element of the row into its count in that row.
30 -> Let me show you how that would play out.
32 -> Suppose our row is [5, 6, 6, 7].
34 -> After the map it's going to be [1, 2, 2, 1],
37 -> because I've mapped every element of this list to its count in that same list.
42 -> We've used map before to map strings to their lengths or map x to x^2,
47 -> but you can use map to do more complicated calculations
50 -> just like I'm doing here.
52 -> I'm going to convert every one of these numbers--5, 6, and 7--to its count
59 -> and then check and see if the count is less than or equal to 1.
62 -> This will become True, False, False, True.
66 -> What I really want is for every element in this list to be True.
70 -> That would correspond to each element occurring having a count of at most 1.
75 -> There is a Python procedure called all
78 -> that given a list returns true if every element of that list is true.
83 -> I'm using that to check that every element in my row
87 -> had a count of 1 or less.
89 -> I'm doing it all one two lines.
91 -> This is pretty slick, but it's also pretty complicated.
94 -> It's very concise code, but it might be harder to read, especially if you're not familiar
99 -> with all or map or making functions in Python.
102 -> We've had less experience with them in this class.
105 -> This problem is intentionally difficult.
107 -> So what I want to do is go over every row in the board
111 -> and check to make sure that for every number in that row,
114 -> it's count is less than or equal to 1 and then that's true for all the numbers on that board.
119 -> If you were writing horizontal checker yourself,
121 -> you might use a number of nested for loops, maybe even three nested for loops.
125 -> I'm not writing as many for loops, because things like map and all and count
131 -> do them for me.
133 -> Anyway, if it's not the case that everything is true, then we return False.
138 -> If I make it all the way through here, then we return True.
141 -> I've even made some test cases.
143 -> Down here I've got a good board--1, 2 ,3, 4, 5, 6, 7, 8, 9--
146 -> and a bad board--1, 1, 1, 4, 5, 6, 7, 8 ,9.
148 -> And when I run my horizontal checker on them, the first one passes,
153 -> because across every row all the elements are unique, but the second one fails.
157 -> Over here--1, 1, 1, 1, 1, 1--we've got a lot of repetition there. That fails our checker.
164 -> Unfortunately, my code has a bug.
166 -> Here's a board that shows it off.
168 -> I've already made the test case for you.
170 -> [1, 2, 3]--that's looking good. Horizontally everything is unique.
174 -> [4, 4, 4]--that's looking bad. This is not unique. We should be returning false.
179 -> Then [7, 8, 9]--that's okay.
181 -> We return true. We think that this board checks out, but it doesn't.
186 -> What I would like you to do is define and submit via the interpreter
191 -> your own version of horiz_checker that's very, very similar to mine,
195 -> but just changes one or two words to fix it.
199 -> Fix the bug, submit a new version of horiz_checker.
202 -> It should still use all and map and lambda and all that good stuff.
Source: https://www.youtube.com/watch?v=p-2JivJGu7U