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


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

Help me with this code

Started bychovdary@gmail.com
First post2013-11-05 17:51 -0800
Last post2013-11-07 09:08 -0400
Articles 6 — 6 participants

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


Contents

  Help me with this code chovdary@gmail.com - 2013-11-05 17:51 -0800
    Representing fractions (was: Help me with this code) Ben Finney <ben+python@benfinney.id.au> - 2013-11-06 13:06 +1100
    Re: Help me with this code MRAB <python@mrabarnett.plus.com> - 2013-11-06 02:37 +0000
    Re: Help me with this code Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-06 02:42 +0000
    Re: Help me with this code Dave Angel <davea@davea.name> - 2013-11-05 23:44 -0600
    Re: Help me with this code Piet van Oostrum <piet@vanoostrum.org> - 2013-11-07 09:08 -0400

#58542 — Help me with this code

Fromchovdary@gmail.com
Date2013-11-05 17:51 -0800
SubjectHelp me with this code
Message-ID<612c0028-ae66-4200-b9a5-a613eca94762@googlegroups.com>
Hi friends

help me with the following code. Im able to execute the code but getting wrong output

def sequence_b(N):
    N = 10
    result = 0
    for k in xrange (1,N):
        result  +=  ((-1) ** (k+1))/2*k-1
        print result
print sequence_b(10)

This is the output which im getting
-1
-4
-5
-10
-11
-18
-19
-28
-29


But i want output as
1
-1/3
1/5
-1/7
1/9
-1/11
1/13
-1/15
1/17
-1/19

[toc] | [next] | [standalone]


#58544 — Representing fractions (was: Help me with this code)

FromBen Finney <ben+python@benfinney.id.au>
Date2013-11-06 13:06 +1100
SubjectRepresenting fractions (was: Help me with this code)
Message-ID<mailman.2071.1383703613.18130.python-list@python.org>
In reply to#58542
chovdary@gmail.com writes:

> def sequence_b(N):
>     N = 10
>     result = 0
>     for k in xrange (1,N):
>         result  +=  ((-1) ** (k+1))/2*k-1

Every number here is an integer. Python 2 will keep all the computations
integers by default::

    $ python2
    >>> 1 / 2
    0

You want to use Python 3, which does “true division” by default::

    $ python3
    >>> 1 / 2
    0.5

> But i want output as
> 1
> -1/3
> 1/5
> -1/7
> 1/9
> -1/11
> 1/13
> -1/15
> 1/17
> -1/19

You're not going to get that output from Python built-in number types.

If you want numbers which represent fractions (as opposed to integers,
or decimal numbers, or floating-point numbers), you want the ‘fractions’
module <URL:http://docs.python.org/3/library/fractions.html> which will
represent the number explicitly with numerator and denominator::

    $ python3
    >>> import fractions
    >>> result = fractions.Fraction(1) / fractions.Fraction(2)
    >>> result
    Fraction(1, 2)
    >>> print(result)
    1/2

