The above diagram shows a standard example of precision and recall in a document retrieval setting.
To calculate the average precision for rank 1 you would just do:
(1.0 + 0.67 + 0.75 + 0.8 + 0.83 + 0.6) / 6 = 0.78
The example above is great for small document collections but say you have a search engine with 100,000s of documents and a query could have 100 of relevant documents. How would the above be adapted if you kept the length of K at 10?
An example:
It has been decided that the query for Ranking #1 has 20 relevant documents does the above become:
(1.0 + 0.67 + 0.75 + 0.8 + 0.83 + 0.6) / 20 = 0.23
or do you still divide by 6 because that is the number of relevant documents within the rank of length K?
You divide by the total number relevant |R| even if it is larger than your cutoff, K.
This seems a little silly, but imagine that your system only returned 10 documents instead of you choosing to cut off the ranking at that point. AP wants to "punish" this system in comparison to one that retrieves more documents.
In traditional IR evaluation, you set K=1000 when you calculate AP, and usually |R| is smaller than 1000. In the homework/textbook example you listed, the goal is to calculate by hand, so they have a very small K, but in computerized evaluation, you want as large a K as is reasonable.
There are other ranking measures that don't have this "problem" of the max not being 1 in all situations, i.e., NDCG@K, which is quite similar to AP, except that it specifically is normalized, i.e., it will always output 1 for the best possible ranking at K, and 0 for the worst possible ranking. This normalization to the best possible ranking is more intuitive to explain to people than the recall-points of MAP, but the measures are highly correlated in real life.