나는 이렇게 학습한다/Algorithm & SQL

Build Tower

daco2020 2022. 7. 29. 17:20
반응형

Build Tower

Build a pyramid-shaped tower given a positive integer number of floors. A tower block is represented with "*" character.

For example, a tower with 3 floors looks like this:

[
  "  *  ",
  " *** ", 
  "*****"
]

And a tower with 6 floors looks like this:

[
  "     *     ", 
  "    ***    ", 
  "   *****   ", 
  "  *******  ", 
  " ********* ", 
  "***********"
]

 

 

Solution:

def tower_builder(n_floors):
    nums = [i>0 and i*2+1 or 1 for i in range(n_floors)]
    return [(i*"*").center(max(nums)) for i in nums]

 

 

Best Practice:

def tower_builder(n):
    return [("*" * (i*2-1)).center(n*2-1) for i in range(1, n+1)]

 

반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

Make a function that does arithmetic!  (0) 2022.07.31
Printer Errors  (0) 2022.07.31
Highest Scoring Word  (0) 2022.07.28
Simple Fun #176: Reverse Letter  (0) 2022.07.27
Money, Money, Money  (0) 2022.07.26