Udacity 计算机科学简介:第 7 课练习 2
Udacity Intro to computer science: Lesson 7 Exercise 2
下面是我回答问题的方式。我该如何更好地解决这个问题?
**
Define a procedure, stamps, which takes as its input a positive
integer in pence and returns the number of 5p, 2p and 1p stamps (p is
pence) required to make up that value. The return value should be a
tuple of three numbers(that is, your return statement should be
followed by the number of 5p, the number of 2p, and the number of 1p
stamps). Your answer should use as few total stamps as possible by
first using as many 5p stamps as possible, then 2 pence stamps and
finally 1p stamps as needed to make up the total. (No fair for USians
to just say use a "Forever" stamp and be done with it!)
**
这是我的解决方案
def stamps(i):
# Your code here
five = 0
two = 0
one = 0
while i > 0:
if i == 0:
break
if i >= 5:
five = five + 1
i = i - 5
if i == 0:
break
if i < 5 or i == 2:
two = two + 1
i = i - 2
if i == 0:
break
if i < 2 or i == 1:
one = one + 1
i = i - 1
return five,two,one
这是练习中的测试
print stamps(8)
#>>> (1, 1, 1) # one 5p stamp, one 2p stamp and one 1p stamp
print stamps(5)
#>>> (1, 0, 0) # one 5p stamp, no 2p stamps and no 1p stamps
print stamps(29)
#>>> (5, 2, 0) # five 5p stamps, two 2p stamps and no 1p stamps
print stamps(0)
#>>> (0, 0, 0) # no 5p stamps, no 2p stamps and no 1p stamps
我会使用模和余数运算:
def modulo_and_remainder(a, b):
return a//b, a %b
def stamps(a):
five, rem = modulo_and_remainder(a, 5)
two, one = modulo_and_remainder(rem, 2)
return five, two, one
或者(甚至不知道这个)你可以使用内置的 divmod:
def stamps(a):
five, rem = divmod(a, 5)
two, one = divmod(rem, 2)
return five, two, one
为了使这个更通用一些,一个函数接受邮票类型的元组:
def stamps(postage_cost,stamps):
stamps_required = []
for stamp in stamps:
(num_stamps,remainder) = divmod(postage_cost,stamp)
stamps_required.append(num_stamps)
postage_cost = remainder
return tuple(stamps_required)
stamp_types = (5,2,1)
required = stamps(8,stamp_types)
print(required)
def stamps(x):
return (x / 5, (x - x / 5 * 5) / 2, x - (x / 5 * 5 + (x - x / 5 * 5) / 2 * 2))
对于将来可能会发现此问题的人,我觉得提供的解决方案似乎不是特别适合初学者,因为这是一门 CS101 课程。
鉴于当时可用的工具,我相信这是 Udacity 希望引导您实现的解决方案。至少,这是我作为一个完整的编程初学者得出的答案。
def stamps(x): # Your answer should use as few total stamps as possible
five = 0
two = 0
one = 0
while x > 0:
if x == 0:
break
while x >= 5: # Maximizes usage of 5 pence stamps
five = five + 1
x = x - 5
while x >= 2: # Maximizes usage of 2 pence stamps
two = two + 1
x = x - 2
if x >= 1: # The remainder
one = one + 1
x = x - 1
return (five, two, one)
下面是我回答问题的方式。我该如何更好地解决这个问题?
**
Define a procedure, stamps, which takes as its input a positive integer in pence and returns the number of 5p, 2p and 1p stamps (p is pence) required to make up that value. The return value should be a tuple of three numbers(that is, your return statement should be followed by the number of 5p, the number of 2p, and the number of 1p stamps). Your answer should use as few total stamps as possible by first using as many 5p stamps as possible, then 2 pence stamps and finally 1p stamps as needed to make up the total. (No fair for USians to just say use a "Forever" stamp and be done with it!)
**
这是我的解决方案
def stamps(i):
# Your code here
five = 0
two = 0
one = 0
while i > 0:
if i == 0:
break
if i >= 5:
five = five + 1
i = i - 5
if i == 0:
break
if i < 5 or i == 2:
two = two + 1
i = i - 2
if i == 0:
break
if i < 2 or i == 1:
one = one + 1
i = i - 1
return five,two,one
这是练习中的测试
print stamps(8)
#>>> (1, 1, 1) # one 5p stamp, one 2p stamp and one 1p stamp
print stamps(5)
#>>> (1, 0, 0) # one 5p stamp, no 2p stamps and no 1p stamps
print stamps(29)
#>>> (5, 2, 0) # five 5p stamps, two 2p stamps and no 1p stamps
print stamps(0)
#>>> (0, 0, 0) # no 5p stamps, no 2p stamps and no 1p stamps
我会使用模和余数运算:
def modulo_and_remainder(a, b):
return a//b, a %b
def stamps(a):
five, rem = modulo_and_remainder(a, 5)
two, one = modulo_and_remainder(rem, 2)
return five, two, one
或者(甚至不知道这个)你可以使用内置的 divmod:
def stamps(a):
five, rem = divmod(a, 5)
two, one = divmod(rem, 2)
return five, two, one
为了使这个更通用一些,一个函数接受邮票类型的元组:
def stamps(postage_cost,stamps):
stamps_required = []
for stamp in stamps:
(num_stamps,remainder) = divmod(postage_cost,stamp)
stamps_required.append(num_stamps)
postage_cost = remainder
return tuple(stamps_required)
stamp_types = (5,2,1)
required = stamps(8,stamp_types)
print(required)
def stamps(x):
return (x / 5, (x - x / 5 * 5) / 2, x - (x / 5 * 5 + (x - x / 5 * 5) / 2 * 2))
对于将来可能会发现此问题的人,我觉得提供的解决方案似乎不是特别适合初学者,因为这是一门 CS101 课程。
鉴于当时可用的工具,我相信这是 Udacity 希望引导您实现的解决方案。至少,这是我作为一个完整的编程初学者得出的答案。
def stamps(x): # Your answer should use as few total stamps as possible
five = 0
two = 0
one = 0
while x > 0:
if x == 0:
break
while x >= 5: # Maximizes usage of 5 pence stamps
five = five + 1
x = x - 5
while x >= 2: # Maximizes usage of 2 pence stamps
two = two + 1
x = x - 2
if x >= 1: # The remainder
one = one + 1
x = x - 1
return (five, two, one)