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.