Skip to content

Instantly share code, notes, and snippets.

@chipbell4
Created August 22, 2014 02:36
Show Gist options
  • Save chipbell4/ef0457c6b16cd30117cf to your computer and use it in GitHub Desktop.
Save chipbell4/ef0457c6b16cd30117cf to your computer and use it in GitHub Desktop.
My Missing Pages solution (Practice 8/21/2014)
def pagesTaken(N, P):
original_p = P
# adjust P to be less than N/2
if P > N/2:
P = N - P + 1
# if even, move back a page
if P % 2 == 0:
P -= 1
# Note: At this point, P is the smallest page on the same page as original_p
# Here's a neat Python 3 thing: set literals. I'm actually doing a set difference here.
# I'm creating a set of all elements on a single page, and then removing the page that
# was already provided, since we're not supposed to print that one.
removed_pages = { P, P + 1, N - P + 1, N - P } - { original_p }
# Lastly, sort for good measure
return sorted(removed_pages)
while True:
line = input()
if line == '0':
break
N, P = map(int, line.split())
# Sorry this is jumbled, but it works.
print(' '.join(map(str, pagesTaken(N, P))))
# I'll split it up here, so you know what's going on, but I haven't tested this:
pages_taken = pagesTaken(N, P) # This is a list of ints (from the sorted call)
str_pages_taken = map(str, pages_taken) # Now its a "map" object of strings
output = ' '.join(str_pages_taken) # Squishing the strings together by ' '. So ['1', '2'] becomes '1 2'
print(output) # Print it out.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment