Examples [code] [code]
- User
Examples
2 1 2 2 1 4 8 9 23 12 Assistant
First, I need to understand what's being asked: ## Examples
2 1 2 2 1 4 8 9 2
2
1 2 2 1
4 8 9 2
3
12
First, I need to understand what's being asked: ## Examples
2
1 2 2 1
4 8 9 2
3
12
Key values given: 2, 1, 2, 2, 1, 4, 8, 9
Working through the calculation: The problem can be inferred from the examples:
Given an integer T (number of test cases), followed by T lines each containing four integers, the task is to output for each test case the sum of the two middle numbers after sorting the four numbers (equivalently, the total sum minus the minimum and the maximum). Approach
total - min - max. Python Implementationimport sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
t = int(next(it))
out_lines = []
for _ in range(t):
read four integers
a = int(next(it)); b = int(next(it)); c = int(next(it)); d = int(next(it))
total = a + b + c + d
mn = min(a, b, c, d)
mx = max(a, b, c, d)
out_lines.append(str(total - mn - mx))
sys.stdout.write("\n".join(out_lines))
if __name__ == "__main__":
solve()
This gives us the final answer: sum minus the minimum and the maximum).
The problem can be inferred from the examples:
Given an integer T (number of test cases), followed by T lines each containing four integers, the task is to output for each test case the sum of the two middle numbers after sorting the four numbers (equivalently, the total sum minus the minimum and the maximum).
total - min - max.This works for any integers, including duplicates and negative values.
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
t = int(next(it))
out_lines = []
for _ in range(t):
# read four integers
a = int(next(it)); b = int(next(it)); c = int(next(it)); d = int(next(it))
total = a + b + c + d
mn = min(a, b, c, d)
mx = max(a, b, c, d)
out_lines.append(str(total - mn - mx))
sys.stdout.write("\n".join(out_lines))
if __name__ == "__main__":
solve()