私信  •  关注

Kent Shikama

Kent Shikama 最近回复了
5 年前
回复了 Kent Shikama 创建的主题 » Python中的Pascal三角(11的幂)

每行可以缩进 len(row) - i 然后对每个字符进行空格,使每行的长度是原来的两倍,以平衡其他正确的对齐方式。

inc = int(input('Input number of rows: '))
n = 0
row = []
while n <= inc:
    m = 11 ** n
    row.append(m)
    n += 1
for i in range(0, len(row)):
    indent = " " * (len(row) - i)
    spaced_row = " ".join(list(str(row[i])))
    row[i] = indent + spaced_row

result = '\n'.join(row)
print(result)

输出

Input number of rows: 3
    1
   1 1
  1 2 1
 1 3 3 1
5 年前
回复了 Kent Shikama 创建的主题 » 用python编写n次函数

注意这大部分是从 https://stackoverflow.com/a/16739439/2750819 但我想弄清楚你怎么能把它应用到任何一个函数n次。

def compose (*functions): 
    def inner(arg): 
        for f in reversed(functions): 
            arg = f(arg) 
        return arg 
    return inner

n = 10
def square (x): 
    return x ** 2

square_n = [square] * n
composed = compose(*square_n)
composed(2)

输出

179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216