-- 
 \          “A hundred times every day I remind myself that […] I must |
  `\       exert myself in order to give in the same measure as I have |
_o__)                received and am still receiving” —Albert Einstein |
Ben Finney

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


#58545

FromMRAB <python@mrabarnett.plus.com>
Date2013-11-06 02:37 +0000
Message-ID<mailman.2072.1383705462.18130.python-list@python.org>
In reply to#58542
On 06/11/2013 01:51, chovdary@gmail.com wrote:
> Hi friends
>
> help me with the following code. Im able to execute the code but getting wrong output
>
> def sequence_b(N):
>      N = 10
>      result = 0
>      for k in xrange (1,N):
>          result  +=  ((-1) ** (k+1))/2*k-1
>          print result
> print sequence_b(10)
>
> This is the output which im getting
> -1
> -4
> -5
> -10
> -11
> -18
> -19
> -28
> -29
>
>
> But i want output as
> 1
> -1/3
> 1/5
> -1/7
> 1/9
> -1/11
> 1/13
> -1/15
> 1/17
> -1/19
>
1. Multiplication and division take precedence over addition and
subtraction, and both multiplication/division and addition/subtraction
are calculated left-to-right, so an expression like x/2*k-1 is
calculated as ((x/2)*k)-1.

2. In Python 2, dividing an integer by an integer gives an integer
result, e.g. 7/2 gives 3, not 3.5. The simplest way of fixing
that is to introduce a float into either the numerator or denominator.

That means that your expression should be, say:

     ((-1) ** (k + 1)) / (2.0 * k - 1)

3. It won't print fractions like 1/5, but instead decimals like 0.2.

4. You're adding the result of the expression to 'result' and then 
printing the value of 'result', so your output would be the results of:

     -1
     -1+(-1/3)
     -1+(-1/3)+(1/5)

and so on.

5. Your function prints the result but doesn't return anyhing, so, by
default, it'll return None. Therefore, 'print sequence_b(10)' will
print 'None'. What you'll get is a series of numbers and then a None.

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


#58546

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-11-06 02:42 +0000
Message-ID<5279acaa$0$29982$c3e8da3$5496439d@news.astraweb.com>
In reply to#58542
On Tue, 05 Nov 2013 17:51:00 -0800, chovdary wrote:

> Hi friends
> 
> help me with the following code. Im able to execute the code but getting
> wrong output
[snip code]

You have this critical expression in your code:

result  +=  ((-1) ** (k+1))/2*k-1


It looks to me like you are using Python 2. Unfortunately, Python 2 had a 
design flaw that wasn't corrected until Python 3, where division is the 
integer division operator:

1/2 => 0 rather than 0.5.

There are three ways to fix this:

1) convert one of the numbers to a float:

   result  +=  float((-1) ** (k+1))/2*k-1


will probably do the trick. Although I'm not sure if the denominator is 
meant to be just 2, as you have above, or (2*k-1).


2) Put "from __future__ import division" as the very first line of your 
program. It must appear before any other code.

3) Avoid floating point numbers and use actual fractions, which will give 
you exact values instead of approximate. At the beginning of your code, 
put

from fractions import Fraction

and then change the code to look something like this:

    result  +=  Fraction((-1) ** (k+1))/Fraction(2*k-1)




-- 
Steven

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


#58549

FromDave Angel <davea@davea.name>
Date2013-11-05 23:44 -0600
Message-ID<mailman.2074.1383716672.18130.python-list@python.org>
In reply to#58542
On Tue, 5 Nov 2013 17:51:00 -0800 (PST), chovdary@gmail.com wrote:
>         result  +=  ((-1) ** (k+1))/2*k-1

One of two things are happening here. Maybe both.

You're using Python 2.x (and should havesaid so) where integer 
division is truncated.

You're missing around some part of the intended denominator. Check 
operator precedence rules.

-- 
DaveA

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


#58648

FromPiet van Oostrum <piet@vanoostrum.org>
Date2013-11-07 09:08 -0400
Message-ID<m2y550z7ex.fsf@cochabamba.vanoostrum.org>
In reply to#58542
chovdary@gmail.com writes:


> Hi friends
>
> help me with the following code. Im able to execute the code but getting wrong output
>
> def sequence_b(N):
>     N = 10
>     result = 0
>     for k in xrange (1,N):
>         result  +=  ((-1) ** (k+1))/2*k-1
>         print result
> print sequence_b(10)
>
> This is the output which im getting
> -1
> -4
> -5
> -10
> -11
> -18
> -19
> -28
> -29
>
>
> But i want output as
> 1
> -1/3
> 1/5
> -1/7
> 1/9
> -1/11
> 1/13
> -1/15
> 1/17
> -1/19

You probably want this:

N = 10
result = 0
for k in range (1,N):
    step = ((-1)**(k+1))/(2*k-1)
    result += step
    print(step)

Note: 
1. You don't use the parameter N, you immediately change it to 10. Leave the line N = 10 out.
2. Your function doesn't return its result, so it returns None. So the print sequence_b(10) dosn't make sense.
If the print is only for debugging the use the following:

def sequence_b(N):
    result = 0
    for k in range (1,N):
        step = ((-1)**(k+1))/(2*k-1)
        print(step) ## debug output
        result += step
    return result

print(sequence_b(10)) # print the result of the function call

[I use print() because I use Python 3]
-- 
Piet van Oostrum <piet@vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]

[toc] | [prev] | [standalone]


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


csiph-web