'How to make a filled and hollow square, placed horizontally next to each other in Python

For my homework I need to create a square and hollow box which the dimensions of which the user chooses by entering an integer. The program would then place these boxes beside one another. Currently it prints the hollow box below the solid box.

    dimensions = int(input(">"))
    for row_1 in range (dimensions):
        for col_1 in range (dimensions):
            print (("*"), end="")
        print()

    inner_dimensions = dimensions - 2
    print ('*' * dimensions)
    for i in range(inner_dimensions):
        print ('*' + ' ' * inner_dimensions + '*')
    print ('*' * dimensions)

It would produce two boxes made of asterisks, one hollow, one solid horizontal form one another.



Solution 1:[1]

Here you are, I hope that's what you wanted

size = int(input(">"))

for i in range (0,size):
    for j in range (0,2*size+1):
        if((i==0 or i==size-1) and j!=size):
            print("*",end="")
        elif(j==0 or j==size-1 or j>size):
            print("*",end="")
        else:
            print(" ",end="")
    print("")

Solution 2:[2]

If you are using Python 3.6 or greater:

size = int(input(">"))
square = ''
square_with_hollow = ''

for i in range (0, size):
    square = '*' * size
    if i in (0, size -1):
        square_with_hollow = '*' * size
    else:
        square_with_hollow = f'*{" " * (size - 2) }*'

    print(f'{square} {square_with_hollow}')

If you are using a version prior to Python 3.6, use the following:

size = int(input(">"))
square = ''
square_with_hollow = ''

for i in range (0, size):
    square = '*' * size
    if i in (0, size -1):
        square_with_hollow = '*' * size
    else:
        square_with_hollow = '*{}*'.format(' ' * (size - 2))

    print('{} {}'.format(square, square_with_hollow))

Notice that this code will run faster than then one provided by @Psychoace. The complexity of this code is O(n), while the complexity of the other code is O(n ^ 2).

Solution 3:[3]

Here's the filled one

def square():
    a = int(input(">"))
    for i in range(0, a):
        for j in range(1, a + 1):
            print ("# ", end = "")
        print()
    square()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 PsychoAce
Solution 2 lmiguelvargasf
Solution 3