DS&A

Data Structures and Algorithms

Exercises: Problem-Solving with “Plans”

Mar 192018

Before 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

  1. Find the sum of a list of numbers.
  2. Count how many numbers in a list are positive.
  3. Find the sum of only the positive numbers in a list of numbers.
  4. Given a list of numbers, find the index of a target number.
  5. In a list of numbers, find the index of the first positive value.
  6. Square the numbers in a list in-place.
  7. Square the numbers in a list, returning a new list.
  8. Count how many even values are in a list of integers.
  9. Given a list of numbers, calculate the sum of the squares.
  10. Find the sum of the even numbers in a list of integers.
  11. Calculate the sum of the odd squares of a list of integers.
  12. Find the index of the first string beginning with "the" in a list of strings.
  13. Given a list of numbers, find the number closest to 0.
  14. Find the maximum value from a list of numbers.
  15. Find the minimum value from a list of numbers.
  16. Given a list of strings, find the sum of their lengths.
  17. Given a list of strings, count how many begin with "a".
  18. Given a list of numbers, find the index of the first number greater than 100.
  19. Given a list of numbers, find the index of the minimum value.
  20. Given a list of strings, find the longest one.
  21. Given a list of strings, find the index of the shortest one.
  22. (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].
  23. (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}.
  24. (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.

New comment

Your comment will not appear until a moderator approves it.

Atom

hosted on werp.site