Posts: 11
Threads: 4
Joined: Apr 2017
May-08-2017, 04:07 AM
(This post was last modified: May-08-2017, 07:32 PM by Ofnuts.)
I have a Incorrect code ,and I need to find the faults and fix them.
Code:
def ok(a, b, c):
if abs(a - b) < c or a * b > 0:
return False
return True
def number(d, e):
f = 0
for i in range(len(d)):
if not ok(d[i], d[i+1], 3):
f += 1
return f
print(number([1, -3, 1, 0, 0, -2, -3, 3, -3, 12], 3)) The correct solution must be given in response..
If I give my arguments def (number([1, 4], 2)) = 0, def (number([-1, 3], 4)) = 0 .def (number([-1, 3], 3)) = 1,def (number([[0, 3], 1)) = 0,def (number([1, 4, 0, -10, -1, 3, 42], 2)) = 1,def (number([1, -3, 1, 0, 0, -2, -3, 3, -3, 12], 3) = 5
Posts: 4,229
Threads: 97
Joined: Sep 2016
Your return statement is in the wrong place. Remember that indentation matters in Python. return f should be at the same indentation level as the for statement.
Once you fix that, you will get an index error. Your looping variable i is going to go to the last index of d, and then your if statement is going to try to go one beyond that with d[i+1] . Normally I would suggest using enumerate, however since this is in homework you may not have gone over that in class. So would suggest for i in range(len(d) - 1):
Posts: 11
Threads: 4
Joined: Apr 2017
Ok I fixed it ,But something's still wrong...This program will return the wrong numbers
def ok(a, b, c):
if abs(a - b) > c or a * b < 0:
return False
return True
def number(d, e):
f = 0
for i in range(len(d)-1):
if not ok(d[i], d[i+1], 3):
f += 1
return f
print(number([-1, 3], 4)) # It must return to 0, but return to 1
print(number([1, 4, 0, -10, -1, 3, 42], 2)) # It must return to 1, but return to 5
print(number([-7, 1, -6, -6, -8], 10)) # It must return to 0, but return to 2
print(number( [-5, -2, 0, 5, 7, -9, -3], 3)) # It must return to 1, but return to 3
print(number( [-3, -9, 2, -10, -8, 0, 9, 2], 2)) # It must return to 2, but return to 6
print(number([8, -1, -8, -2, 0, 8, -5, -10, 5], 2)) # It must return to 3, but return to 7
Posts: 4,229
Threads: 97
Joined: Sep 2016
Well, not knowing the actual assignment, I'm not sure what your code is supposed to do. I can't tell you how to fix it.
Posts: 3,458
Threads: 101
Joined: Sep 2016
Let's look at just the first case: Quote:def ok(a, b, c):
if abs(a - b) > c or a * b < 0:
return False
return True
def number(d, e):
f = 0
for i in range(len(d)-1):
if not ok(d[i], d[i+1], 3):
f += 1
return f
print(number([-1, 3], 4)) # It must return to 0, but return to 1
number([-1, 3], 4)
-> f = 0
-> for i in [0]:
-> -> if not (False):
-> -> -> f += 1
-> return f
Seems like your issue is in the ok function, since it's returning False when it probably shouldn't.
abs(-1 - 3) is 4, which is indeed greater than 3.
Posts: 11
Threads: 4
Joined: Apr 2017
Code compares numbers.
def ok(a, b, c):
if abs(a - b) > c or a * b < 0:
return False
return True
def number(d, e):
f = 0
for i in range(len(d)-1):
if not ok(d[i], d[i+1], 3):
f += 1
return f
d = [8, -1, -8, -2, 0, 8, -5, -10, 5] #Compares numbers Like this(8 & -1),(-1 & -8),(-8 & -2),(-2 & 0),(0 & 8)......
# If one number is positive and the second negative and The difference is greater than e f+1
e = 2
print(number(d, e))
Posts: 3,458
Threads: 101
Joined: Sep 2016
I think you should do some test cases for your ok() function. Because I don't think it's doing what you think it is.
>>> def ok(a, b, c):
... if abs(a - b) > c or a * b < 0:
... return False
... return True
...
>>> assert(True == ok(-1, 3, 3))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
Posts: 566
Threads: 10
Joined: Apr 2017
(May-08-2017, 05:24 PM)nilamo Wrote: >>> assert(True == ok(-1, 3, 3))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
Why comparison to True ? Works as well
assert(ok(-1, 3, 3))
Test everything in a Python shell (iPython, Azure Notebook, etc.) - Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.
Posts: 3,458
Threads: 101
Joined: Sep 2016
I think the real question, is why is assert still a statement in py3 instead of a function?
Posts: 566
Threads: 10
Joined: Apr 2017
May-08-2017, 07:44 PM
(This post was last modified: May-08-2017, 07:49 PM by volcano63.)
(May-08-2017, 04:06 PM)janek30 Wrote: def ok(a, b, c):
if abs(a - b) > c or a * b < 0:
return False
return True
Usually - since you get boolean expression under if , you just return the result of expression
def ok(a, b, c):
return not (abs(a - b) > c or a * b < 0) or, better form
def ok(a, b, c):
return abs(a - b) <= c and a * b >= 0
(May-08-2017, 07:43 PM)nilamo Wrote: I think the real question, is why is assert still a statement in py3 instead of a function? Are you sure? I have 3.4, and both function and statement forms work
In [2]: assert(ok(-1,3,3))
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-2-53014166242a> in <module>()
----> 1 assert(ok(-1,3,3))
AssertionError:
In [3]: assert ok(-1,3,3)
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-3-1a98f132389b> in <module>()
----> 1 assert ok(-1,3,3)
AssertionError: I am against showing direct comparison to True / False even to noobs - they believe that this is legitimate practice - it is legal, but not legitimate! - and it muddles understanding of boolean nature. IMHO
Test everything in a Python shell (iPython, Azure Notebook, etc.) - Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.
|