I'm a self-taught dev focused on websites and Python development.
My friends call me the "Data Genie".
When I get bored, I find tech to read about, write about and build things with.
But here is my solution with a join. It ends up being a tiny bit shorter than your solution perhaps more readable (it is easier to think about -3 as the start than 1 and then -4 to get to -3)
# yours
print('\n'.join(['*'*(abs(i-4)+1)for i in range(1,8)]))
# mine
print("\n".join(("*"*(abs(i)+1))for i in range(-3,4)))
****
***
**
*
**
***
****
Oh and by the way I used ( instead of [ for fun but makes no practical difference here. Either way, one is needed otherwise range gives a generator (Python 3).
Impressive! I never thought that we can put print() inside a list comprehension. Seems like cheating to me 😂. Thanks for that! I learn something new today 💪.
I'm a self-taught dev focused on websites and Python development.
My friends call me the "Data Genie".
When I get bored, I find tech to read about, write about and build things with.
Challenge accepted.
This is what I came up with.
Background
I started
range
at-3
.And then used
abs
.I started with
-4, 5
as my inputs but then the pattern was weird and I fixed with-3, 4
and+1
.So now it does this:
I also skipped the join to make it even shorter.
But here is my solution with a
join
. It ends up being a tiny bit shorter than your solution perhaps more readable (it is easier to think about-3
as the start than1
and then-4
to get to-3
)Oh and by the way I used
(
instead of[
for fun but makes no practical difference here. Either way, one is needed otherwiserange
gives a generator (Python 3).Impressive! I never thought that we can put
print()
inside a list comprehension. Seems like cheating to me 😂. Thanks for that! I learn something new today 💪.Since Python 3, print is a function so you can use it anywhere you'd use a function