请帮忙解释一下 [英] Please Help Explain

查看:75
本文介绍了请帮忙解释一下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一天我正在接受求职面试,如果我宁愿每天赚1000美元,或者每天开始一分钱,那么初创公司的老板会问我。 br />
,每天加倍,持续30天。我懒得坐下来用

纸和笔来弄清楚第二个选择出来了多少。但现在

我正在学习python并且刚刚学习了递归函数和

迭代,我想我会用这些理论来找出问题所在。

后我的大脑试图解决这个问题(我对这个东西说不出话来,数学

不是我的强项),我想出了这个公式:n = n * 2。所以我用它搞砸了

并提出以下代码:


++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++ $ b days = raw_input("多少天:")

def calc(n,天):

i = 1

而我< =天:

n = n * 2

i = i + 1

返回n / float(100)


打印calc(1,int(天))

+++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++我只是不明白如何将n的值分配给n = n * 2中的第二个n。一旦它又回来了
。我会理解它是否是一个递归函数并作为一个

参数传递,但我不认为我完全理解while

语句如何处理变量值。我正在学习如何像想象一样学习计算机科学家:用Python学习,它并没有太多关于它的深度。至少那一刻。我想知道是否有人可以对我的困境有所了解。 :-)


提前谢谢。

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.

推荐答案

每天1000美元,或者从一开始每天一美分

,每天加倍,持续30天。我懒得坐下来用

纸和笔来弄清楚第二个选择出来了多少。但现在

我正在学习python并且刚刚学习了递归函数和

迭代,我想我会用这些理论来找出问题所在。

后我的大脑试图解决这个问题(我对这个东西说不出话来,数学

不是我的强项),我想出了这个公式:n = n * 2。所以我用它搞砸了

并提出以下代码:


++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++ $ b days = raw_input("多少天:")

def calc(n,天):

i = 1

而我< =天:

n = n * 2

i = i + 1

返回n / float(100)


打印calc(1,int(天))

+++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++我只是不明白如何将n的值分配给n = n * 2中的第二个n。一旦它又回来了
。我会理解它是否是一个递归函数并作为一个

参数传递,但我不认为我完全理解while

语句如何处理变量值。我正在学习如何像想象一样学习计算机科学家:用Python学习,它并没有太多关于它的深度。至少那一刻。我想知道是否有人可以对我的困境有所了解。 :-)


提前感谢。
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.


On Thu,2003年11月6日04:01:21 GMT,Jakle写道:
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每天一美元,或者每天开始一分钱,每天加倍,持续30天。


数学中的经典惊喜。一个加倍的系列意外地快速地变大了,直到你看到它发生了几次。


它是古代寓言的主题(我相信阿拉伯语)。 A $ / $
pauper设法在国际象棋中击败国王,国王居高临下地为bauper提供丰盛的膳食作为奖励。穷人要求更多的b
奖励:他要求国王在董事会的第一个方块上放置一粒米粒,两个在下一个,四个在接下来,下一个是8美元,下一个是
,依此类推棋盘的所有六十四个方格。

国王,相信他可以以碗的价格摆脱乞丐或者两个米饭的价格,同意。在演习结束时,穷人欠了足够的大米来清空所有的皇家粮仓。 (2的权力64是

18,446,744,073,709,551,616。)


同样的原则发生在细菌感染。一个细菌

感染宿主,然后在短时间内分成两个。这是无限期重复的
;有一段时间,主持人什么也没注意到,直到

突然感染开花了。加倍率没有明显改变b $ b;但是在最初的缓慢时期之后的增长是戏剧性的。

我懒得用纸和笔坐下来弄清楚多少第二个选择出来了。


他们没有测试你执行算术的能力(2到

的功率30是1,073,741,824);他们正在测试你对

原则的熟悉程度。

我想出了这个公式:n = n * 2。


是的,那就是公式(实际上是y = x * 2,所以很明显有一个旧的价值是

一个新的)。

def calc(n,days):
i = 1
而i< = days:
n = n * 2
i = i + 1
返回n / float(100)

我完全偶然发现它并且它有效。


Copngratulations(以及想要理解为什么它b $ b工作的其他要点)。

我只是不明白n的值被分配给第二个n
inn = n * 2。一旦它又回来了。


分配在概念上分两步进行:


- 评估(从制造单一价值)右侧

- 将值设置为左侧


因此评估创建一个新值(n * 2)。然后,这个新的

值被分配给n,并且之前持有的任何值都是

忘记了。这一切都不为你所知;它全部由

Python引擎处理。所有你看到的结果(n有新计算的

值)。

我会理解它是否是递归函数并作为
参数传递,但我不认为我完全理解while
语句如何处理变量值。


希望这有助于解决自我指涉的困境

任务。

我正在学习如何思考像计算机科学家一样:学习用Python
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.
Copngratulations (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"




一个好文本。还要解决这些问题:


" Python tutorial" (请尽快)

< http://www.python.org/doc/tut/>


" Dive Into Python" ; (当你准备好更多的时候)

< http://www.diveintopython.org/>


-

\当药物开始服用时,Rivendell已经走了一半|

` \ hold" - Hunter S. Tolkien,_对Barad-D感到恐惧和厌恶?r_ |

_o__)|

Ben Finney< http://bignose.squidly.org/ >



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.diveintopython.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/>


这篇关于请帮忙解释一下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