Most efficient solution for USACO: Cow Gymnastics - Python 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(&quot;gymnastics.in&quot;, &quot;r&quot;) 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[1]:
                  asdf = False
                  break
              if j == i[0]:
                  break
          if not asdf:
              break
      if asdf:
          results.append(i)

  return results

pairs = populatePairs(cows)

with open(&quot;gymnastics.out&quot;, &quot;w+&quot;) 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.
Problem synopsis:
Given an \$n\$ by \$m\$ grid of points, find the number of pairs in which number is consistently placed before the other.
Example:
Input:
3 4
4 1 2 3
4 1 3 2
4 2 1 3

Output:
4
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.
Suggestions:

Use the variable names from the problem specification instead of inventing your own (just make them lower-case since that's preferred in Python code). Makes it easier to see the connections. Unless yours are much better.
I guess nums is plural so num is a single number, but you can iterate it? Bad name. And wth does asdf mean?
Python prefers snake_case for function names.
To be honest, the lack of any explanation of your method, the name asdf and the highly convoluted code made me give up reading it. But here's my solution, simply counting occurring pairs and then the result is the number of pairs that appeared K times:

from itertools import combinations
from collections import Counter

ctr = Counter()
with open('gymnastics.in') as f:
    k, _ = map(int, next(f).split())
    for session in f:
        cows = session.split()
        ctr.update(combinations(cows, 2))

with open('gymnastics.out', 'w') as f:
    print(list(ctr.values()).count(k), file=f)