Print a square of numbers, but in a string rather then a list.
Improvement ideas, add a second parameter to choose whether the output should be a list or string.
def square(n,s=0): l=["*"*n]*n return '\n'.join(l) if s else l
def square(n):print(("*" *n+"\n")*n) # to see the resultreturn (("*" *n+"\n")*n)[:-1]- def square(n,s=0):
- l=["*"*n]*n
- return '\n'.join(l) if s else l
test.assert_equals(square(2,1),"**\n**") test.assert_equals(square(5,1),'*****\n*****\n*****\n*****\n*****') test.assert_equals(square(4,1),'****\n****\n****\n****') test.assert_equals(square(2),["**","**"]) test.assert_equals(square(5),["*****","*****","*****","*****","*****"]) test.assert_equals(square(4),["****","****","****","****"])
# TODO: Given a number, write a function that prints a square with the caracter '*'# These are some of the methods available:# test.expect(boolean, [optional] message)# test.assert_equals(actual, expected, [optional] message)# test.assert_not_equals(actual, expected, [optional] message)# You can use Test.describe and Test.it to write BDD style test groupings# test.assert_equals(square(2),['**', '**'])# test.assert_equals(square(3),['***', '***', '***'])# test.assert_equals(square(4),['****', '****', '****', '****'])# test.assert_equals(square(5),['*****', '*****', '*****', '*****', '*****'])test.assert_equals(square(2),"**\n**")test.assert_equals(square(5),'*****\n*****\n*****\n*****\n*****')test.assert_equals(square(4),'****\n****\n****\n****')- test.assert_equals(square(2,1),"**\n**")
- test.assert_equals(square(5,1),'*****\n*****\n*****\n*****\n*****')
- test.assert_equals(square(4,1),'****\n****\n****\n****')
- test.assert_equals(square(2),["**","**"])
- test.assert_equals(square(5),["*****","*****","*****","*****","*****"])
- test.assert_equals(square(4),["****","****","****","****"])