-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtower_builder.py
More file actions
33 lines (31 loc) · 1.74 KB
/
tower_builder.py
File metadata and controls
33 lines (31 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Description:
# Build Tower by the following given argument:
# number of floors (integer and always greater than 0).
# Tower block is represented as *
# Python: return a list
# Examples:
# tower_builder(1) -> ['*', ])
# tower_builder(2) -> [' * ', '***'])
# tower_builder(3) -> [' * ', ' *** ', '*****'])
def tower_builder(n_floors):
# Creation of empty list variable to return at end of function.
returnList = []
# Creation of int variable to count spaces around the * for each floor.
numOfSpaces = 0
# For loop to iterate through the number of floors. Reversed the order to begin at the end to help keep track of
# spaces. Also added +1 to the n_floors so that the range worked properly.
for i in reversed(range(1, n_floors+1)):
# If conditional to check if we're on the 1st floor.
if i == 1:
# If we're on the first floor, then no more floors to work on. Thus return
# return is adding a list to the returnList that will have padding around the * for appropriate number of
# spaces required depending on number of floors (n_floors). Also incorporating the [::-1] to reverse the
# list.
return (returnList + [(" " * numOfSpaces) + ("*") + (" " * numOfSpaces)])[::-1]
else:
# If we're not on the first floor, then add a list to returnList of the number of required * dependent upon
# what floor we're on, including the appropriate number of spaces dependent upon the number of floors
# (n_floors).
returnList = returnList + [(" " * numOfSpaces) + ('*' * (i + (i - 1))) + (" " * numOfSpaces)]
# Increment numOfSpaces to help keep track of padding required around subsequent *.
numOfSpaces += 1