Pages and Chapters

Published by Deep Xavier in

Write a function that returns the closest chapter to the current page you are at. If two chapters are similarly close, return whichever has the higher page.

Examples

closestToPage(new ArrayList<Map.Entry<String, Integer> >(
  Arrays.asList(
      new SimpleEntry<String, Integer>("Chapter 1", 1),
      new SimpleEntry<String, Integer>("Chapter 2", 15),
      new SimpleEntry<String, Integer>("Chapter 3", 37)  
)), 10) ➞ "Chapter 2"


closestToPage(new ArrayList<Map.Entry<String, Integer> >(
  Arrays.asList(
      new SimpleEntry<String, Integer>("Chapter 1", 1),
      new SimpleEntry<String, Integer>("Chapter 2", 15),
      new SimpleEntry<String, Integer>("Chapter 3", 37)
)), 200) ➞ "The End?"


closestToPage(new ArrayList<Map.Entry<String, Integer> >(
  Arrays.asList(
      new SimpleEntry<String, Integer>("Chapter 1a", 1), 
      new SimpleEntry<String, Integer>("Chapter 1b", 5)
)), 3) ➞ "Chapter 1b"

Notes

  • All page numbers in the book are valid integers.
  • Return the higher page number if ever two pages are equidistant (see test case #8).
  • The challenge introduces the use of Map.Entryand SimpleEntry classes in lieu to Map and HashMap.
  • Another version of this challenge which uses custom classes instead of Map-types can be found via this link.
Watch a quick demo on how Edabit works.