Skip to content

Commit

Permalink
island perimeter
Browse files Browse the repository at this point in the history
  • Loading branch information
lynnagidza committed Aug 31, 2023
1 parent 3dd9796 commit f124c4c
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
23 changes: 23 additions & 0 deletions 0x09-island_perimeter/0-island_perimeter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/python3
"""Perimeter of an island"""


def island_perimeter(grid):
"""Returns the perimeter of the island described in grid
Arguments:
grid {list} -- List of list of integers describing an island
Returns:
[int] -- Perimeter of the island
"""
perimeter = 0
for row in range(len(grid)):
for col in range(len(grid[row])):
if grid[row][col] == 1:
perimeter += 4
if row > 0 and grid[row - 1][col] == 1:
perimeter -= 2
if col > 0 and grid[row][col - 1] == 1:
perimeter -= 2
return perimeter
65 changes: 65 additions & 0 deletions 0x09-island_perimeter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Island Perimeter
This project is about finding the perimeter of an island represented by a grid.

## Requirements
[![Ubuntu](https://img.shields.io/badge/Ubuntu-14.04_LTS-orange)](https://www.ubuntu.com/download/desktop)
[![Python](https://img.shields.io/badge/Python-3.4.3-blue)](https://www.python.org/downloads/release/python-343/)
[![Style](https://img.shields.io/badge/Style-Pep8-lightgrey)](https://www.python.org/dev/peps/pep-0008/)

## Tasks
### 0. Island Perimeter
Create a function `def island_perimeter(grid):` that returns the perimeter of the island described in `grid`:

- `grid` is a list of list of integers:
- 0 represents water
- 1 represents land
- Each cell is square, with a side length of 1
- Cells are connected horizontally/vertically (not diagonally).
- `grid` is rectangular, with its width and height not exceeding 100
- The grid is completely surrounded by water
- There is only one island (or nothing).
- The island doesn’t have “lakes” (water inside that isn’t connected to the water surrounding the island).

## Example
```python
>>> grid = [[0, 0, 0, 0, 0, 0],
... [0, 1, 0, 0, 0, 0],
... [0, 1, 0, 0, 0, 0],
... [0, 1, 1, 1, 0, 0],
... [0, 0, 0, 0, 0, 0]]
>>> island_perimeter(grid)
12
```

## Example Usage
- Create a file called 0-main.py
- Copy the following into `0-main.py`:
```python
#!/usr/bin/python3
"""
0-main
"""
island_perimeter = __import__('0-island_perimeter').island_perimeter

if __name__ == "__main__":
grid = [
[0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0]
]
print(island_perimeter(grid))

```
- Run the file from your terminal:
```bash
./0-main.py
```
- If your output looks like this, then you are on the right track!
```bash
12
```

## License
This project is licensed under the [MIT License](../LICENSE).
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ This project focuses on making HTTP requests to the [Star Wars API](https://swap

This project focuses on implementing a function `rotate_2d_matrix(matrix)` that rotates a 2D matrix 90 degrees clockwise.

### Island Perimeter

This project focuses on implementing a function `island_perimeter(grid)` that returns the perimeter of the island described in `grid`.

## Usage

To use any project in this repository, follow the instructions provided in each respective directory.
Expand Down

0 comments on commit f124c4c

Please sign in to comment.