I was trying to figure out this problem, and I did. However, my code is so abhorrently ugly that I want to tear out my eyeballs when I look at it:
with open("gymnastics.in", "r") as fin: rounds, cows = [int(i) for i in fin.readline().split()] nums = [tuple(map(int, line.split())) for line in fin] def populatePairs(cows): pairs =  for i in range(cows): for j in range(cows): if i != j: pairs.append((i+1,j+1)) return pairs def consistentPairs(pairs, nums): results =  for i in pairs: asdf = True for num in nums: for j in num: if j == i: asdf = False break if j == i: break if not asdf: break if asdf: results.append(i) return results pairs = populatePairs(cows) with open("gymnastics.out", "w+") as fout: print(len(consistentPairs(pairs, nums)), file=fout)
I feel like that there should definitely be a better solution that is faster than \$O(n^3)\$, and without the triple nested for-loop with the if-statements trailing behind them, but I cannot, for the love of god, think of a better solution.
Given an \$n\$ by \$m\$ grid of points, find the number of pairs in which number is consistently placed before the other.
3 4 4 1 2 3 4 1 3 2 4 2 1 3
Explanation: The consistent pairs of cows are (1,4), (2,4), (3,4), and (3,1), in which case 4 is consistently greater than all of them, and 1 is always greater than 3.