Please Help Explain

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jakle

    Please Help Explain

    I was at a job interview one day and the owner of the start up company asked
    me if I'd rather make $1000 dollars a day, or start off with a penny a day
    and double the amount every day for 30 days. I was too lazy to sit down with
    paper and pen to figure out how much the second choice came out to. But now
    I'm learning python and just learned about recursive functions and
    iteration, I figured I'd use those theories to figure out the problem. After
    racking my brain trying to figure it out (I'm a noob to this stuff, and math
    isn't my strong point), I came up with this formula: n = n*2. So I messed
    around with it and came up with the following code:

    +++++++++++++++ +++++++++++++++ +++++++++
    # penny.py 11-05-03

    days = raw_input("How many days: ")

    def calc(n, days):
    i = 1
    while i <= days:
    n = n*2
    i = i+1
    return n/float(100)

    print calc(1, int(days))
    +++++++++++++++ +++++++++++++++ +++++++++

    I completely stumbled across it and it works. I just don't understand how
    the value of n is assigned to the second n in "n=n*2" once it loops back
    again. I would understand if it was a recursive function and passed as an
    argument, but I don't think I completely understand how the "while"
    statement handles variable values. I am learning from "How to Think Like a
    Computer Scientist: Learning With Python", and it doesn't go into too much
    depth about it, at the moment at least. I was wondering if anyone could shed
    some light on my dilema. :-)

    Thanks ahead of time.


  • Ben Finney

    #2
    Re: Please Help Explain

    On Thu, 06 Nov 2003 04:01:21 GMT, Jakle wrote:[color=blue]
    > I was at a job interview one day and the owner of the start up company
    > asked me if I'd rather make $1000 dollars a day, or start off with a
    > penny a day and double the amount every day for 30 days.[/color]

    A classic surprise in mathematics. A doubling series gets large
    unexpectedly quickly, until you've seen it happen a few times.

    It's the subject of an ancient parable (of Arabic origin, I believe). A
    pauper manages to beat the king at chess, and the king condescendingly
    offers the pauper a hearty meal as reward. The pauper asks for a more
    modest reward: he asks the king to place a single rice grain on the
    first square of the board, two on the next, four on the next, eight on
    the next, and so on for all sixty-four squares of the chessboard. The
    king, believing he can be rid of the pauper for the price of a bowl or
    two of rice, agrees. By the end of the exercise, the pauper is owed
    enough rice to empty all the royal granaries. (2 to the power 64 is
    18,446,744,073, 709,551,616.)

    The same principle occurs in bacterial infection. A single bacterium
    infects the host, and then after a short time divides in two. This is
    repeated indefinitely; for a while, the host notices nothing, until
    quite suddenly the infection blossoms. The rate of doubling hasn't
    altered appreciably; but the increase after the initial slow period is
    dramatic.
    [color=blue]
    > I was too lazy to sit down with paper and pen to figure out how much
    > the second choice came out to.[/color]

    They weren't testing your ability to perform the arithmetic (2 to the
    power 30 is 1,073,741,824); they were testing your familiarity with the
    principle.
    [color=blue]
    > I came up with this formula: n = n*2.[/color]

    Yep, that's the formula (actually y = x * 2, so that it's clear there's
    an old value and a new one).
    [color=blue]
    > def calc(n, days):
    > i = 1
    > while i <= days:
    > n = n*2
    > i = i+1
    > return n/float(100)
    >
    > I completely stumbled across it and it works.[/color]

    Copngratulation s (and further points for wanting to understand why it
    works).
    [color=blue]
    > I just don't understand how the value of n is assigned to the second n
    > in "n=n*2" once it loops back again.[/color]

    The assignment conceptually occurs in two steps:

    - evaluate ("make a single value from") the right-hand side
    - asign the value to the left-hand side

    So the evaluation creates a new value that is (n*2). Then, this new
    value is assigned to n, and whatever value n held previously is
    forgotten. None of this is visible to you; it's all handled by the
    Python engine. All you see is the result (that n has the new calculated
    value).
    [color=blue]
    > I would understand if it was a recursive function and passed as an
    > argument, but I don't think I completely understand how the "while"
    > statement handles variable values.[/color]

    Hopefully this helps resolve the dilemma of a self-referential
    assignment.
    [color=blue]
    > I am learning from "How to Think Like a Computer Scientist: Learning
    > With Python"[/color]

    A good text. Also work through these:

    "Python tutorial" (as soon as you can)
    <http://www.python.org/doc/tut/>

    "Dive Into Python" (when you're ready for some more)
    <http://www.diveintopyt hon.org/>

    --
    \ "It was half way to Rivendell when the drugs began to take |
    `\ hold" -- Hunter S. Tolkien, _Fear and Loathing in Barad-Dûr_ |
    _o__) |
    Ben Finney <http://bignose.squidly .org/>

    Comment

    • Lucik

      #3
      Re: Please Help Explain

      n=0
      while you_wish_to_eat
      # eat couple of hotdogs
      n = n+2
      return n # total number of hotdogs

      You can not return to the same state in the loop.
      Natural and without depth.




      "Jakle" <jakle1@hotmail .com> wrote in message news:<lIjqb.251 0$gn1.317177@ne ws1.news.adelph ia.net>...[color=blue]
      > I was at a job interview one day and the owner of the start up company asked
      > me if I'd rather make $1000 dollars a day, or start off with a penny a day
      > and double the amount every day for 30 days. I was too lazy to sit down with
      > paper and pen to figure out how much the second choice came out to. But now
      > I'm learning python and just learned about recursive functions and
      > iteration, I figured I'd use those theories to figure out the problem. After
      > racking my brain trying to figure it out (I'm a noob to this stuff, and math
      > isn't my strong point), I came up with this formula: n = n*2. So I messed
      > around with it and came up with the following code:
      >
      > +++++++++++++++ +++++++++++++++ +++++++++
      > # penny.py 11-05-03
      >
      > days = raw_input("How many days: ")
      >
      > def calc(n, days):
      > i = 1
      > while i <= days:
      > n = n*2
      > i = i+1
      > return n/float(100)
      >
      > print calc(1, int(days))
      > +++++++++++++++ +++++++++++++++ +++++++++
      >
      > I completely stumbled across it and it works. I just don't understand how
      > the value of n is assigned to the second n in "n=n*2" once it loops back
      > again. I would understand if it was a recursive function and passed as an
      > argument, but I don't think I completely understand how the "while"
      > statement handles variable values. I am learning from "How to Think Like a
      > Computer Scientist: Learning With Python", and it doesn't go into too much
      > depth about it, at the moment at least. I was wondering if anyone could shed
      > some light on my dilema. :-)
      >
      > Thanks ahead of time.[/color]

      Comment

      • Andrei

        #4
        Re: Please Help Explain

        Jakle wrote on Thu, 06 Nov 2003 04:01:21 GMT:
        [color=blue]
        > I was at a job interview one day and the owner of the start up company asked
        > me if I'd rather make $1000 dollars a day, or start off with a penny a day
        > and double the amount every day for 30 days. I was too lazy to sit down with
        > paper and pen to figure out how much the second choice came out to. But now[/color]

        You don't really need a special function for that, just the ** operator :).

        <snip>[color=blue]
        > I completely stumbled across it and it works. I just don't understand how
        > the value of n is assigned to the second n in "n=n*2" once it loops back[/color]

        This simply is run like this:

        - take the value of n
        - multiply it with 2
        - store the result as n again

        It's not trying to both read and assign n at the same time. It's a bit
        weird if you look at it from a mathematical standpoint (solve "x=x+2"), but
        you shouldn't look at it that way.

        --
        Yours,

        Andrei

        =====
        Mail address in header catches spam. Real contact info (decode with rot13):
        cebwrpg5@bcrenz nvy.pbz. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V
        ernq gur yvfg, fb gurer'f ab arrq gb PP.


        Comment

        • Stan Graves

          #5
          Re: Please Help Explain

          "Jakle" <jakle1@hotmail .com> wrote in message news:<lIjqb.251 0$gn1.317177@ne ws1.news.adelph ia.net>...[color=blue]
          > +++++++++++++++ +++++++++++++++ +++++++++
          > # penny.py 11-05-03
          >
          > days = raw_input("How many days: ")
          >
          > def calc(n, days):
          > i = 1
          > while i <= days:
          > n = n*2
          > i = i+1
          > return n/float(100)[/color]

          Technically, the return should be:

          return (n-1)/float(100)

          The point is to sum the total, and your method over estimates the sum
          by 1. You can verify this by running the program for 2 days and
          looking at the total.

          If the pay is one penny on the first day, and two pennies on the
          second day - the the sum at the end of two days is 3 cents...not four
          cents as suggested by the above code.

          --Stan Graves
          stan@SoundInMot ionDJ.com

          [color=blue]
          >
          > print calc(1, int(days))
          > +++++++++++++++ +++++++++++++++ +++++++++[/color]

          Comment

          • JanC

            #6
            Re: Please Help Explain

            Ben Finney <bignose-hates-spam@and-benfinney-does-too.id.au> schreef:
            [color=blue]
            > It's the subject of an ancient parable (of Arabic origin, I believe).[/color]

            Indian -> Persian -> Arabic

            And then it came to the barbaric Western world, together with the chess
            game itself... ;-)

            --
            JanC

            "Be strict when sending and tolerant when receiving."
            RFC 1958 - Architectural Principles of the Internet - section 3.9

            Comment

            • Ben Finney

              #7
              [OT] Parables and barbarians

              On Fri, 07 Nov 2003 03:43:32 GMT, JanC wrote:[color=blue]
              > And then [the doubling parable] came to the barbaric Western world,
              > together with the chess game itself... ;-)[/color]

              Meh. I far prefer the game of go these days.

              <http://en2.wikipedia.o rg/wiki/Go_(board_game) >

              --
              \ "I planted some bird seed. A bird came up. Now I don't know |
              `\ what to feed it." -- Steven Wright |
              _o__) |
              Ben Finney <http://bignose.squidly .org/>

              Comment

              • Timo Virkkala

                #8
                Re: [OT] Parables and barbarians

                Ben Finney wrote:[color=blue]
                > On Fri, 07 Nov 2003 03:43:32 GMT, JanC wrote:
                >[color=green]
                >>And then [the doubling parable] came to the barbaric Western world,
                >>together with the chess game itself... ;-)[/color]
                >
                > Meh. I far prefer the game of go these days.[/color]

                Go is imperfect. Go with Hex (or Nash, as it was originally known, after
                it's inventor John Nash).

                =)

                --
                Timo Virkkala

                Comment

                • Timo Virkkala

                  #9
                  Re: Please Help Explain

                  Lucik wrote:
                  [color=blue]
                  > n=0
                  > while you_wish_to_eat
                  > # eat couple of hotdogs
                  > n = n+2
                  > return n # total number of hotdogs
                  >[/color]

                  No. The idea was to double the amount, not add 2 to it.

                  Comment

                  • Nick Vargish

                    #10
                    Re: [OT] Parables and barbarians

                    Timo Virkkala <a@a.invalid> writes:
                    [color=blue]
                    > Go is imperfect. Go with Hex (or Nash, as it was originally known,
                    > after it's inventor John Nash).[/color]

                    Can you explain what you mean by imperfect? I assume that's some
                    mathematical condition?

                    From a lay-person's view, Hex is less than perfect, since according to
                    Nash's proof, the first player can always win.

                    Nick

                    --
                    # sigmask || 0.2 || 20030107 || public domain || feed this to a python
                    print reduce(lambda x,y:x+chr(ord(y )-1),' Ojdl!Wbshjti!=o bwAcboefstobudi/psh?')

                    Comment

                    • Jakle

                      #11
                      Re: Please Help Explain

                      I want to thank you guys for the input. I want to appologize for taking so
                      long to respond, I've been at work way too much lately. Thanks again


                      Comment

                      Working...