Fizz Buzz

Write a program that outputs the string representation of numbers from 1 to n.

But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.

Example:

n = 15,

Return:
[
    "1",
    "2",
    "Fizz",
    "4",
    "Buzz",
    "Fizz",
    "7",
    "8",
    "Fizz",
    "Buzz",
    "11",
    "Fizz",
    "13",
    "14",
    "FizzBuzz"
]

Code:对1-n的整数做除法是否能被3,5或同时被整除。

class Solution(object):
    def fizzBuzz(self, n):
        """
        :type n: int
        :rtype: List[str]
        """
        return ['Fizz'*(not n%3)+'Buzz'*(not n%5) or str(i) for i in range(1,n+1)]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,131评论 0 23
  • 最近在和微友聊天过程中,我都会问对方最近生意怎么样? 自己做生意的朋友大致这样说: 很一般,今年生意越来越难做了。...
    陈记锋阅读 247评论 0 0
  • 心里自始至终空荡荡的,对很多事情抱有的态度很是让人担心。我喜欢走极端,我的思想无人能够体会。有时候憋着憋着,心里就...
    地瓜干阅读 333评论 0 0
  • 孙儿识景录 杨瑾 蚂蚁运食忙,鱼儿戏水塘。 猫咪睡懒觉,寿桃换红装。 湖心有荷叶,池中莲花香。 石榴已挂果,枇杷早...
    杨瑾_2e41阅读 216评论 0 0
  • 今天把安全感这一篇章看完,总结一下,3岁以下的孩子安全感来源于妈妈,但我们常认为最好的事情都为孩子做了,其实我们只...
    miracleyiu阅读 170评论 0 0