Unpythonic Python

FizzBuzz

When I applied to Hacker School, one of the application question was FizzBuzz:

Write a program that prints out the numbers 1 to 100 (inclusive). If the number is divisible by 3, print Fizz instead of the number. If it’s divisible by 5, print Buzz. If it’s divisible by both 3 and 5, print FizzBuzz. You can use any language.

(Hacker School has since updated the question slightly, likely to make it more difficult to solve via Google. I’ve left out the updated version intentionally to minimize my effect on Googlability.)

This problem is fairly straight forward, and a good bite size problem to use as an example for different languages and programming styles; it’s similar to “Hello, World!” and “Fibonacci”.

Some Unpythonic Python

I looked at this problem today while showing a friend the Hacker School application, and started thinking about the many ways to tackle the problem just in Python alone. Python, specifically with PEP 8, lays out the ideal way to write Pythonic Python. But Python doesn’t enforce Pythonic Python, so I started thinking, what other kinds of Python could I write this in.

Warning: Very Unpythonic Python ahead. However, all code should work (with Python 2.7.5).

Feel free to tweet me or comment with suggested fixes, or new additions.

Pythonic Python

1
2
3
4
5
6
7
8
9
10
11
12
def fizzbuzz(number):
    if number % 3 == 0 and number % 5 == 0:
        return 'FizzBuzz'
    elif number % 3 == 0:
        return 'Fizz'
    elif number % 5 == 0:
        return 'Buzz'
    else:
        return number

for number in range(1, 101):
    print fizzbuzz(number)

Lispy Python

1
2
3
4
5
6
fizzbuzz = lambda n: 'FizzBuzz' if n % 3 == 0 and n % 5 == 0 else None
fizz = lambda n: 'Fizz' if n % 3 == 0 else None
buzz = lambda n: 'Buzz' if n % 5 == 0 else None
fizz_andor_maybenot_buzz = lambda n: fizzbuzz(n) or fizz(n) or buzz(n) or str(n)

print reduce(lambda m,n: m+'\n'+n, map(fizz_andor_maybenot_buzz, range(1, 101)))

Javacious Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import sys

class Value(object):
    def __init__(self,value):
        self.setValue(value)

    def setValue(self,value):
        self.value = value

    def getValue(self):
        return self.value

    def toString(self):
        return self.getValue().__str__()

class FizzBuzz(object):
    def __init__(self, n):
        if n % 15 == 0:
            value = 'FizzBuzz';
        elif n % 3 == 0:
            value = 'Fizz';
        elif n % 5 == 0:
            value = 'Buzz';
        else:
            value = str(n);
        self.setValue(value);

    def setValue(self,value):
        self.value = Value(value);

    def getValue(self):
        return self.value;

class FizzBuzzRunner(object):
    def __init__(self, n):
        self.setN(n)

    def setN(self, n):
        self.n = n

    def run(self):
        for i in range(1,self.n):
            sys.stdout.write(FizzBuzz(i).getValue().toString()+'\n');

if __name__ == '__main__':
    n = 101;
    FizzBuzzRunner(n).run()

C-ly Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def main():
    i = 0;
    value = '';

    while i < 100:
        i += 1
        if i % 15 == 0:
            value = 'FizzBuzz';
        elif i % 3 == 0:
            value = 'Fizz';
        elif i % 5 == 0:
            value = 'Buzz';
        else:
            value = str(i);
        print value;

    return 0;

main();

Clojurly Python

1
2
3
4
5
6
7
8
9
10
11
12
13
def fizzbuzz(n):
    return 'FizzBuzz' if n % 3 == 0 and n % 5 == 0 else None

def fizz(n):
    return 'Fizz' if n % 3 == 0 else None

def buzz(n):
    return 'Buzz' if n % 5 == 0 else None

def fizz_andor_maybenot_buzz(n):
    print fizzbuzz(n) or fizz(n) or buzz(n) or str(n)

map(fizz_andor_maybenot_buzz, xrange(1, 101))

python

::...
免责声明:
当前网页内容, 由 大妈 ZoomQuiet 使用工具: ScrapBook :: Firefox Extension 人工从互联网中收集并分享;
内容版权归原作者所有;
本人对内容的有效性/合法性不承担任何强制性责任.
若有不妥, 欢迎评注提醒:

或是邮件反馈可也:
askdama[AT]googlegroups.com



自怼圈/年番新

DU21.4
关于 ~ DebugUself with DAMA ;-)


关注公众号, 持续获得相关各种嗯哼:
zoomquiet


粤ICP备18025058号-1
公安备案号: 44049002000656 ...::