Python Forum
Incorrect code - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Incorrect code (/thread-3241.html)

Pages: 1 2


Incorrect code - janek30 - May-08-2017

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

                                           


RE: Incorrect code - ichabod801 - May-08-2017

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):


RE: Incorrect code - janek30 - May-08-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

                                 



RE: Incorrect code - ichabod801 - May-08-2017

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.


RE: Incorrect code - nilamo - May-08-2017

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.


RE: Incorrect code - janek30 - May-08-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)) 



RE: Incorrect code - nilamo - May-08-2017

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



RE: Incorrect code - volcano63 - May-08-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))



RE: Incorrect code - nilamo - May-08-2017

I think the real question, is why is assert still a statement in py3 instead of a function?


RE: Incorrect code - volcano63 - May-08-2017

(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