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[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("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.
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.
-
\$\begingroup\$ Do you have test cases with this? \$\endgroup\$Mast– Mast ♦2020年10月25日 17:12:28 +00:00Commented Oct 25, 2020 at 17:12
-
\$\begingroup\$ @Mast yes. i'm going to edit the question with the test case included \$\endgroup\$naisuu42– naisuu422020年10月25日 22:28:48 +00:00Commented Oct 25, 2020 at 22:28
1 Answer 1
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 sonum
is a single number, but you can iterate it? Bad name. And wth doesasdf
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)
-
\$\begingroup\$ respect for the brutal honesty; i was incredibly tired when i was writing the code so i didnt put even an iota of thought into writing the variable/function names. \$\endgroup\$naisuu42– naisuu422020年10月26日 00:10:04 +00:00Commented Oct 26, 2020 at 0:10
-
1\$\begingroup\$ WAIT UR SOLUTION IS SO SMART WTF. jesus christ, mr. superb rain, you are incredibly cool \$\endgroup\$naisuu42– naisuu422020年10月26日 00:15:06 +00:00Commented Oct 26, 2020 at 0:15