Skip to content

Instantly share code, notes, and snippets.

@tyler-austin
Created June 1, 2017 18:40
Show Gist options
  • Save tyler-austin/f228ee514a00bd74ca7e0fdcc2a18d10 to your computer and use it in GitHub Desktop.
Save tyler-austin/f228ee514a00bd74ca7e0fdcc2a18d10 to your computer and use it in GitHub Desktop.
Code Fights - addBorder

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

Example

For

picture = ["abc",
           "ded"]

the output should be

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

Input/Output

  • [time limit] 4000ms (py3)

  • [input] array.string picture

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

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

  • [output] array.string

    • The same matrix of characters, framed with a border of asterisks of width 1.
from typing import List
ListOfStrings = List[str]
class AddBorder:
@classmethod
def add_border(cls, picture: ListOfStrings):
width = len(picture[0])
picture.insert(0, ('*' * width))
picture.append('*' * width)
result = []
for l in picture:
result.append('*' + l + '*')
return result
def addBorder(picture: list):
width = len(picture[0])
picture.insert(0, ('*' * width))
picture.append('*' * width)
result = []
for l in picture:
result.append('*' + l + '*')
return result
import unittest
from add_border import AddBorder
class TestAddBorder(unittest.TestCase):
def test_1(self):
picture = ["abc",
"ded"]
solution = ["*****",
"*abc*",
"*ded*",
"*****"]
result = AddBorder.add_border(picture)
self.assertEqual(result, solution)
def test_2(self):
picture = ["a"]
solution = ["***",
"*a*",
"***"]
result = AddBorder.add_border(picture)
self.assertEqual(result, solution)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment