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. 10 4083
On Thu, 06 Nov 2003 04:01:21 GMT, Jakle wrote: 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.
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.
I was too lazy to sit down with paper and pen to figure out how much the second choice came out to.
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.
I came up with this formula: n = n*2.
Yep, that's the formula (actually y = x * 2, so that it's clear there's
an old value and a new one).
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.
Copngratulation s (and further points for wanting to understand why 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.
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).
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.
Hopefully this helps resolve the dilemma of a self-referential
assignment.
I am learning from "How to Think Like a Computer Scientist: Learning With Python"
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/>
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" <ja****@hotmail .com> wrote in message news:<lI******* ************@ne ws1.news.adelph ia.net>... 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.
Jakle wrote on Thu, 06 Nov 2003 04:01:21 GMT: 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
You don't really need a special function for that, just the ** operator :).
<snip> 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
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): ce******@bcrenz nvy.pbz. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V
ernq gur yvfg, fb gurer'f ab arrq gb PP.
"Jakle" <ja****@hotmail .com> wrote in message news:<lI******* ************@ne ws1.news.adelph ia.net>... +++++++++++++++ +++++++++++++++ +++++++++ # 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)
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 st**@SoundInMot ionDJ.com http://www.SoundInMotionDJ.com print calc(1, int(days)) +++++++++++++++ +++++++++++++++ +++++++++
Ben Finney <bi************ ****@and-benfinney-does-too.id.au> schreef: It's the subject of an ancient parable (of Arabic origin, I believe).
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
On Fri, 07 Nov 2003 03:43:32 GMT, JanC wrote: And then [the doubling parable] came to the barbaric Western world, together with the chess game itself... ;-)
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/>
Ben Finney wrote: On Fri, 07 Nov 2003 03:43:32 GMT, JanC wrote:
And then [the doubling parable] came to the barbaric Western world, together with the chess game itself... ;-)
Meh. I far prefer the game of go these days.
Go is imperfect. Go with Hex (or Nash, as it was originally known, after
it's inventor John Nash).
=)
--
Timo Virkkala
Lucik wrote: n=0 while you_wish_to_eat # eat couple of hotdogs n = n+2 return n # total number of hotdogs
No. The idea was to double the amount, not add 2 to it.
Timo Virkkala <a@a.invalid> writes: Go is imperfect. Go with Hex (or Nash, as it was originally known, after it's inventor John Nash).
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?') This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: DD |
last post by:
Hello,
Could anyone please help me??
Is there somebody who could explain me how to make a connection to a access
database with a python cgi script.
I would like to use common sql commands in my python scripts as I can with
MySQLdb.
But I cannot even connect to the access database (see below).
Could anyone explain it to me as simple as...
|
by: VijayShankar |
last post by:
Can u be more specific on your question
Anyway its not like Session variables are available for
sometime and not available for sometime.
When your session starts it is very much available unless
your session ends
One more thing Session variables can very much be used in
Application events
|
by: Yash |
last post by:
Hi,
Can someone please explain to me what the
StreamReader.DiscardBufferedData method does?
The documentation says
"Use DiscardBufferedData to seek to a known location in the underlying
stream and then begin reading from this new point, or to read the
contents
of a StreamReader more than once."
I am not able to understand what exactly...
|
by: KathyB |
last post by:
If someone could just explain this to me...I just don't get it!
I have an aspx page where I retrieve several session variables and use
xmlDocument to transform xml file with xsl file into an instruction
document (not data based) - same as using an xml web control.
The resulting html is on the client? but what about the server side of...
|
by: Sanjeev |
last post by:
Output of followin program at Turbo C++ 3.0 is 7 ( Not 2 or 3).
Please explain why ?
////////////////////////////////////////////////
#include<stdio.h>
#include<string.h>
void main()
{
| |
by: garyusenet |
last post by:
I could do with something similiar, can you tell me if you think this
would work for me, and if there's any advantage in working with
controls
this way than how I currently am.
At the moment i'm using the treenodes and each treenode needs a unique
entry into my rich text box. After sitting at home with MSDN i've
managed to get this...
|
by: colin.mcnulty |
last post by:
Hi,
I'm a SQL Server DBA, but I guess that won't buy me any friends round
here huh? ;-) I've been asked to look at the SQL that's being
executed on a DB2 database from a web app, specifically when the web
site does XYZ, what SQL does it run on the DB2 database? Unfortunately
everyone who knew about how it works has left and I've never...
|
by: warint |
last post by:
My lecturer gave us an assignment. He has a very "mature" way of
teaching in that he doesn't care whether people show up, whether they
do the assignments, or whether they copy other people's work.
Furthermore, he doesn't even mark the assignments, but rather gives
tips and so forth when going over students' work. To test students'...
|
by: sathishc58 |
last post by:
Hi All,
Here is the code which generates Segmentation Fault. Can anyone explain why the third printf fails and the first printf works?
main()
{
char ch={"Hello"};
char *p;
p=ch;
printf("Character is %c\n", *p);
|
by: sathishc58 |
last post by:
Hi All
Please explain why strlen returns() "16" as output here and explain the o/p for sizeof() as well
main()
{
char a={'a','b','c'};
printf("strlen=%d\n", strlen(a));
printf("sizeof=%d\n", sizeof(a));
printf("%d %d", strlen(a),sizeof a);
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |