Skip to content

Instantly share code, notes, and snippets.

@Nagasaki45
Created March 13, 2014 12:33
Show Gist options
  • Save Nagasaki45/9527580 to your computer and use it in GitHub Desktop.
Save Nagasaki45/9527580 to your computer and use it in GitHub Desktop.
def find_items_to_buy(credit, items):
for i, item in enumerate(items[:-1]):
another = credit - item
if another in items[i + 1:]:
return i, items[i + 1:].index(another) + i + 1
assert (1, 2) == find_items_to_buy(100, [5, 75, 25])
assert (0, 3) == find_items_to_buy(200, [150, 24, 79, 50, 88, 345, 3])
assert (3, 4) == find_items_to_buy(8, [2, 1, 9, 4, 4, 56, 90, 3])
import sys
input_file = sys.argv[1]
with open(input_file) as f:
lines = f.read().splitlines()
cases = int(lines[0])
for i in range(cases):
credit = int(lines[1 + 3 * i])
items = lines[3 + 3 * i].split()
items = [int(item) for item in items]
a, b = find_items_to_buy(credit, items)
print('Case #{}: {} {}'.format(i + 1, a + 1, b + 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment