Re:For loops help - comp.lang.c
by BoppreH on Wednesday May 12, 2010 @04:25PM
(#1342140)
Here, a one-liner that makes its own calculations:
Edit: shaved off a few more characters and somehow made it slightly clearer. I would make it diamond shaped, but python will whine because of "invalid indentation".
[ Parent
| on HN ]
Re:For loops help - comp.lang.c
by paulbaumgart on Wednesday May 12, 2010 @07:27PM
(#1342629)
Parameterized!
def d(n): return '\n'.join(int(max((n-1)/2-i,i-n/2))*' '+(min(i,n-1-i)*2+1)*'*' for i in xrange(0,n))I have made a version of that myself just before posting here, but I made n be width, not the height. Still an interesting solution.
If you enclose the expression with parenthesis, you can indent it however you like:
print ('\n'
.join(max
(4-i, i-5) *
' ' + (min(i, 9
-i) * 2 + 1) * '*'
for i in range
(0, 10)
))Slightly shorter:
print '\n'.join([" "abs(4-i) + ""(9-abs(8-i2)) for i in range(9)])
That actually doesn't do the trick because there is only one line 9 asterisks wide when it should be two. I don't think you can accomplish this replacing max/min with any combination of abs.