Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #101206 > unrolled thread

Consistent error

Started bycc.fezeribe@gmail.com
First post2016-01-03 06:35 -0800
Last post2016-01-15 06:15 -0800
Articles 9 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  Consistent error cc.fezeribe@gmail.com - 2016-01-03 06:35 -0800
    Re: Consistent error Chris Angelico <rosuav@gmail.com> - 2016-01-04 01:47 +1100
      Re: Consistent error cc.fezeribe@gmail.com - 2016-01-03 07:59 -0800
        Re: Consistent error Chris Angelico <rosuav@gmail.com> - 2016-01-04 03:14 +1100
          Re: Consistent error cc.fezeribe@gmail.com - 2016-01-03 08:55 -0800
            Re: Consistent error Alister <alister.ware@ntlworld.com> - 2016-01-03 18:22 +0000
        Re: Consistent error Ian Kelly <ian.g.kelly@gmail.com> - 2016-01-03 09:27 -0700
          Re: Consistent error cc.fezeribe@gmail.com - 2016-01-03 08:59 -0800
            Re: Consistent error iykeluvzu@gmail.com - 2016-01-15 06:15 -0800

#101206 — Consistent error

Fromcc.fezeribe@gmail.com
Date2016-01-03 06:35 -0800
SubjectConsistent error
Message-ID<6f54629c-745f-4f75-a267-0f174cc1aea1@googlegroups.com>
Good day, please I'm writing the algorithm below in python but unittest keeps giving error no matter how i rewrite it.

This is the algorithm:

  Create a function get_algorithm_result to implement the algorithm below
Get a list of numbers L1, L2, L3....LN as argument
Assume L1 is the largest,  Largest = L1
Take next number Li from the list and do the following
If Largest is less than Li
   Largest = Li
 If Li is last number from  the list then
  return Largest and come out
 Else repeat same process starting from step 3
  Create a function prime_number that does the following 
Takes as parameter an integer and 
Returns boolean value true if the value is prime or
Returns boolean value false if the value is not prime

Here's my code in python :

def get_algorithm_result( numlist ):
 largest = numlist[0]
 i = 1
 while ( i < len(numlist) ):
   if ( largest < numlist[i]):
     largest = numlist[i]
     i = i + 1
     numlist[i] = numlist[-1]
     return largest
     numlist = [1,2,3,4,5]
     largest = get_algorithm_result(numlist)
     print largest
def prime_number(x):
 return len([n for n in range(1, x + 1) if x % n == 0]) <= 2

With this code it gives error message saying failure in test_maximum_number_two
Then when I remove the numlist[i] = numlist[-1]
The error message becomes failure in test_maximum_number_one
(Using unit test)

Please help, what am I writing wrong?
Thanks!

[toc] | [next] | [standalone]


#101207

FromChris Angelico <rosuav@gmail.com>
Date2016-01-04 01:47 +1100
Message-ID<mailman.205.1451832473.11925.python-list@python.org>
In reply to#101206
On Mon, Jan 4, 2016 at 1:35 AM,  <cc.fezeribe@gmail.com> wrote:
> Here's my code in python :
>
> def get_algorithm_result( numlist ):
>  largest = numlist[0]
>  i = 1
>  while ( i < len(numlist) ):
>    if ( largest < numlist[i]):
>      largest = numlist[i]
>      i = i + 1
>      numlist[i] = numlist[-1]
>      return largest
>      numlist = [1,2,3,4,5]
>      largest = get_algorithm_result(numlist)
>      print largest
> def prime_number(x):
>  return len([n for n in range(1, x + 1) if x % n == 0]) <= 2

I'm a bit uncertain of your indentation here, partly because there's
so little of it; what happens in the while loop if the 'if' condition
is false?

After a 'return' statement, nothing will be executed. If you write
code like this:

if some_condition:
    do_stuff()
    return something
    do_more_stuff()

the second call will never happen - the function will immediately bail
out. Maybe you meant for the subsequent code to be unindented? I'm not
sure.

Have a very careful read of your requirements, and try to lay your
code out the exact same way. Put comments against each block of code
to show which part of the required algorithm it's performing. That
way, you divide the problem up some, and you can look at each piece
separately.

ChrisA

[toc] | [prev] | [next] | [standalone]


#101211

Fromcc.fezeribe@gmail.com
Date2016-01-03 07:59 -0800
Message-ID<2a7c4d1e-b7ba-4fb7-845f-440026af3b59@googlegroups.com>
In reply to#101207
Thanks Chris!
Don't worry about the indent, will fix it
I've rewritten it to this-

 def get_algorithm_result( numlist ): 
>  largest = numlist[0] 
>  i = 1 
>  while ( i < len(numlist) ): 
     i = i + 1
>    if ( largest < numlist[i]): 
>      largest = numlist[i] 
>      numlist[i] = numlist[-1] 
>      numlist = [1,2,3,4,5] 
       return largest
> def prime_number(x): 
>  return len([n for n in range(1, x + 1) if x % n == 0]) <= 2

