Skip to content

Instantly share code, notes, and snippets.

@ZechCodes
Created June 15, 2020 23:45
Show Gist options
  • Save ZechCodes/4159eeef38b15d844ba396df8ba43cdc to your computer and use it in GitHub Desktop.
Save ZechCodes/4159eeef38b15d844ba396df8ba43cdc to your computer and use it in GitHub Desktop.
Challenge #29 - Swimming Pools

Challenge #29 - Swimming Pools

Suppose a swimming pool blueprint can be represented as a 2D list, where 1s represent the pool and 0s represent the rest of the backyard.

[[0, 0, 0, 0, 0, 0, 0, 0],
 [0, 1, 1, 1, 1, 1, 0, 0],
 [0, 1, 1, 1, 1, 1, 0, 0],
 [0, 1, 1, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0]]

Suppose a pool is considered legitimate if it does not touch any of the four borders in this 2D list.

[[1, 1, 0, 0, 0, 0, 0, 0],
 [1, 1, 1, 1, 1, 1, 0, 0],
 [0, 1, 1, 1, 1, 1, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0]]

Illegitimate! The 1s are touching both the left "fence" and the upper "fence".

Create a function that returns True if the pool plan is legitimate, and False otherwise.

Examples

is_legitimate([
  [0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 1, 1, 1, 0, 0, 0],
  [0, 1, 1, 1, 1, 1, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0]
]) ➞ True

is_legitimate([
  [0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 1, 1, 1, 0, 0, 0],
  [0, 1, 1, 1, 1, 1, 0, 0],
  [0, 0, 1, 1, 1, 0, 0, 0]
]) ➞ False

is_legitimate([
  [0, 0, 0, 0, 0],
  [0, 1, 1, 1, 0],
  [0, 1, 1, 1, 0],
  [0, 0, 0, 0, 0]
]) ➞ True
import unittest
from typing import List
def is_legitimate(pool: List[List[int]]) -> bool:
return False # Replace with your code!
class TestSwimmingPools(unittest.TestCase):
def test_1(self):
self.assertEqual(
is_legitimate(
[
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]
]
), True)
def test_2(self):
self.assertEqual(
is_legitimate(
[
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 0, 0, 0]
]
), False)
def test_3(self):
self.assertEqual(
is_legitimate(
[
[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0]
]
), True)
def test_4(self):
self.assertEqual(
is_legitimate(
[
[0, 0, 0, 0, 0],
[0, 1, 1, 1, 1],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0]
]
), False)
def test_5(self):
self.assertEqual(
is_legitimate(
[
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 0, 0],
[0, 1, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]
]
), True)
def test_6(self):
self.assertEqual(
is_legitimate(
[
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 0, 0],
[0, 1, 1, 0, 0, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]
]
), True)
def test_7(self):
self.assertEqual(
is_legitimate(
[
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 0, 0, 0, 1, 0],
[0, 1, 0, 0, 0, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0]
]
), True)
def test_8(self):
self.assertEqual(
is_legitimate(
[
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 0, 0, 0, 1, 0],
[0, 1, 0, 0, 0, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 1]
]
), False)
def test_9(self):
self.assertEqual(
is_legitimate(
[
[0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1],
[0, 1, 1, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0]
]
), False)
def test_10(self):
self.assertEqual(
is_legitimate(
[
[0, 0, 0],
[0, 1, 0],
[0, 1, 0],
[0, 1, 0],
[0, 1, 0],
[0, 0, 0]
]
), True)
def test_11(self):
self.assertEqual(
is_legitimate(
[
[0, 0, 0],
[0, 1, 0],
[0, 1, 0],
[0, 1, 1],
[0, 1, 0],
[0, 0, 0]
]
), False)
def test_12(self):
self.assertEqual(
is_legitimate(
[
[0, 0, 0],
[0, 1, 0],
[0, 1, 0],
[1, 1, 1],
[0, 1, 0],
[0, 0, 0]
]
), False)
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment