## Exercises: Problem-Solving with “Plans”

Mar 192018Before attempting these exercises, you should read the posts on specifying problems, problem-solving and algorithmic “plans”.

For each of the problem statements below:

- Highlight the key words to determine the
*inputs*and*outputs*. - Identify the subproblems.
- Select plans to solve the subproblems.
- Write a function to solve the problem, by combining the plans in code.
- Test your solution with example inputs.

Complete as many as you can. The “challenges” may require unusual combinations of plans, or for you to invent a plan yourself.

### Exercises

- Find the sum of a list of numbers.
- Count how many numbers in a list are positive.
- Find the sum of only the positive numbers in a list of numbers.
- Given a list of numbers, find the index of a target number.
- In a list of numbers, find the index of the first positive value.
- Square the numbers in a list in-place.
- Square the numbers in a list, returning a new list.
- Count how many even values are in a list of integers.
- Given a list of numbers, calculate the sum of the squares.
- Find the sum of the even numbers in a list of integers.
- Calculate the sum of the odd squares of a list of integers.
- Find the index of the first string beginning with
`"the"`

in a list of strings. - Given a list of numbers, find the number closest to 0.
- Find the maximum value from a list of numbers.
- Find the minimum value from a list of numbers.
- Given a list of strings, find the sum of their lengths.
- Given a list of strings, count how many begin with
`"a"`

. - Given a list of numbers, find the index of the first number greater than 100.
- Given a list of numbers, find the index of the minimum value.
- Given a list of strings, find the longest one.
- Given a list of strings, find the index of the shortest one.
- (Challenge:) Compute all the running totals from a list of numbers, returning the results in a new list.
For example,
`[1, 1, 2, -1]`

has the running totals`[0, 1, 2, 4, 3]`

. - (Challenge:) Count the occurrences of each distinct string in a list, returning the result as a dictionary.
For example,
`['a', 'b', 'a', 'b', 'c', 'b']`

gives`{'a': 2, 'b': 3, 'c': 1}`

. - (Challenge:) Count how many pairs of duplicate values are in a list of strings.
For example,
`['a', 'b', 'a', 'b', 'c', 'b']`

has one pair of`'a'`

and three pairs of`'b'`

, giving a total of four pairs.

## There are no published comments.