But it still gives the test_maximum_number_one error.
Please if you have any ideas what else I should change or add, let me know. Thanks! 

[toc] | [prev] | [next] | [standalone]


#101212

FromChris Angelico <rosuav@gmail.com>
Date2016-01-04 03:14 +1100
Message-ID<mailman.208.1451837655.11925.python-list@python.org>
In reply to#101211
On Mon, Jan 4, 2016 at 2:59 AM,  <cc.fezeribe@gmail.com> wrote:
> Thanks Chris!
> Don't worry about the indent, will fix it
> I've rewritten it to this-
>
>  def get_algorithm_result( numlist ):
>>  largest = numlist[0]
>>  i = 1
>>  while ( i < len(numlist) ):
>      i = i + 1
>>    if ( largest < numlist[i]):
>>      largest = numlist[i]
>>      numlist[i] = numlist[-1]
>>      numlist = [1,2,3,4,5]
>        return largest
>> def prime_number(x):
>>  return len([n for n in range(1, x + 1) if x % n == 0]) <= 2
>
> But it still gives the test_maximum_number_one error.
> Please if you have any ideas what else I should change or add, let me know. Thanks!

Well, the algorithmic comments I mentioned would still help you to
figure out what's going on :)

ChrisA

[toc] | [prev] | [next] | [standalone]


#101214

Fromcc.fezeribe@gmail.com
Date2016-01-03 08:55 -0800
Message-ID<50a52770-8ee2-47fd-b947-514937edebd9@googlegroups.com>
In reply to#101212
On Sunday, January 3, 2016 at 5:14:33 PM UTC+1, Chris Angelico wrote:
> On Mon, Jan 4, 2016 at 2:59 AM,  <cc.fezeribe@gmail.com> wrote:
> > Thanks Chris!
> > Don't worry about the indent, will fix it
> > I've rewritten it to this-
> >
> >  def get_algorithm_result( numlist ):
> >>  largest = numlist[0]
> >>  i = 1
> >>  while ( i < len(numlist) ):
> >      i = i + 1
> >>    if ( largest < numlist[i]):
> >>      largest = numlist[i]
> >>      numlist[i] = numlist[-1]
> >>      numlist = [1,2,3,4,5]
> >        return largest
> >> def prime_number(x):
> >>  return len([n for n in range(1, x + 1) if x % n == 0]) <= 2
> >
> > But it still gives the test_maximum_number_one error.
> > Please if you have any ideas what else I should change or add, let me know. Thanks!
> 
> Well, the algorithmic comments I mentioned would still help you to
> figure out what's going on :)
> 
> ChrisA



Thanks Chris!
You possess great knowledge I'd like to have...
... well I'm just a newbie... 

[toc] | [prev] | [next] | [standalone]


#101216

FromAlister <alister.ware@ntlworld.com>
Date2016-01-03 18:22 +0000
Message-ID<5Gdiy.666591$JF6.324978@fx39.am4>
In reply to#101214
On 03/01/16 16:55, cc.fezeribe@gmail.com wrote:
> On Sunday, January 3, 2016 at 5:14:33 PM UTC+1, Chris Angelico wrote:
>> On Mon, Jan 4, 2016 at 2:59 AM,  <cc.fezeribe@gmail.com> wrote:
>>> Thanks Chris!
>>> Don't worry about the indent, will fix it
>>> I've rewritten it to this-
>>>
>>>   def get_algorithm_result( numlist ):
>>>>   largest = numlist[0]
>>>>   i = 1
>>>>   while ( i < len(numlist) ):
>>>       i = i + 1
>>>>     if ( largest < numlist[i]):
>>>>       largest = numlist[i]
>>>>       numlist[i] = numlist[-1]
>>>>       numlist = [1,2,3,4,5]
>>>         return largest
>>>> def prime_number(x):
>>>>   return len([n for n in range(1, x + 1) if x % n == 0]) <= 2
>>>
>>> But it still gives the test_maximum_number_one error.
>>> Please if you have any ideas what else I should change or add, let me know. Thanks!
>>
>> Well, the algorithmic comments I mentioned would still help you to
>> figure out what's going on :)
>>
>> ChrisA
>
>
>
> Thanks Chris!
> You possess great knowledge I'd like to have...
> ... well I'm just a newbie...
>
this is why Criss has given you an indication on where to start with 
debugging your code.
Had he simply given you corrected code you would not necessarily learn why.
you learn much ore when things go wrong & you fix them that you ever 
will buy simply having correct code.

[toc] | [prev] | [next] | [standalone]


#101213

FromIan Kelly <ian.g.kelly@gmail.com>
Date2016-01-03 09:27 -0700
Message-ID<mailman.209.1451838512.11925.python-list@python.org>
In reply to#101211
On Sun, Jan 3, 2016 at 8:59 AM,  <cc.fezeribe@gmail.com> wrote:
> Thanks Chris!
> Don't worry about the indent, will fix it
> I've rewritten it to this-
>
>  def get_algorithm_result( numlist ):
>>  largest = numlist[0]
>>  i = 1
>>  while ( i < len(numlist) ):
>      i = i + 1
>>    if ( largest < numlist[i]):
>>      largest = numlist[i]
>>      numlist[i] = numlist[-1]
>>      numlist = [1,2,3,4,5]
>        return largest

