please use the python language, preferabally 2.7 for this, and you will be const
ID: 3874411 • Letter: P
Question
please use the python language, preferabally 2.7 for this, and you will be constructing if-elif-else statement to meet the condition of the tile that the grid would be asking. Please be sure it would work for all inputs. The parameters and answer template for the solution is also given beneath the screenshot below
Here below is answer template and parameter for this problem.
def getCorner(width, height):
width = width
height = height
#YOUR CODE GOES HERE (indented)
#END YOUR CODE
Explanation / Answer
Python 2.7 code:
def getCorner(width, heigth):
mystr= ""
matrix = []
tile_value = 1
for i in range(0,heigth):
row = []
for j in range(0,width):
row.append(tile_value)
tile_value = tile_value + 1
matrix.append(row)
if(width >=4 and heigth >= 4):
mystr += str((matrix[0][0])); mystr += " ";mystr += str(matrix[0][1]);mystr += " ";
mystr += str(matrix[0][width-2]);mystr += " ";mystr += str(matrix[0][width-1]);mystr += " ";
mystr += str(matrix[1][0]);mystr += " ";mystr += str(matrix[1][width-1]);mystr += " ";
mystr += str(matrix[heigth-2][0]);mystr += " ";mystr += str(matrix[heigth-2][width-1]);mystr += " ";
mystr += str(matrix[heigth-1][0]);mystr += " ";mystr += str(matrix[heigth-1][1]);mystr += " ";
mystr += str(matrix[heigth-1][width-2]);mystr += " ";mystr += str(matrix[heigth-1][width-1]);mystr += " ";
elif(width<=0 or heigth <= 0):
return "Invalid width or heigth, both heigth and widht must > 0"
else:
mystr += str((matrix[0][0])); mystr += " ";
if(str(matrix[0][width-1]) not in mystr):
mystr += str(matrix[0][width-1]);mystr += " ";
if(str((matrix[heigth - 1][0])) not in mystr):
mystr += str((matrix[heigth - 1][0])); mystr += " ";
if(str(matrix[heigth - 1][width-1]) not in mystr):
mystr += str(matrix[heigth - 1][width-1]);mystr += " ";
return mystr
print getCorner(7,10)
Sample Output:
1)
print getCorner(7,10)
1 2 6 7 8 14 57 63 64 65 69 70
2)
print getCorner(4,4)
1 2 3 4 5 8 9 12 13 14 15 16
3)
print getCorner(4,2)
1 4 5 8
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.