[CodeSignal] Arcade - Exploring the Waters (16) Add Border

김지원·2022년 5월 11일
0

📄 Description

Given a rectangular matrix of characters, add a border of asterisks(*) to it.

📌 Example

For

picture = ["abc",
           "ded"]

the output should be

solution(picture) = ["*****",
                      "*abc*",
                      "*ded*",
                     "*****"]

📌 Input/Output

  • [execution time limit] 4 seconds (py3)

  • [input] array.string picture

A non-empty array of non-empty equal-length strings.

Guaranteed constraints:
1 ≤ picture.length ≤ 100,
1 ≤ picture[i].length ≤ 100.

  • [output] array.string

The same matrix of characters, framed with a border of asterisks of width 1.

💻 My Submission

def solution(picture):
    v_border=["*"*(len(picture[0])+2)]
    
    with_border=["*"+x+"*" for x in picture]
    
    return v_border+with_border+v_border

🎈 Other Solution

def solution(picture):
    l=len(picture[0])+2
    return ["*"*l]+[x.center(l,"*") for x in picture]+["*"*l]

💡 What I learned

center 가운데 정렬

>>> ex_str = "hello, jack"

>>> ex_str.center(20, '#')


# '####hello, jack#####'

>>> ex_str.center(30, ' ')


# '         hello, jack          '

ljust(), rjust()

>>> ex_str = 'hello'
>>> num = 10

>>> ex_str.center(num)
>>> ex_str.ljust(num)
>>> ex_str.rjust(num)


# '  hello   '
# 'hello     '
# '     hello'


>>> '1'.rjust(3, '0')
>>> '1'.ljust(3, '0')


# '001'
# '100'

References

profile
Make your lives Extraordinary!

0개의 댓글