Drill

ProblemsGo › network

How many pages does the result set need

easynetworkMathGo

A paginated API needs to know how many pages to render. Divide the total items by the page size and round up.

pagesTotal(items: int, perPage: int) → int

Go needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

func pagesTotal(items int, perPage int) int {
	
}

Worked examples

CallResult
pagesTotal(10, 3)4
pagesTotal(9, 3)3
pagesTotal(0, 5)0
pagesTotal(5, 0)0

Hint

The classic integer ceil is (items + perPage - 1) / perPage.

Reference solution in Go
func pagesTotal(items int, perPage int) int {
	if items <= 0 || perPage <= 0 {
		return 0
	}
	return (items + perPage - 1) / perPage
}

The same problem in another language

More network problems in Go