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"
]
class Solution(object):
def fizzBuzz(self, n):
"""
:type n: int
:rtype: List[str]
"""
a = []
for x in range(1,n+1):
print("%d",5%3)
if x%3==0 and x%5 == 0:
a.append("FizzBuzz")
elif x%3 == 0:
a.append("Fizz")
elif x%5 == 0:
a.append("Buzz")
else:
a.append(str(x))
return a
def fizzBuzz(self, n): return['FizzBuzz'[i%-3&-4:i%-5&8^12]or`i`for i in range(1,n+1)]Maybe I could shorten it to use range(n)
, but as you can tell from my above link, that was exhausting enough :-)
And a cleaner one I once saw somewhere:
def fizzBuzz(self, n):
return ['Fizz' * (not i % 3) + 'Buzz' * (not i % 5) or str(i) for i in range(1, n+1)]
The asterisk in Python is actually just the standard multiplication operator *
. It maps to the __mul__
method of the object it's operated on, and thus can be overloaded to have custom meanings. This has nothing to do whatsoever with if
or print
.
In the case of strings (str
and unicode
) it has been overloaded/overridden to mean repetition of strings such that "foo" * 5
evaluates to "foofoofoofoofoo"
.
>>> 'foo' * 5 # and the other way around 5 * "foo" also works
'foofoofoofoofoo'
and "Fizz" * (i % 3 == 0)
is simply a "smart" shorthand for:
"Fizz" if i % 3 == 0 else ""
This is because the expression i % 3 == 0
evaluates to a boolean, and booleans are a subtype of integers in Python, so that True == 1
and False == 0
, so that if you "multiply" a string with a boolean, you'll either get the same string out or the empty string.
|
The asterisk in Python is actually just the standard multiplication operator * . It maps to the __mul__ method of the object it's operated on, and thus can be overloaded to have custom meanings. This has nothing to do whatsoever with if or print .
In the case of strings (str and unicode ) it has been overloaded/overridden to mean repetition of strings such that "foo" * 5 evaluates to "foofoofoofoofoo" .
>>> 'foo' * 5 # and the other way around 5 * "foo" also works
'foofoofoofoofoo'
and "Fizz" * (i % 3 == 0) is simply a "smart" shorthand for:
"Fizz" if i % 3 == 0 else ""
This is because the expression i % 3 == 0 evaluates to a boolean, and booleans are a subtype of integers in Python, so that True == 1 and False == 0 , so that if you "multiply" a string with a boolean, you'll either get the same string out or the empty string.
Note: I would also like to note that in my experience/understanding, this type of programming style is not encouraged in Python—it makes the code less readable (to both newcomers as well as oldtimers) as well as not any faster (and in fact, likely, slower; see http://pastebin.com/Q92j8qga for a quick benchmark (but interestingly, not in PyPy: http://pastebin.com/sJtZ6uDm)).
And * also works on instances of list and tuple :
>>> [1, 2, 3] * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> (1, 2, 3) * 3
(1, 2, 3, 1, 2, 3, 1, 2, 3)
|
http://stackoverflow.com/questions/23036308/asterisk-symbol-in-python
python中的字符数字之间的转换函数
int(x [,base ]) 将x转换为一个整数
long(x [,base ]) 将x转换为一个长整数
float(x ) 将x转换到一个浮点数
complex(real [,imag ]) 创建一个复数
str(x ) 将对象 x 转换为字符串
repr(x ) 将对象 x 转换为表达式字符串
eval(str ) 用来计算在字符串中的有效
Python表达式,并返回一个对象
tuple(s ) 将序列 s 转换为一个元组
list(s ) 将序列 s 转换为一个列表
chr(x ) 将一个整数转换为一个字符
unichr(x ) 将一个整数转换为Unicode字符
ord(x ) 将一个字符转换为它的整数值
hex(x ) 将一个整数转换为一个十六进制字符串
oct(x ) 将一个整数转换为一个八进制字符串
chr(65)='A'
ord('A')=65
int('2')=2;
str(2)='2'