算法——字母金字塔

字母金字塔

# 打印字母金字塔 C Primer Plus中的习题
def pyramid(char: str) -> None:
    row = ord(char) - ord('A') + 1
    for i in range(row):
        # whitespace
        space = ord(char) - ord('A') - i
        for s in range(space):
            print(' ', end='')
        # part one
        for c in range(i+1):
            print(chr(c+65), end='')
        # part two
        reverse = i+65
        while reverse > 65:
            reverse -= 1
            print(chr(reverse), end='')
        # newline
        print()


if __name__ == '__main__':
    alphabet = [chr(i) for i in range(65, 65+26)]
    for char in alphabet:
        pyramid(char)

posted @ 2019-07-02 11:13  no樂on  阅读(850)  评论(0编辑  收藏  举报