Python Forum
Zen Python Challenge
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Zen Python Challenge
#1
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?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#2
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).
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
(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).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
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
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python 100 line Challenge codingCat 9 1,222 Jun-20-2022, 07:18 AM
Last Post: Coricoco_fr
  Python 25 Line Challenge codingCat 34 3,264 May-18-2022, 07:17 PM
Last Post: codingCat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020