Coding Curriculum - Python

Worksheet 4

In this worksheet we are trying to construct the grid by breaking the problem down into smaller trunks, as we discussed earlier.

The problems that we will solve during this lesson are going to follow these steps:

  1. Finding the size of the Grid
  2. Storing information of the Grid
  3. Printing out information in the form of the Grid

First, we need to find the size of the grid. Let's assume that the answers are entered as arrays. So one of the strategies could be finding the answer with maximum length in the array.

Step 1. Finding the size for your Grid

Suppose you got the questions and answers in the source code in the form:

entries = [{"question":"…", "answer": "…"},
{"question":"…", "answer": "…"}]

Write a program to find the answer with maximum length.

(We might actually want to return a slightly higher value in case the amount of questions is greater than the maximum length. However we could prevent this easily by comparing number of questions against max length and take the higher value. Do not worry if this doesn't make sense yet, it will later on!)

Next, as we want to place the answer vertically and horizontally on the grid, we would design a grid with each side as long as the value returned from previous function.

This is tricky though because we would need to store each unit of length as a character so that we could replace them with characters later on.

Using an array consists of arrays might be the easiest approach here.

Hint: Have a look at example for len() function in lesson 4

Step 2. Putting information into the Grid

Extend the previous program to store each unit of length as a “.” i.e. with length of 3 the program returns [[“.”,”.”,”.”],[“.”,”.”,”.”],[“.”,”.”,”.”]]. So each “.” corresponds to a unit area. (The grid should be a square)

Next we will print the array stored in the previous exercise as grid.

Hint: The range() function might be useful to you here, the point is understanding the concept of arrays within an array.

Step 3. Printing out the information in the form of the Grid

Create a function to print the grid.

Hint: To print the grid, you may need to use join() function, remember in this case each array can be seen as a string so each of them represents a row

The Complete Code

entries = [
{"question":"What is the chemical composition of water?", "answer": "H2O"},
{"question":"What does Mg stand for?", "answer": "magnesium"},
{"question":"What type of bonding is there betwwn Fe atoms?", "answer": "metallic"},
{"question":"Which sub atomic particle has a positive charge?", "answer": "protons"},
{"question":"What are atoms with the same amount of protons but a different mass number?", "answer": "isotopes"},
{"question":"What does K stand for?", "answer": "potassium"},
]

# First we need to figure out how big the grid needs to be
def sizeOfGrid(entries):
maximum = len(entries[1]["answer"])
for item in entries:
if len(item["answer"]) > maximum:
maximum = len(item["answer"])
return maximum + int(len(entries)/3)

# Create the grid, initialize values with x

def createGrid(size):
grid = [['.' for i in range(size)] for x in range(size)]
return grid

def printGrid(grid):
for item in grid:
print " ".join(item)

printGrid(createGrid(sizeOfGrid(entries)))