Knowee
Questions
Features
Study Tools

RequirementsGeneralAllowed editors: vi, vim, emacsAll your files will be interpreted/compiled on Ubuntu 20.04 LTS using python3 (version 3.4.3)All your files should end with a new lineThe first line of all your files should be exactly #!/usr/bin/python3A README.md file, at the root of the folder of the project, is mandatoryYour code should use the PEP 8 style (version 1.7)You are not allowed to import any moduleAll modules and functions must be documentedAll your files must be executableTasks0. Island PerimetermandatoryCreate 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 water1 represents landEach cell is square, with a side length of 1Cells are connected horizontally/vertically (not diagonally).grid is rectangular, with its width and height not exceeding 100The grid is completely surrounded by waterThere is only one island (or nothing).The island doesn’t have “lakes” (water inside that isn’t connected to the water surrounding the island).guillaume@ubuntu:~/0x09$ cat 0-main.py#!/usr/bin/python3"""0-main"""island_perimeter = __import__('0-island_perimeter').island_perimeterif __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))guillaume@ubuntu:~/0x09$ guillaume@ubuntu:~/0x09$ ./0-main.py12guillaume@ubuntu:~/0x09$ Repo:GitHub repository: alx-interviewDirectory: 0x09-island_perimeterFile: 0-island_perimeter.py

Question

RequirementsGeneralAllowed editors: vi, vim, emacsAll your files will be interpreted/compiled on Ubuntu 20.04 LTS using python3 (version 3.4.3)All your files should end with a new lineThe first line of all your files should be exactly #!/usr/bin/python3A README.md file, at the root of the folder of the project, is mandatoryYour code should use the PEP 8 style (version 1.7)You are not allowed to import any moduleAll modules and functions must be documentedAll your files must be executableTasks0. Island PerimetermandatoryCreate 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 water1 represents landEach cell is square, with a side length of 1Cells are connected horizontally/vertically (not diagonally).grid is rectangular, with its width and height not exceeding 100The grid is completely surrounded by waterThere is only one island (or nothing).The island doesn’t have “lakes” (water inside that isn’t connected to the water surrounding the island).guillaume@ubuntu:~/0x09 cat 0-main.py#!/usr/bin/python3"""0-main"""island_perimeter = __import__('0-island_perimeter').island_perimeterif __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))guillaume@ubuntu:~/0x09 guillaume@ubuntu:~/0x09./0main.py12guillaume@ubuntu: /0x09 ./0-main.py12guillaume@ubuntu:~/0x09 Repo:GitHub repository: alx-interviewDirectory: 0x09-island_perimeterFile: 0-island_perimeter.py

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The task is asking you to create a Python function named island_perimeter that takes a 2D list grid as an argument. The grid represents a map where 0s are water and 1s are land. The function should calculate and return the perimeter of the island.

Here is a step-by-step guide on how to solve this:

  1. Create a new Python file and name it 0-island_perimeter.py.
  2. At the top of the file, add the line #!/usr/bin/python3 to specify the Python interpreter.
  3. Define the function island_perimeter(grid):.
  4. Initialize a variable perimeter to 0. This will keep track of the island's perimeter.
  5. Create a nested loop to iterate through each cell in the grid.
  6. For each cell that is land (1), add 4 to perimeter (since a square has 4 sides).
  7. Check the adjacent cells. If the cell above, below, to the left, or to the right is also land (1), subtract 1 from perimeter for each such cell (since that side is not part of the perimeter).
  8. After iterating through all cells, return perimeter.
  9. Make sure your Python file is executable by running chmod +x 0-island_perimeter.py in the terminal.
  10. Test your function with different grids to make sure it works correctly.

Remember to follow PEP 8 style guide and to document your module and function. Also, don't forget to add a new line at the end of your file.

This problem has been solved

Similar Questions

Install the Python Extension: Make sure you have the Python extension installed in Visual Studio Code. This extension provides support for Python development, including formatting capabilities.

python programs to create, read, write, append and delete a file

What extension must you add to the end of a Python source code file when saving?.xlsx.pptx.docx.py

All your files, classes and methods must be unit tested and be PEP 8 validated.guillaume@ubuntu:~/$ python3 -m unittest discover tests.............................................................................................................................................................................................----------------------------------------------------------------------Ran 189 tests in 13.135sOKguillaume@ubuntu:~/$Note that this is just an example. The number of tests you create can be different from the above example.Repo:GitHub repository: alx-higher_level_programmingDirectory: 0x0C-python-almost_a_circleFile: tests/

Which of the following is TRUE about Python?1 pointPython comments start with /*To get into Python in Ubuntu, type python at the command prompt.Python scripts are saved as .python files.Python requires indentation and it must be done consistently.

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.