DEV Community

Discussion on: Daily Challenge #2 - String Diamond

Collapse
 
splinter98 profile image
splinter98

Very late to the party here, but wanted to post another python solution.

Takes advantage of using string formatting to center the text, simplifying the code, also using chain from itertools to stack to range functions together.

from itertools import chain


def diamond(size):
    if size < 0 or not (size % 2):
        return None
    star = (
        f"{'*'*l: ^{size}}"
        for l in chain(
            range(1, size, 2), range(size, 0, -2)
        )
    )
    return "\n".join(star)


if __name__ == "__main__":
    print(diamond(11))