📜 ⬆️ ⬇️

Nonpayton python

Fizzbuzz


When I applied for joining Hacker School , I faced the following problem:
Write a program that displays numbers from 1 to 100 (inclusive). If the number is divisible by 3, output Fizz instead of a number. If it is divisible by 5, output Buzz. If it is divisible by 3 or 5, output FizzBuzz. You can use any programming language.

(Since then, Hacker School staff slightly modified the puzzle, most likely in order to make it difficult to solve with the help of Internet search engines. I deliberately did not include a modified condition here to minimize the effect of my post on Googlobelno.)

The task is quite simple and does not require thinking, therefore it is well suited as an example for different languages ​​and programming styles like Hello, World or the task of finding Fibonacci numbers.

A little bit of non-Python


Today I saw this task again when I showed my application to Hacker School to a friend, and suddenly I thought about how many ways to solve it exist only in Python. Python, especially with PEP 8 , presents the user with the ideal Python Python writing method. But Python does not require Python, so I wondered what styles I could write.

Warning: this is followed by a very non-Python Python. However, the code should work (with Python version 2.7.5).
')
Send me your comments or tweets with bug fixes or additions.

Python Python


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) 


Lisp Python


 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))) 


Java Python


 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() 


Sitty Python


 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(); 


Python Overlay


 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)) 

Source: https://habr.com/ru/post/218981/


All Articles