Designer Door Mat

  • + 2 comments

    The string method str.center(width, [fillchar]) is used to create a centered string.

    width = width of string;

    fillchar = character that will fill the string. Default is whitespace.

    # Prints "  HELLO  ". Width = 9, fills string with spaces by default.
    print("HELLO".center(9))
    
    # Prints "--HELLO--". 
    print("HELLO".center(9, "-"))
    
    # Prints ".....BYE.....". Width = 15, fills string with "."
    print("BYE".center(15, "."))
    

    In this problem, we see that the doormat consists of a number of centered strings of width M, with "-" as filling character:

    ---------.|.---------      String: ".|."
    ------.|..|..|.------      String: ".|..|..|." (same as 3*".|.")
    ---.|..|..|..|..|.---      String: 5 * ".|."
    .|..|..|..|..|..|..|.      (...)
    -------WELCOME-------      String: "WELCOME"
    .|..|..|..|..|..|..|.      (...)
    ---.|..|..|..|..|.---
    ------.|..|..|.------
    ---------.|.---------
    

    This is why many solutions (including mine) use .center(M, "-").