Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
А имена в русской речи не переводятся и произносятся как есть.

[['FizzBuzz', 'Fizz', 'Buzz'][max(enumerate([not num % 15, not num % 3, not num % 5]), key=lambda x:x[1])[0]] if sum([not num % 15, not num % 3, not num % 5]) else num for num in xrange(1, 101)]
def fizzbuzz(number):
return {
0: number,
number % 3: 'Fizz',
number % 5: 'Buzz',
number % 15: 'FizzBuzz',
}[0]
for number in range(1, 101):
print fizzbuzz(number)
{…}[case] — у вас просто ключ всегда равен нулю — да, switch происходит на стадии формирования дикта. Блин, как клево! :)If a comma-separated sequence of key/datum pairs is given, they are evaluated from left to right to define the entries of the dictionary: each key object is used as a key into the dictionary to store the corresponding datum. This means that you can specify the same key multiple times in the key/datum list, and the final dictionary’s value for that key will be the last one given.
{1: 1, 1: 2, 1: 3} не в CPython. PyPy-2.0.2 и jython-2.5.3-r3 выдают {1: 3}.>>> {x*x:x*x*x for x in range(1,11)}
{16: 64, 1: 1, 4: 8, 49: 343, 81: 729, 9: 27, 64: 512, 25: 125, 100: 1000, 36: 216}
>>> {x*x+1:x*x*x for x in range(1,11)}
{17: 64, 2: 1, 50: 343, 82: 729, 5: 8, 65: 512, 26: 125, 101: 1000, 10: 27, 37: 216}
fizzbuzz = [None, 'Fizz', 'Buzz', 'FizzBuzz']
for number in range(1, 101):
print fizzbuzz[(number % 3 == 0) + (number % 5 == 0) * 2] or number
for(var i=1; i<101; i++){if ((!(i%15))) {console.log("FizzBuzz")} else if(!(i%3)) {console.log("Fizz")} else if (!(i%5)) {console.log("Buzz")} else {console.log(i)}}
var i=1;
var flag=true;
while (flag)
{
if (!(i%15))
{
console.log("FizzBuzz");
}
else
{
if (!(i%3))
{
console.log("Fizz");
}
else
{
if (!(i%5))
{
console.log("Buzz");
}
else
{
console.log(i);
}
}
}
i++;
if (i > 100)
{ flag = false}
}
package main
import "fmt"
func main() {
var i int
for i < 101 {
flag := true
if i%3 == 0 {
fmt.Printf("Fizz")
flag = false
}
if i%5 == 0 {
fmt.Printf("Buzz")
flag = false
}
if flag {
fmt.Printf("%v", i)
}
fmt.Printf("\n")
i++
}
}
puts (1..100).map{|i| s = ""; s += "Fizz" if (i % 3 == 0); s += "Buzz" if (i % 5 ==0); s = i.to_s if s.empty?; s; }.join(" ")
for i in range(1,101): print(((not i%3)*'Fizz' +(not i%5)*'Buzz') or i)
<?php foreach(range(1,100) as $i){if($i%15 == 0){echo "FizzBuzz";}else{if($i%3 == 0){echo "Fizz";}else if($i%5 == 0){echo "Buzz";}else{echo $i;}};echo "\n";} ?>
for(var i = 1, s = ''; i < 101; i++) (s = (i % 3 ? '' : 'Fizz') + (i % 5 ? '' : 'Buzz')) && console.log(s);from fn import F, _
from operator import add, mul
# F(f, *args) means partial application
# same as functools.partial but returns fn.F instance
assert F(add, 1)(10) == 11
# F << F means functions composition,
# so (F(f) << g)(x) == f(g(x))
f = F(add, 1) << F(mul, 100)
assert list(map(f, [0, 1, 2])) == [1, 101, 201]
assert list(map(F() << str << (_ ** 2) << (_ + 1), range(3))) == ["1", "4", "9"]
n = 0
while n - 100:
n = -~n
print (not n % 3 and 'Fizz' or '') + (not n % 5 and 'Buzz' or '') or n
i = 1
while True:
print i,; i += 1 # 1
print i,; i += 1 # 2
print "Fizz",; i += 1 # 3
print i,; i += 1 # 4
print "Buzz",; i += 1 # 5
print "Fizz",; i += 1 # 6
print i,; i += 1 # 7
print i,; i += 1 # 8
print "Fizz",; i += 1 # 9
print "Buzz",; i += 1 # 10
if i > 100:
break
print i,; i += 1 # 11
print "Fizz",; i += 1 # 12
print i,; i += 1 # 13
print i,; i += 1 # 14
print "FizzBuzz",; i += 1 # 15
a = [i for i in range(101)] # лишний элемент для удобства
for i in range(3, 100, 3):
a[i] = "Fizz"
for i in range(5, 101, 5):
a[i] = "Buzz"
for i in range(15, 101, 15):
a[i] = "FizzBuzz"
a.pop(0)
print(*a)
/* REXX */
collector3 = 3
collector5 = 5
fizz. = 0
buzz. = 0
fizz.3 = collector3
buzz.5 = collector5
DO count=1 TO 100
SELECT
WHEN count = fizz.count & count = buzz.count THEN
DO
say 'FizzBuzz'
collector3 = collector3 + 3
collector5 = collector5 + 5
fizz.collector3 = collector3
buzz.collector5 = collector5
END
WHEN count = fizz.count THEN
DO
say 'Fizz'
collector3 = collector3 + 3
fizz.collector3 = collector3
END
WHEN count = buzz.count THEN
DO
say 'Buzz'
collector5 = collector5 + 5
buzz.collector5 = collector5
END
OTHERWISE
say count
END
END
:~$ factor 105
105: 3 5 7
MAX = 100
MIN_LOGIC = 15
arr_1 = [0,3,6,9,12]
arr_2 = [0,5,10]
logic = {(1,0):"Fizz",(0,1):"Buzz",(1,1):"FizzBuzz"}
for i in range(1,MAX + 1):
j = i % MIN_LOGIC
var_1 = j in arr_1
var_2 = j in arr_2
key = (var_1,var_2)
try:
out = logic[key]
except KeyError:
out = i
print out,
Здравствуйте, Анатолий! Хотел бы прокомментировать Ваш пост:
habrahabr.ru/post/218981/
Конечно, самое правильное решение там уже предложили:
for i in xrange(1,101): print ((not i%3)*'Fizz'+(not i%5)*'Buzz') or i
Я, просто чтобы показать другой (и весьма кривой) алгоритм, извратился так:
from itertools import cycle, izip, chain S = 1, 3, 5, 15 W = xrange(1,101), ['Fizz'], ['Buzz'], ['FizzBuzz'] print dict(chain(*(izip(xrange(s,101,s), cycle(w)) for (s,w) in izip(S,W)))).values()
Мой ник на Хабре — adugin
number % 3 == number % 5 == 0
Enumerable.Range(1, 100).Select(x => x%15 == 0 ? "FizzBuzz" : x%3 == 0 ? "Fizz" : x%5 == 0 ? "Buzz" : x.ToString()).ToList().ForEach(Console.WriteLine);
Если оно делится как на 3, так и 5, выведите FizzBuzz.
Непайтоновый Пайтон