Question

Terrain analysis. Suppose that a terrain is represented by a two-dimensional grid of elevation values (in...

Terrain analysis. Suppose that a terrain is represented by a two-dimensional grid of elevation values (in meters). A peak is a grid point whose four neighboring cells (left, right, up, and down) have strictly lower elevation values. Compose a program peaks.py that reads a terrain from standard input and then computes and writes the number of peaks in the terrain. HAVE TO BE IN PYTHON PLEASE. using stdarray.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

below is the code for Terrain analysis. max function will give max of the passed arguments i had used max with 4 arguments max.

_________________________PYTHON 3.X code___________________________

n = int(input())   # for takin length of 2D array.
m = int(input())    # for takin length of 2D array.
arr= [] # for makin array itself
count=0 # count of peaks
for i in range(n): # loop for takin inputs
   arr.append(list(map(int,input().split()))) # takin all space saprated input and append list of integers in arr.

for i in range(n): # loop for travesing 2D array
   for j in range(m): # loop for traversing i and j.
      
       center = arr[i][j] # definition of cell   
       if i==0:    # check if i == 0 first row.
           if j == 0: # first element check
               if center > max(arr[i+1][j],arr[i][j+1]): #checking for DOWN and RIGHT cell only.
                  count+=1
           elif j == m-1:   # last element of first row.
               if center > max(arr[i+1][j],arr[i][j-1]): #checking for DOWN and LEFT cell only.
                   count+=1
           else:
               if center > max(arr[i+1][j],arr[i][j-1],arr[i][j+1]):   #checking for DOWN RIGHT and LEFT cells.
                   count+=1
       elif j == 0:    # for first column checking.
           if i == n-1: # last cell of first column.
               if center > max(arr[i-1][j],arr[i][j+1]): #checking for UP and RIGHT cell only.
                   count+=1
           else:   
               if center > max(arr[i-1][j],arr[i+1][j],arr[i][j+1]): #checking for UP DOWN and RIGHT cells.
                count+=1
      elif i == n-1: #checking cells of last row.
       if j == m-1: #checkin last row last element j = m-1.
              if center > max(arr[i-1][j],arr[i][j-1]): #checking for UP and LEFT cell only.
                   count+=1
           else :
               if center > max(arr[i-1][j],arr[i][j+1],arr[i][j-1]): #checking for UP RIGHT and LEFT cells..
                  count+=1
      elif j == m-1: # checking last column. first and last elements are checked in above conditions we will only check middle elements
           if center > max(arr[i-1][j],arr[i][j-1],arr[i+1][j]): #checking for UP DOWN and LEFT cells..
               count+=1
    else : #for all the cells which is not on the edge of the terrain.
           if center > max(arr[i-1][j],arr[i][j+1],arr[i][j-1],arr[i+1][j]): #checking for UP DOWN RIGHT and LEFT cells..
               count+=1
          
print("the number of peaks in the terrain = " ,count)   #output at console   

___________________INPUT Snippet _____________________________

circled one are the peaks. count is 8 in total.


  

__________________________OUTPUT Snippet________________________________________

Add a comment
Know the answer?
Add Answer to:
Terrain analysis. Suppose that a terrain is represented by a two-dimensional grid of elevation values (in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • **JAVA PLEASE!!** CSE205 Assignment 2 ASCII Terrain Generator 5Opts Topics: Arrays Multidimensional Arrays Metho...

    **JAVA PLEASE!!** CSE205 Assignment 2 ASCII Terrain Generator 5Opts Topics: Arrays Multidimensional Arrays Methods Loops and Conditionals Description The goal of this assignment is for you to produce a simple procedurally generated terrain map out of ASCII character symbols. This will be done through simple probability distributions and arrays Use the following Guideline s: Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc) Keep identifiers to a reasonably short length. User upper case for constants....

  • Write a JAVA program to solve a sudoku! Given a partially filled 9×9 2D array ‘grid[9][9]’,...

    Write a JAVA program to solve a sudoku! Given a partially filled 9×9 2D array ‘grid[9][9]’, the goal is to assign digits (from 1 to 9) to the empty cells so that every row, column, and subgrid of size 3×3 contains exactly one instance of the digits from 1 to 9. I have posted 3 input files: sudoku1.txt, sudoku2.txt and sudoku3.txt Problem Analysis & Design - Think about how you will need to solve this problem. You should do an...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT