Created
September 5, 2014 11:58
-
-
Save yuheiomori/65164289bdda74e11b02 to your computer and use it in GitHub Desktop.
Number Pairs (CodeEval) in python 3.x
This file contains hidden or 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
# 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