Created
January 30, 2014 23:50
-
-
Save kevinmcconnell/8722643 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PAGE_SIZE = 5 | |
EVENTS = %w(a a b c b c a a a c b c b b b c a a c a a a c b b b c b c a a a c) | |
EVENTS_WITH_TIMES = EVENTS.map.with_index { |id, i| [id, i] } | |
PARTS_A = EVENTS_WITH_TIMES.select { |entry| entry.first == 'a' } | |
PARTS_B = EVENTS_WITH_TIMES.select { |entry| entry.first == 'b' } | |
PARTS_C = EVENTS_WITH_TIMES.select { |entry| entry.first == 'c' } | |
def get_page(start_at) | |
parts_a_old_enough = PARTS_A.select { |entry| entry.last > start_at } | |
parts_b_old_enough = PARTS_B.select { |entry| entry.last > start_at } | |
parts_c_old_enough = PARTS_C.select { |entry| entry.last > start_at } | |
combined = parts_a_old_enough + parts_b_old_enough + parts_c_old_enough | |
sorted = combined.sort_by(&:last) | |
limited = sorted[0...PAGE_SIZE] | |
limited | |
end | |
def check_page(page, expected) | |
puts expected == page.map(&:first).join(' ') | |
end | |
page1 = get_page(-1) | |
page2 = get_page(page1.last.last) | |
page3 = get_page(page2.last.last) | |
check_page(page1, 'a a b c b') | |
check_page(page2, 'c a a a c') | |
check_page(page3, 'b c b b b') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment