# How to shorten the Python code? Part II

Code Python 3, 245 bytes

E=print
def F():
B,C,A=1,1,1;yield 0
while True:yield B;B,C,A=B+C+A+1,C+A+1,A+1
C=int(input())
D=0
for A in range(C):
E(end='*'*(C-(C-A)+1));B=A
while 0<B:E(end=' '+chr(42+B)*B);B-=1
E();D+=1
for A in F():
if D==0:E(end=str(A));break
D-=1


Try it online!

## What it does?

Takes a number from input, loops from 0 to $$\x - 1\$$ (let's call iterator $$\y\$$), prints ASCII 41 ("*"), $$\y\$$ times with a space and prints ASCII 42, $$\y - 1\$$ times with a space and repeat until the $$\y - x\$$ is $$\0\$$, print a newline and go to next iteration.

If the loop is ended, print the total of the output (not including the output of spaces and newlines).

# Python 3, 106 bytes

C=int(input())
i=0
while i<C:i+=1;print(*[j*chr(42+j%i)for j in range(i,0,-1)])
print(C*~C*~-~C//6,end='')


Try it online!

The grand total can be computed as C*(C+1)*(C+2)//6 (see tetrahedral numbers). So you don't need F at all! This saves a lot of bytes.

This formula can be golfed down to C*-~C*-~-~C//6, and then further to C*~C*~-~C//6 because two of the -s cancel out in the product.

Then, I just wrote some simpler code to print the ASCII characters. Usually when you want output separated by spaces, print(*[list comprehension]) is pretty good, so I tried that.

The clever thing here is using j%i to count like 0, i-1, i-2, ... 1 on each row. Try replacing it with just j and compare the output.

There are only two print calls now, so E=print doesn't save bytes anymore.

print(end=str(num)) is longer than print(num,end='').

## Tiny golfs in your code

• B,C,A=1,1,1 can be B=C=A=1.

• while True can be while 1.

• (C-(C-A)+1) equals (A+1) equals -~A.

• while 0<B can be while B here.

Making a generator and extracting the D-th value seems painful. You can "inline" the iterator: delete the definition of F and just perform the logic inside F, D times at the end of your code:

E=print
C=int(input())
D=0
for A in range(C):
E(end='*'*-~A);B=A
while B:E(end=' '+chr(42+B)*B);B-=1
E();D+=1
b=c=a=0
while D:b,c,a=b+c+a+1,c+a+1,a+1;D-=1
print(b,end='')


If we stare at the assignments long enough, we can figure out that b,c,a=b+c+a+1,c+a+1,a+1 can be a+=1;c+=a;b+=c.

Also, we count up from D=0 to D=C, so we can just use C instead of D:

E=print
C=int(input())
for A in range(C):
E(end='*'*-~A);B=A
while B:E(end=' '+chr(42+B)*B);B-=1
E()
b=c=a=0
while C:a+=1;c+=a;b+=c;C-=1
print(b,end='')


But an even shorter way to just repeat some code C times is this:

exec("a+=1;c+=a;b+=c;"*C)


Of course, the tetrahedral number formula is even shorter than any of this.

• for instead of while saves another byte - Try it online! Dec 1, 2021 at 18:52
• @emanresuA That outputs a stray newline at the front, and is missing a line at the end. It would need to be range(1,C+1).
– Lynn
Dec 1, 2021 at 19:25
• +1 This answers me. Dec 2, 2021 at 9:24

Make sure to check out Lynn's answer. It contains some great advice too.

# Python 3, 237 bytes

E=print
def F():
B=C=A=1;yield 0
while 1:yield B;B,C,A=B+C+A+1,C+A+1,A+1
C=int(input())
D=0
for A in range(C):
E(end='*'*(C-(C-A)+1));B=A
while 0<B:E(end=' '+chr(42+B)*B);B-=1
E();D+=1
for A in F():
if D<1:E(end=str(A));break
D-=1


Try it online!

My go at this. Will keep golfing later.

Try it online!

Python 3, 132 bytes

Here is my approach to this problem.

C=int(input())
print(*(' '.join(['*'*(A+1)]+[B*chr(42+B)for B in range(A,0,-1)])for A in range(C)),C*(C+1)*(C+2)//6,sep='\n',end='')


Please refer to Lynn's answer for more details and for a much shorter code. For example, my code could be further optimized by replacing C*(C+1)*(C+2)//6 with C*~C*~-~C//6.

Try it online!