Question
PYHTON CODING FUNCTIONS HELP Part 2. Also need help with these last functions. Requirements/restraints and the map referred to is pictured in the screenshot. Need help with these 4 tasks:
Function 4:
def is_map(map) : given a map (see screenshot), does it meet the following criteria? 1) it is a list of lists of values of type int; 2) it has at least one row and one column; 3) it’s rectangular (all sub-lists are same length); 4) only non-negative ints are present (zero and up is accepted)
Assume: map is a list.
• is_map([[1,2,3],[0,1,5]) → True
• is_map([[[1,2,3,4],[5],[6,7]]) → False # not rectangular
• is_map([[-2,False, [1,2,3]], [4,5,6]]) → False #can't contain negatives or non-ints
Function 5:
def neighbors(map, r, c): Given a map and a location, there are up to eight possible locations immediately around it that might also exist on the map (unless along an edge or corner position). Build up a list of tuples, each tuple storing the (row, column) of the neighbors. They must be in order from lowest-row to later, lowest-column to later. This happens to mean the same ordering as they appear when the map is printed out. If the point isn't on the map, return an empty list!!
• Assume: map is a map as defined above, and r and c are int values.
• neighbors(map1,1,1) → [(0, 0), (0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1), (2, 2)]
• neighbors(map2,0,3) → [(0, 2), (0, 4), (1, 2), (1, 3), (1, 4)]
• neighbors(map2,2,0) → [(1, 0), (1, 1), (2, 1)]
Function 6:
def water_adjacent(map, r, c): Given a map and a location, are any of that spot's neighbors water? Water spots do not count themselves, only their neighbors. If (r,c) isn't on the map, the answer is False.
• Assume: map is a map as defined above, and r and c are int values.
• water_adjacent(map1,1,1) → True
• water_adjacent(map1,2,2) → False
• water_adjacent([[1,1,1],[1,0,1],[1,1,1]],1,1)→ False
Function 7:
def count_coastline(map): Given a map, how many spots are adjacent to water
• Assume: map is a map as defined above, and r and c are int values.
• count_coastline(map1) → 9
• count_coastline(map2) → 11
• count_coastline([[1,1,1],[1,0,1],[1,1,1]]) → 8
Background The purpose of this assignment is to practice building, inspecting, and modifying 2D lists effectively. This often requires nested for-loops, but not always. It involves thinking of the structure like an N x M matrix of labeled spots, each with a row-and column-index. As before, we are restricting ourselves to the most basic functionalities, and any others that you want, you should implement yourself. Two-dimensional lists arent conceptually more difficult than single-dimension lists, but in practice the nested loops, aliasing, and more complex traversals and interactions merit some extra practice and thought Project Basics document (part of assignment): http://cs.gmu.edu/-marks/112/projects/project basics,pdf Project Four tester file: http://cs.gmu.edu marks/112/projects/tester4p.py Grading Code passes shared tests: We11-commented/submitted: 1 TOTAL: 90 100 +5 extra credit What can I use? You may only use the following things. You may ask about adding things to a list, but we are unlikely to add anything. If you use something disallowed, its not an honor code violation, you just wont get points for any tests that use them. Restrictions no modules may be imported. you may only use built-in functions and methods explicitly listed below in the allowed section. list comprehensions and lambdas may not be used on this project Allowed basic statements, variables, operators, del, indexing, slicing, in, are all allowed any form of control flow weve covered is allowed (if else, loops, etc) only these built-in functions: range), len(), int), str, type(), list) only these built-in methods: s.split), s.join(), s.pop), xs.append(), xs.extend() xs.insert(), s.format() on extra credit: xs.sort() method (only when indicated!) calling other functions of the project (and your own defined helper functions). Please do this! e Hint In our solution, we only used range, len, type, pop(), and .append(). (And.sort() just on the extra credit as directed by the instructions). Remember: your grade is significantly based on passing test cases-try to completely finish individual functions before moving on. The easiest way to implement many functions is to call the earlierleasier functions, so itll pay off to complete functions before trying others. Dont let yourself be kind of done with a function, but miss all the tests!
media%2Fd36%2Fd362fd2c-832b-4037-b1d7-9f
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thanks for posting the question, we are glad to help you. Given the limited time, i could able to complete Function 4 and 5. Refer the screenshots for code indentation and output

Function 4

-------------------------------------------------------------------------------------------------------------------------------------

def is_map(map):
  
############################################
#1) it is a list of lists
if type(map) is list:
for row in map:
if type(row) is list:
continue
else:
return False
else:
return False
  
############################################
#2) it has at least one row and one column;
row_length=len(map)
col_length=len(map[0])
if row_length<1 or col_length<1:
return False
  
############################################
# it is a list of lists of values of type in
  
column_width=len(map[0])
for row in map:
# validates the width of each row is the same
if column_width==len(row):
pass
for col in row:
#4) only non-negative ints are present (zero and up is accepted)
col=str(col)
if col.isdigit():
continue
else:
return False
else:
return False
  
############################################
# if it passes all the test
return True


print(is_map([[1,2,3],[0,1,5]]))
print(is_map([[[1,2,3,4],[5],[6,7]]]))
print(is_map([[-2,False, [1,2,3]], [4,5,6]]))

-------------------------------------------------------------------------------------------------------------------------------------

far rma in rap row length len rap) enl.nn utdtm]Pn(#Plaj f cnl.tsdicit: Awiable are valabi etoryor s 0ick to review, 2ndintal updates

Function 5

------------------------------------------------------------------------------------------------------------------------------------

def neighbors(map, r, c):

rows=len(map)-1
cols=len(map[0])-1

if(r>rows or c>cols):
return []
start_row=0
end_row=0
start_col=0
end_col=0

if(r==0):
start_row=r
else:
start_row=r-1
if(r==rows):
end_row=r
else:
end_row=r+1
if(c==0):
start_col=0
else:
start_col=c-1
if(c==cols):
end_col=c
else:
end_col=c+1
  

  
index_list=[]
for row in range(start_row,end_row+1):
for col in range(start_col,end_col+1):
if row==r and col==c:
continue
index_list.append((row,col))

return index_list


print(neighbors([[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1]], 1, 1))
print(neighbors([[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1]], 0, 3))
print(neighbors([[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1]], 2, 0))

--------------------------------------------------------------------------------------------------------------------------------------

Image screenshot

Due to time constraint, i was not able to write code for 6 and 7. You, can follow the code for 4 and 5 and comeup with a solution for 6 and 7

thank you so much...........

Add a comment
Know the answer?
Add Answer to:
PYHTON CODING FUNCTIONS HELP Part 2. Also need help with these last functions. Requirements/restraints and the...
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
  • 1)def toggle_cell(row, column, cells): • Return value: Flip the cell in the column of row from...

    1)def toggle_cell(row, column, cells): • Return value: Flip the cell in the column of row from alive to dead or vice versa. Return True if the toggle was successful, False otherwise. • Assumptions: o cells will be a two-dimensional list with at least one row and one element in that row. o row and column are index values of cells • Notes: o Cells is being edited in place. Note that the return value is a Boolean and not a...

  • I need help as quick as possible, thanks beforehand. Please provide the test output The Lo...

    I need help as quick as possible, thanks beforehand. Please provide the test output The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown below. 35 The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 - 9 exactly The sum of each row, each column and each diagonal all add up to the same number. This is shown below: 15 4 92 15 - 81 + 15 15 15...

  • Encoding and Decoding PPM image 3 & 4 are parts of question 2 Code for a09q1:...

    Encoding and Decoding PPM image 3 & 4 are parts of question 2 Code for a09q1: We were unable to transcribe this image3 Encoding an Image in a09q2.py In the next four sections, you will no longer write class methods but rather functions. In each of the next four parts, you should be importing the PPM class from a09q1.py to use with the command from a09q1 import PPM Note that this is similar to how the check module was imported...

  • I need a python 3 help. Please help me with this question Part 2. Linked Lists...

    I need a python 3 help. Please help me with this question Part 2. Linked Lists You start working with the class LinkNode that represents a single node of a linked list. It has two instance attributes: value and next. class LinkNode: def __init__(self,value,nxt=None): assert isinstance(nxt, LinkNode) or nxt is None self.value = value self.next = nxt Before you start with the coding questions, answer the following questions about the constructor Valid Constructor or Not? LinkNode(1, 3) LinkNode(1, None) LinkNode(1,...

  • I only need the "functions" NOT the header file nor the main implementation file JUST the impleme...

    I only need the "functions" NOT the header file nor the main implementation file JUST the implementations for the functions Please help, if its difficult to do the complete program I would appreciate if you could do as much functions as you can especially for the derived class. I am a beginer so I am only using classes and pointers while implementing everything using simple c++ commands thank you in advanced Design and implement two C++ classes to provide matrix...

  • PLEASE HELP!!! C PROGRAMMING CODE!!! Please solve these functions using the prototypes provided. At the end...

    PLEASE HELP!!! C PROGRAMMING CODE!!! Please solve these functions using the prototypes provided. At the end please include a main function that tests the functions that will go into a separate driver file. Prototypes int R_get_int (void); int R_pow void R Jarvis int start); void R_fill_array(int arrayll, int len); void R_prt_array (int arrayl, int len) void R-copy-back (int from[], int to [], int len); int R_count_num (int num, int arrayll, int len) (int base, int ex) 3. Build and run...

  • Write a C++ program to simulate a stream path. In this problem you can make the...

    Write a C++ program to simulate a stream path. In this problem you can make the following assumptions 1. The elevation grid will be a n row, m column grid of integers, where n and m are constant. 2. Once the stream origin location is specified, then the stream flows to whichever of the four adjacent locations in the grid represents the largest descent. 3. If more than one of the adjacent locations have the largest descent, then the water...

  • Please help i need a C++ version of this code and keep getting java versions. Please...

    Please help i need a C++ version of this code and keep getting java versions. Please C++ only Purpose: This lab will give you experience harnessing an existing backtracking algorithm for the eight queens problem, and seeing its results displayed on your console window (that is, the location of standard output). Lab A mostly complete version of the eight queens problem has been provided for you to download. This version has the class Queens nearly completed. You are to provide...

  • This is a python question. Write a function mult_table(n) that consumes a natural number n and...

    This is a python question. Write a function mult_table(n) that consumes a natural number n and returns the n+1 by n+1 multiplication table (where each entry in the inner list is equal to the product of which list it is and the inner list position number, or in other words, the product of the row and column numbers). Using accumulative recursion, no abstract list functions. def mult_table(n) ''' Returns the n+1 by n+1 multiplication table    mult_table: Nat => (listof...

  • Need Part 1 and Part 2 please! also has to build and run, thank you! CSCI...

    Need Part 1 and Part 2 please! also has to build and run, thank you! CSCI 40 Computer Programming Methodology I Assignment 16 Part l Write a program that declares a two dimensional array and initialize it with following integers 2 4 3 81 45 2 10 3 6 20 21 12 28-5 7 11 2 4 75041 8 5 -10 3 2 0 Then 1. Write statements that display all the numbers in the array 2. Write statements that...

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
ADVERTISEMENT