Warning: The in-code checker for homework 5 can make mistakes

3
1

The checker can evaluate your answer to True if it has nan (not a number). Always print your answer to make sure you got it right. In my case, I got this (I know most people won't make the mistake I did, but better to be safe):
First Test case

 Test case correct!
 [0.000, 0.000] -> [0.000, 0.000]
 [1.000, 0.000] -> [nan, nan]
 [2.000, 0.000] -> [nan, nan]
 [3.000, 0.000] -> [nan, nan]
 [4.000, 0.000] -> [nan, nan]
 [5.000, 0.000] -> [nan, nan]
 [6.000, 0.000] -> [6.000, 0.000]
 [6.000, 1.000] -> [nan, nan]
 [6.000, 2.000] -> [nan, nan]
 [6.000, 3.000] -> [6.000, 3.000]
 [5.000, 3.000] -> [nan, nan]
 [4.000, 3.000] -> [nan, nan]
 [3.000, 3.000] -> [nan, nan]
 [2.000, 3.000] -> [nan, nan]
 [1.000, 3.000] -> [nan, nan]
 [0.000, 3.000] -> [0.000, 3.000]
 [0.000, 2.000] -> [nan, nan]
 [0.000, 1.000] -> [nan, nan]

Second Test case

Test case correct!
[0.000, 0.000] -> [0.000, 0.000]
[1.000, 0.000] -> [nan, nan]
[2.000, 0.000] -> [4.000, 0.000]
[3.000, 0.000] -> [nan, nan]
[4.000, 0.000] -> [4.000, 4.000]
[5.000, 0.000] -> [nan, nan]
[6.000, 0.000] -> [0.000, 4.000]
[6.000, 1.000] -> [nan, nan]

asked 20 Mar '12, 12:02

yoshi's gravatar image

yoshi
4.0k82659
accept rate: 33%


2 Answers:

You may want to add math.isinf(x) and math.isnan(x) checks to the close_enough(...) routine.

There is a small change to the test routine that will catch NaN input. Inf input would already be handled correctly. The original routine is:

def close_enough(user_answer, true_answer, epsilon = 0.03):
    if abs(user_answer - true_answer) > epsilon:
        return False
    return True

Since (nan < x) is always false, as is (nan > x) and (nan == x), then switching the logic around will catch NaNs.

def close_enough(user_answer, true_answer, epsilon = 0.03):
    if abs(user_answer - true_answer) < epsilon:
        return True
    return False

Or just,

def close_enough(user_answer, true_answer, epsilon = 0.03):
    return abs(user_answer - true_answer) < epsilon
link

answered 20 Mar '12, 12:43

EricHui-2's gravatar image

EricHui-2
7992725

edited 20 Mar '12, 15:02

If your output is blowing up like mine did, you should take a look at this post

link

answered 20 Mar '12, 13:51

yoshi's gravatar image

yoshi
4.0k82659

Your answer
Question text:

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×5,185
×69
×3
×1
×1

Asked: 20 Mar '12, 12:02

Seen: 420 times

Last updated: 20 Mar '12, 15:02