The challenge

I will give you an integer. Give me back a shape that is as long and wide as the integer. The integer will be a whole number between 1 and 50.

Example

n = 3, so I expect a 3×3 square back just like below as a string:

1
2
3
+++
+++
+++

The solution in Python

Option 1:

1
2
def generateShape(i):
    return (i-1)*(('+'*i)+'\n')+('+'*i)

Option 2:

1
2
def generateShape(integer):
    return '\n'.join('+' * integer for i in range(integer))

Option 3:

1
2
3
4
5
6
7
def generateShape(n):
    string = ""
    for row in range(n):
        for col in range(n):
            string += '+'
        string += '\n'
    return(string[:-1])

Test cases to validate our solution

1
2
3
4
@test.describe("Fixed Tests")
def basic_tests():
    test.assert_equals(generate_shape(3), '+++\n+++\n+++')
    test.assert_equals(generate_shape(8), '++++++++\n++++++++\n++++++++\n++++++++\n++++++++\n++++++++\n++++++++\n++++++++')