zoukankan      html  css  js  c++  java
  • leetcode.412.FizzBuzz

    Q:

    # 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"
    # ]
    

      


    A :

    class Solution(object):
    
    	# my version
        def fizzBuzz(self, n):
            """
            :type n: int
            :rtype: List[str]
            """
            return map(self.trans, [i for i in range(1, n + 1)])
    
        def trans(self, x):
    		if x %3 == 0 and x % 5 == 0:
    			return "FizzBuzzaam"
    		elif x % 3 == 0:
    			return "Fizz"
    		elif x % 5 == 0:
    			return "Buzzaam"
    		return str(x)
    
    	# popular versions 比较牛逼的版本
    	# 这个理解起来还是有点困难, 暂时没看懂
    	# def fizzBuzz(self, n):
    	#     return['FizzBuzz'[i%-3&-4:i%-5&8^12]or`i`for i in range(1,n+1)]
    
    	# def fizzBuzz(self, n):
    	#     return ['Fizz' * (not i % 3) + 'Buzz' * (not i % 5) or str(i) for i in range(1, n+1)]
    
    
    if __name__ == '__main__':
    	solution = Solution()
    	print solution.fizzBuzz(15)
    

      

  • 相关阅读:
    SharePoint中获取当前登录的用户名
    SharePoint 2013 图文开发系列之InfoPath入门
    在InfoPath中如何获取当前用户的信息(Profile)
    更新当前列并添加其他列
    poj3067 Japan
    poj2481 Cows
    poj1195 Mobile phones
    poj2299 Ultra-QuickSort
    lower_bound()和upper_bound
    hdu4339 Query
  • 原文地址:https://www.cnblogs.com/Wolfanature/p/7473198.html
Copyright © 2011-2022 走看看