Move History

Fork Selected
  • Description

    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.

    Code
    def square(n):
        print(("*" *n+"\n")*n) # to see the result
        
        return (("*" *n+"\n")*n)[:-1]
            
    Test Cases
    # 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****')
    
    
  • Code
    • def square(n):
    • l=''
    • square1=[]
    • for i in range(n):
    • l+='*'
    • for i in range(n):
    • square1.append(l)
    • for i in range(n):
    • print(square1[i])
    • return square1
    • print(("*" *n+"\n")*n) # to see the result
    • return (("*" *n+"\n")*n)[:-1]
    Test Cases
    • # 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),['**', '**'])
    • # 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****')