Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write python code for this prompt: NOTE: This assignment is meant to be completed over three weeks. As such, it will likely be longer and

Write python code for this prompt: NOTE: This assignment is meant to be completed over three weeks. As such, it will
likely be longer and more complicated than past assessments. We recommend you start on
this assessment early and not wait until after the end of Spring Break to begin work on it.
Introduction
In this assessment youll use functions and nested loops to write code to analyze a grid
of black-and-white pixels to determine which letter it most resembles.
Problem
Computers have only recently been able to reliably recognize writing, and still cant
in certain difficult conditions. For this assignment, you will code a simplified version of a
writing recognition system. This code will only deal with single letters at a time (a much
more approachable task than reading general writing).
Importantly, letter data should always be considered in a 5x5 grid of black and white
pixels. In practical terms, this grid will be a list of lists, with each row of the grid represented
by its own sublist. Each pixels value will be 0(for a black pixel) or 1(for a white one).
e.g. the following list:
# list formatted for readability
letter_grid =[
[1,1,0,1,1],
[1,0,1,0,1],
[1,0,0,0,1],
[1,0,1,0,1],
[1,0,1,0,1]
]
would be rendered as a black-and-white image like so:
Figure 1: A capital A in a 5x5 pixel grid
1
Your code will determine which letter best matches the given input grid by comparing
two grids together: one grid that is unknown and one grid that represents a known letter.
You will assign each of these comparisons a score that is how many pixels the two grids
have in common.
For example, take the following input image of an unknown letter.
Figure 2: A pixel grid with an (as yet) unidentified letter.
We can compare how many pixels this grid has in common with a known A and a known
B grid.
Figure 3: Two known letter grids for A and B.
The unknown grid has 24 pixels that match with the A grid and only 18 pixels in
common with the B grid. Thus, we would conclude the unknown grid is an A because it
has the highest match score.
Using a given list of letter grids, you will write code to manage and make these compar-
isons. Like with the last assessment, you will accomplish this through a series of functions
you will define and implement.
2
Required Functions
get input grid()
Parameters
There should not be any parameters for this function
Returns
This function should return EITHER a list of lists of integers or a single integer, de-
pending on what is collected by the function through user input, i.e. the input() function.
This input will normally be a series of grid rows, with pixel values separated by spaces. For
example, this function could cause the following interaction and would return the list in the
Example Usage section below.
ROW1>11011
ROW2>10101
ROW3>10001
ROW4>10101
ROW5>10101
However, if the user inputs DONE at any point then this function should immediately halt
and return -1 instead.
ROW1>11011
ROW2>10101
ROW3> DONE # Function stops asking for input and returns a -1
Description
Unlike last week, this function will need to make use of the input() function to collect
user input. You should accept at most 5 lines of input, and convert each input line into a list
of integers. Besides checking for DONE, this function should not do any input validation.
That will be done by the following function.
Example usage
pixel_grid = get_input_grid()
#assuming the user inputs the rows in the example above
print(pixel_grid) # prints: [[1,1,0,1,1],
# list formatted for readability [1,0,1,0,1],
# [1,0,0,0,1],
# [1,0,1,0,1],
# [1,0,1,0,1]]
3
grid is valid(pixel grid)
Parameters
1. A list of lists of integers
Returns
This function should return a Boolean value indicating if the pixel grid parameter meets
the format requirements for analysis.
Description
The format requirements are:
The grid must be a list of exactly five lists
Each nested list must contain exactly 5 integers, each with values of 0 or 1
If the provided grid fails to meet these requirements, this function should return False.
Example usage
pixel_grid =[[1,1,0,1,1],
[1,0,1,0,1],
[1,0,0,0],
[1,0,1,0,1],
[1,0,1,0,1]]
is_valid = grid_is_valid(pixel_grid)
print(is_valid) # False
4
match score(pixel grid, letter grid)
Parameters
1. A list of lists of integers
2. A list of lists of integers
You can assume these parameters have already been validated, and thus both will be exactly
5x5 grids of integers that are 0 or 1.
Returns
This function should return an integer representing how many individual pixel values
from pixel grid matched pixels in the same location from letter grid.
Description
This function will need to loop over the pixel locations in the provided pixel grid, and
count how many individual values match with values in letter gri

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Michael V. Mannino

3rd Edition

0071107010, 978-0071107013

More Books

Students also viewed these Databases questions