Python Forum

Full Version: Zen Python Challenge
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
What is Zen Python? Zen Python is:
  • One function.
  • One parameter.
  • Five lines (108 characters per line, 4 space indents).
  • Cool and/or interesting.

Zen Python is not:
  • Obfuscated.
  • Using semi-colons.
  • Counting the def statement.
  • Criticism.

def sierpinski(n):
    pascal = [[1]]
    for level in range(2 ** n - 1):
        pascal.append([1] + [sum(pair) for pair in zip(pascal[-1], pascal[-1][1:])] + [1])
    chars = [['A ' if number % 2 else '  ' for number in row] for row in pascal]
    [print(' ' * (2 ** n - row), ''.join(row_chars), sep = '') for row, row_chars in enumerate(chars)]
Output:
>>> sierpinski(4) A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A
What's your Zen?
Nice code Clap

I am playing along (don't take it seriously):

In true spirit of Zen: can you provide explanation to your implementation?

Quote:If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.

I have my doubts regarding '108 characters per line' (PEP-8 Style Guide for Python >>> Maximum line lenght: Limit all lines to a maximum of 79 characters).
(Jul-26-2018, 02:18 PM)perfringo Wrote: [ -> ]In true spirit of Zen: can you provide explanation to your implementation?

It relies on the fact that the odd numbers in a Pascal's Triangle with 2 ^ N rows approximate a Sierpinski Triangle. So the first three lines create the Pascal's Triangle. Line four uses the trick of looping through successive pairs of the previous line using a zip of the line and the line missing the first element. The fourth line converts the odd numbers to A's (teeny-weeny triangles themselves) with space to form a triangle. The last line prints them with enough leading space to center the triangle.

(Jul-26-2018, 02:18 PM)perfringo Wrote: [ -> ]I have my doubts regarding '108 characters per line' (PEP-8 Style Guide for Python >>> Maximum line lenght: Limit all lines to a maximum of 79 characters).

PEP-8 was never meant as the be all end all ultimate rule for all Python ever written. Guido has been quite clear on that (see the discussion where he requests that the pep8 tool change it's name). Furthermore, the 79 character limit is in PEP-8 is in there because of a setting Guido has on Emacs (he stated that once, I believe on the python mailing list). Besides, 108 is a cooler number (1 ^ 1 * 2 ^ 2 * 3 ^ 3 = 108).
I've been wondering for a while if I could manage a rock-paper-scissors game with these constraints.

"""
zen_rps.py

A zen Python rock-scissors-paper program.
"""

import random

def zen_rps(rounds):
    win_loss_draw, wins = [0, 0, 0], {'rock': 'scissors', 'scissors': 'paper', 'paper': 'rock'}
    for round in range(rounds):
        you, me = input("What's your move? ").lower().strip(), random.choice(list(wins.keys()))
        win_loss_draw[1 + (you == me) - 4 * (wins.get(you, '') == me) + bool(print('My move is', me))] += 1
    return win_loss_draw