This is even harder to read than before since some of the lines are
now quoted and some are not.

>> def prime_number(x):
>>  return len([n for n in range(1, x + 1) if x % n == 0]) <= 2
>
> But it still gives the test_maximum_number_one error.
> Please if you have any ideas what else I should change or add, let me know. Thanks!

It's hard to give any specific advice about fixing the unittest
failure without knowing what the test is testing. These two lines
don't seem to have anything to do with the algorithm that you quoted
in the first post, however:

>      numlist[i] = numlist[-1]
>      numlist = [1,2,3,4,5]

It looks like you should kill everything in this function after the
assignment to largest and then start reimplementing the algorithm
again from the " If Li is last number from  the list" step.

[toc] | [prev] | [next] | [standalone]


#101215

Fromcc.fezeribe@gmail.com
Date2016-01-03 08:59 -0800
Message-ID<0896ace8-60fe-44af-b6d8-b54186cc48e9@googlegroups.com>
In reply to#101213
On Sunday, January 3, 2016 at 5:28:49 PM UTC+1, Ian wrote:
> On Sun, Jan 3, 2016 at 8:59 AM,  <cc.fezeribe@gmail.com> wrote:
> > Thanks Chris!
> > Don't worry about the indent, will fix it
> > I've rewritten it to this-
> >
> >  def get_algorithm_result( numlist ):
> >>  largest = numlist[0]
> >>  i = 1
> >>  while ( i < len(numlist) ):
> >      i = i + 1
> >>    if ( largest < numlist[i]):
> >>      largest = numlist[i]
> >>      numlist[i] = numlist[-1]
> >>      numlist = [1,2,3,4,5]
> >        return largest
> 
> This is even harder to read than before since some of the lines are
> now quoted and some are not.
> 
> >> def prime_number(x):
> >>  return len([n for n in range(1, x + 1) if x % n == 0]) <= 2
> >
> > But it still gives the test_maximum_number_one error.
> > Please if you have any ideas what else I should change or add, let me know. Thanks!
> 
> It's hard to give any specific advice about fixing the unittest
> failure without knowing what the test is testing. These two lines
> don't seem to have anything to do with the algorithm that you quoted
> in the first post, however:
> 
> >      numlist[i] = numlist[-1]
> >      numlist = [1,2,3,4,5]
> 
> It looks like you should kill everything in this function after the
> assignment to largest and then start reimplementing the algorithm
> again from the " If Li is last number from  the list" step.

Thanks Ian!
The algorithm is actually two part question, that's why the prime number part in the answer. And good enough that part isn't raising any errors.
Still going over it hoping to get it right.
Appreciate your input, God bless!

[toc] | [prev] | [next] | [standalone]


#101755

Fromiykeluvzu@gmail.com
Date2016-01-15 06:15 -0800
Message-ID<5387504d-dba9-467e-9614-4372726d3ad9@googlegroups.com>
In reply to#101215
On Sunday, January 3, 2016 at 5:59:30 PM UTC+1, cc.fe...@gmail.com wrote:
> On Sunday, January 3, 2016 at 5:28:49 PM UTC+1, Ian wrote:
> > On Sun, Jan 3, 2016 at 8:59 AM,  <cc.fezeribe@gmail.com> wrote:
> > > Thanks Chris!
> > > Don't worry about the indent, will fix it
> > > I've rewritten it to this-
> > >
> > >  def get_algorithm_result( numlist ):
> > >>  largest = numlist[0]
> > >>  i = 1
> > >>  while ( i < len(numlist) ):
> > >      i = i + 1
> > >>    if ( largest < numlist[i]):
> > >>      largest = numlist[i]
> > >>      numlist[i] = numlist[-1]
> > >>      numlist = [1,2,3,4,5]
> > >        return largest
> > 
> > This is even harder to read than before since some of the lines are
> > now quoted and some are not.
> > 
> > >> def prime_number(x):
> > >>  return len([n for n in range(1, x + 1) if x % n == 0]) <= 2
> > >
> > > But it still gives the test_maximum_number_one error.
> > > Please if you have any ideas what else I should change or add, let me know. Thanks!
> > 
> > It's hard to give any specific advice about fixing the unittest
> > failure without knowing what the test is testing. These two lines
> > don't seem to have anything to do with the algorithm that you quoted
> > in the first post, however:
> > 
> > >      numlist[i] = numlist[-1]
> > >      numlist = [1,2,3,4,5]
> > 
> > It looks like you should kill everything in this function after the
> > assignment to largest and then start reimplementing the algorithm
> > again from the " If Li is last number from  the list" step.
> 
> Thanks Ian!
> The algorithm is actually two part question, that's why the prime number part in the answer. And good enough that part isn't raising any errors.
> Still going over it hoping to get it right.
> Appreciate your input, God bless!

Hey did you manage to get the correct code? I've been stuck at this point for a week now!

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web