Coding Curriculum - Python

Worksheet 5

In this worksheet, we will try to achieve the following:

  • Decide if a word is placed horizontally or vertically
  • Find a random column/row to place the word
  • Check if the word fits

We will break this down in the following steps:

  1. Placing the words horizontally or vertically.
  2. Making sure that you place every word in the grid.
  3. We do not simply want to print if a word is placed vertically or horizontally, but we actually want to place them so let's call those functions.
  4. Finding a column and a row to place the first letter in.
  5. Improve upon placing the word inside the grid
  6. Checking if the word actually fits.

Step 1: Horizontally or Vertically?

So first we have to decide if we will place the word horizontally or vertically and we can achieve this by using the random number generator that we covered during the lesson.

Since there are only 2 possibilities, it is a binary choice. That means that it can be represented by a 0 and a 1. For example a 0 could mean that the word is placed horizontally and 1 if it is placed vertically? So how would we do that? That's right, using randint(x, y) and if statement. But what values do x and y equal to?

Since the minimum value is 0 and the maximum value is 1, x = 0 and y = 1. Now try to write a function called placeWords that randomly prints "horizontal" or "vertical". Don't forget to use an if-else statement.

def placeWords():
number = randint(0, 1)
if number == 0:
#do something
else:
#do something else

Step 2: Placing every word in the grid

We will have to decide whether a word is placed horizontally or vertically for every single answer. Therefore let's make a loop that makes this decision for every single answer. Also, since the grid and the entries are generated by other functions, we can take them as parameters so we can use and change them.

def placeWords(grid, list):
for something in list:
number = randint(0, 1)
if number == 0:
#still do something
else:
#do something else

Step 3: Using other functions

Good job, this was the first part of this worksheet. Next, instead of printing horizontal or vertical, we actually want to find a spot to place the word. So lets make 2 more functions, called placeHorizontally and placedVertically and instead of printing, call those. They will each have to take the grid and the entry as a parameter, as the functions will need access to those variables. The structure should look like:

if number == 0:
firstFunction(parameters)
else:
secondFunctions(otherParameters)

Step 4: Placing the words inside the Grid

4) Placing a word inside the grid cannot be too hard either, can it? Try it out for yourself.

Hint: figure out how big the grid is and then we just use a random number generator to find a random column and row.

variable = randint(0, sizeofgrid)

Step 5: Improving our previous function

Sadly, it is not as easy as this because if we for example have a 3 x 3 grid, and we want to place the word "yes" inside it, using this method, we could get a result like row = 2 and column = 2. So if we try to fit the word "yes", we would insert 'y' into grid[2][2] and then '2' into grid[2][3]. This would give an out of bounds exception because the array is not that big. (Remember, we start counting from 0)

5) Therefore we have to find a different way. If we place the words horizontally, the row doesn't matter but the column does. We still need to leave enough space for the rest of the word. How can we do that?

variable = randint(0, len(answer) - 1)

Step 6: Checking if the word fits

Yeeees!! This works!! However we need to be sure that whenever we do this, we do not overwrite a word that is already in the grid. So we need to write some code for this. Since every entry in the initial grid contains '.', we can use this to our advantage. We check every spot where we would insert a letter and see if there is a '.', if not, we continue. Otherwise, we stop since we cannot fit the word in that position.

The first thing that we need is a loop that goes from the random column that we picked till the end of the word. Then we check if there is a dot in that place. If there is not, we set a variable to false (to indicate that the word does not fit) and we break out of the loop.

variable = True # assume that it fits
for i in range(column, column + len(entry["answer"])):
if grid[row][i] != '.':
variable = False
break
print(variable)

The Complete Code

If we put all that we have written together, it should look a little like:

def placeWords(grid, entries):
for answer in entries:
number = randint(0, 1)
if number == 0:
placeHorizontally(grid, answer)
else:
placeVertically(grid, answer)


def placeHorizontally(grid, entry):
column = randint(0, len(grid) - len(entry["answer"]))
row = randint(0, len(grid) - 1)

fits = True # assume that it fits
for i in range(column, column + len(entry["answer"])):
if grid[row][i] != '.':
fits = False
break
print(fits)

Well, this is the end of this worksheet. Now you are on your own and you should try to make the function placeVertically, which should look the same. In the next worksheet, we will finish this code so make sure that you fully understand this.