Back to Contents

# Hints for Questions

## 1. Starting Off

1

Try to work these out on paper, and then check by typing them in. Can you show possible steps of evaluation for each expression?

2

Type it in. What does Python print? Consider the precedence of `+` and `*`.

## 2. Names and Functions

2

What does the function take as arguments? You can use the `!=` operator and the `and` keyword here.

3

The function will have three arguments i.e. `def`` volume(w, h, d):`

4

Can you define this in terms of the `is_vowel` function we have already written?

5

When does it not terminate? Can you add a check to see when it might happen, and return 0 instead? What is the factorial of 0 anyway?

6

What is the sum of all the integers from 1…1? Perhaps this is a good start.

7

What happens when you raise a number to the power 0? What about the power 1? What about a higher power?

8

You can use an additional argument to keep track of which number the function is going to try to divide by.

## 3. Again and Again

1

Make sure to consider how the start and stop arguments are defined at the beginning of this chapter.

3

You will need a local variable to store the count.

6

The `input` function with an argument, and using the `\n` newline sequence might look like this:

``````entered =

To remove the `entered` variable, we can do the `input` inside the `while` loop’s test itself.

7

You might need three variables: the chosen secret number, the current guess, and the number of tries so far. Remember the `int` function can convert a string to an integer.

8

This is just a big `if` construct. Make sure not to output both a letter space and a word space at the end of a word – just a word space.

## 4. Making Lists

2

Try making a fresh, empty list, and then putting items from the original list into it one by one. Maybe you could use the `insert` method to put them in a particular place.

3

What initial value can we use for keeping track of both the maximum number seen and the minimum number seen?

6

Start from a fresh, empty list. Then, looking at each element of the original list, decide whether it should go into the new list or not.

7

You can use the `setify` function you have already written to find the unique items, and then the `count` method to find out how many of each appear in the input list.

11

The rotation may be achieved by slicing.

13

The problem may be split into two. First identify “correct numbers in the correct place” then “correct numbers in incorrect place”. In the latter stage, be careful not to use any position in the code identified in the first stage, nor to use a position twice.

## 5. More with Lists and Strings

1

We already know how to make the list of words with `split`.

4

Just a function to remove spaces from the beginning of a list is required; the rest can be done with list reversal.

9

Remember that a list comprehension can have an `if` part as well as a `for` part.

## 6. Prettier Printing

1

Remember not to add a comma or space after the final item. We did this once before, in chapter 3 question 4.

3

Recall that `print` can take multiple values.

5

How will the user signal that they have no more names to type in?

## 7. Arranging Things

1

Remember that we can assign to a tuple using tuple unpacking: `a, b = ...`

2

The `items` method, described in the chapter, can be used to iterate on two variables at once with `for`` k, v = ...`

5

Consider the indices when deletion happens.

6

The `items` method, described in the chapter can be used to iterate on two variables at once with `for`` k, v = ...`

7

Remember that `set` can build a set of the letters in a string.

## 8. When Things Go Wrong

2

The technique is to use map to build a list (possibly containing `None` values), then filter it to remove them.

## 9. More with Files

3

What happens if `int` cannot proceed because the file is malformed? How will you know when all the entries have been read?

5

The `split` method works, of course, equally well on numbers as on words.

7

A dictionary is suitable for storing a histogram. When do we need to add a new entry? When do we need to increment an existing entry?

## 10. The Other Numbers

1

Consider the two functions `math.ceil` and `math.floor`.

3

Consider the function `math.floor`. What should happen in the case of a negative number?

4

Calculate the column number for the asterisk carefully. How can it be printed in the correct column?

5

You will need to call the `star` function with an appropriate argument at points between the beginning and end of the range, as determined by the step.

## 11. The Standard Library

2

The string `string.digits` is `’0123456789’`.

5

The use of `time.sleep` is to provide a count-down for the user.

## 12. Building Bigger Programs

1

The two cases (a maximum is provided, and is not provided) may be distinguished by testing the length of the `sys.argv` list.

2

The `plot` function requires a function to be passed to it. We can build such a function from the argument provided on the command line, using the `eval` function.

## Project 1: Pretty Pictures

3

The number of segments used to approximate a circle ought to be related to its circumference not its radius.

5

There are three dimensions to the gamut: red, green and blue. Since we cannot display these on a 2D screen, you must find a way to ‘flatten‘ the space out.

Project 1A

We need to build a function which can be called repeatedly to evaluate an expression from the command line, in terms of x. Can you write a function to return such a function?

Project 1B

Recall that `turtle.Screen().tracer(0, 0)` turns off animation, and that you will need `turtle.Screen().update()` to show the clock face when you have finished drawing it, and that `time.sleep` may be used to wait for the next time we need to draw a clock.

## Project 2: Counting Calories

1

Remember that the `items` method may be used to iterate over the keys and values of the table resulting from `table_of_file`.

2

The dates will sort as if they were in alphabetical order – the digits too are ordered.

1

If a table lookup fails, `None` is returned.

## Project 3: Noughts and Crosses

3

The `random_move` function will need a mechanism to pick a random blank space. You might repeatedly pick spaces until one is found to be blank, though this may be inefficient when few spaces remain.

Another way would be to pick a random place to start, and then cycle through the positions in turn until a blank one is found.

6

Our `try_to_take` function is useful here.

8

Draw out diagrams of the situations described by the rules. You will need a list of intersecting lines and the points at which they intersect.

9

To calculate the total number of boards, remember only to count ones which are full or won.

10

The function must traverse the whole tree, counting once for each board for which the function passed to it returns `True`.

11

The tree may be represented by nested tuples of three items each, representing the data, the left branch and the right branch.

## Project 4: Photo Finish

1

There is no right or wrong answer for the formulae for brightness and contrast. Design them to produce a sensible result for a sensible input.

2

Rotation may be achieved by a combination of flips.

4

Consider how the blurring operation spreads the colour of a pixel around: how do we make sure we lose none?