Skip to content

Instantly share code, notes, and snippets.

@yuheiomori
Created September 5, 2014 11:58
Show Gist options
  • Save yuheiomori/65164289bdda74e11b02 to your computer and use it in GitHub Desktop.
Save yuheiomori/65164289bdda74e11b02 to your computer and use it in GitHub Desktop.
Number Pairs (CodeEval) in python 3.x
# coding=utf-8
import sys
from itertools import combinations
def number_pairs(nums, x):
return filter(lambda t: sum(t) == x, combinations(nums, 2))
def display_number_pairs(pairs):
if pairs:
print(';'.join(['%s,%s' % p for p in pairs]))
else:
print('NULL')
def main():
with open(sys.argv[1], 'r') as f:
for line in f:
nums, x = line.rstrip().split(';')
pairs = number_pairs(map(lambda x: int(x), nums.split(',')), int(x))
display_number_pairs(pairs)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment