I think this problem should be easy to everyone. When I started to solve this problem, I have switched to Python 3 from 2, so there were several technical difficulties, including:
- in python 3, next() is no longer generator’s method. Instead, you should do next(generator)
- in python 3, print “blah” is no longer possible. print(“blah”) is correct.
def get_stream(file): for line in file: for token in line.strip().split(): yield token def get_int(stream): return int( next(stream) ) def open_file(file): return open(file, "r") s = get_stream( open_file("A-large-practice.in") ) o = open("A-large-practice.out", "w") num_of_cases = get_int(s) for i in range(0, num_of_cases): num_of_pairs = get_int(s) pairs = [] for pair in range(0, num_of_pairs): pairs.append( (get_int(s), get_int(s)) ) pairs = sorted( pairs, key=lambda x: x[0], reverse=True) crossing = 0 for k in range(0, num_of_pairs-1): crossing += len( list( filter(lambda x: x[1] > pairs[k][1], pairs[k+1:]) ) ) o.write("Case #{}: {}\n".format(i+1, crossing)) o.close()