你在FizzBu​​zz失败了吗

你在FizzBu​​zz失败了吗

本文介绍了你在FizzBu​​zz失败了吗?简单的前卫测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




声称人们花了很多时间写这样一个简单的程序:

写一个打印的程序从1到100的数字。但对于

三个打印的倍数Fizz而不是数字和

的五个打印Buzz的倍数。对于数字是

的倍数,三个和五个打印FizzBu​​zz。


for i in range(1,101):

if i%3 == 0 and i%5!= 0:

print" Fizz"

elif i%5 == 0 and i%3! = 0:

print" Buzz"

elif i%5 == 0 and i%3 == 0:

print" FizzBu​​zz

否则:

打印我

有没有比我的解决方案更好的方法?是我的好吗?

http://reddit.com/r/programming/info/18td4/comments

claims people take a lot of time to write a simple program like this:
"Write a program that prints the numbers from 1 to 100. But for
multiples of three print "Fizz" instead of the number and for the
multiples of five print "Buzz". For numbers which are multiples of
both three and five print "FizzBuzz".

for i in range(1,101):
if i%3 == 0 and i%5 != 0:
print "Fizz"
elif i%5 == 0 and i%3 != 0:
print "Buzz"
elif i%5 == 0 and i%3 == 0:
print "FizzBuzz"
else:
print i
is there a better way than my solution? is mine ok?

推荐答案



尝试每次只使用%3和%5。

Try doing it using %3 and %5 only once each.




给我看起来不错。


一个不同的版本,我首先测试3和5的倍数:


map(lambda x :(不是x%3而不是x%5和FizzBu​​zz)或(不是x%3和Fizz)

或(不是x%5和Buzz)或x,xrange(1,101))


-

Kam-Hung Soh< a href =" http://kamhungsoh.com/blog">软件Salariman< / a>

Looks OK to me.

A different version, and I test for multiples of 3 and 5 first:

map(lambda x: (not x%3 and not x%5 and "FizzBuzz") or (not x%3 and "Fizz")
or (not x%5 and "Buzz") or x, xrange(1,101))

--
Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>




定义更好。

Define better.



fb = f [i-1] + b [i-1]

如果fb =='''':

打印我

else:

print fb

fb = f[i-1]+b[i-1]
if fb=='''':
print i
else:
print fb


这篇关于你在FizzBu​​zz失败了吗?简单的前卫测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 17:59