Round #876
11 May 2026, 09:55· csharp· Meta-Llama-3.3-70B-Instruct· r15-firing-v3
- Complexity
- band 0
- ZP / UO / OBS
- 3 / 2 / 3
- Journey weight
- 525
- Lightshift ΔL
- —
Prompt
Write a generic method 'MaxBy<T,TKey>(IEnumerable<T>, Func<T,TKey>) where TKey:IComparable<TKey>' that returns the element with the maximum key.
Zero-point index (what the engine surfaced)
ZPI-ZPI-0008 | 2026-05-11T09:55:50Z
Write a generic method 'MaxBy<T,TKey>(IEnumerable<T>, Func<T,TKey>) where TKey:IComparable<TKey>' that returns the element with the maximum key.
ZERO POINT — 3 entries (structural invariants)
● hsl(7.18, 100%, 50%) class [feature]
● hsl(0.00, 100%, 50%) public [feature]
● hsl(84.10, 100%, 50%) namespace [feature]
UNOBSERVED — 2 entries (implied truths)
○ hsl(340.51, 100%, 22%) IEnumerable of T [feature]
○ hsl(15.38, 100%, 22%) var [feature]
OBSERVED — 3 entries (explicit in request)
◉ hsl(65.64, 100%, 15%) where [feature]
◉ hsl(88.21, 100%, 15%) where [feature]
◉ hsl(98.46, 100%, 50%) with [feature]
COMPLEXITY │ 8 entries │ spread: 325° │ band: LOW
Baseline response (no ZPI)
### Generic Method to Find Maximum Element by Key
Here's a C# method that uses LINQ to find the maximum element in a collection based on a specified key.
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
public static class EnumerableExtensions
{
/// <summary>
/// Returns the element with the maximum key.
/// </summary>
/// <typeparam name="T">Type of the elements in the collection.</typeparam>
/// <typeparam name="TKey">Type of the key.</typeparam>
/// <param name="source">The collection to search.</param>
/// <param name="keySelector">A function to extract the key from each element.</param>
/// <returns>The element with the maximum key.</returns>
public static T MaxBy<T, TKey>(this IEnumerable<T> source, Func<T, TKey> keySelector)
where TKey : IComparable<TKey>
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (keySelector == null)
{
throw new ArgumentNullException(nameof(keySelector));
}
return source.Aggregate((max, current) =>
keySelector(max).CompareTo(keySelector(current)) > 0 ? max : current);
}
}
```
### Example Usage
Here's an example of how to use this method:
```csharp
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main()
{
var people = new List<Person>
{
new Person { Name = "John", Age = 25 },
new Person { Name = "Alice", Age = 30 },
new Person { Name = "Bob", Age = 20 },
};
var oldestPerson = people.MaxBy(p => p.Age);
Console.WriteLine($"The oldest person is {oldestPerson.Name} with age {oldestPerson.Age}");
}
}
```
This will output:
```
The oldest person is Alice with age 30
```
This code defines a `MaxBy` method that takes an `IEnumerable<T>` and a `Func<T, TKey>` as parameters. The `Func<T, TKey>` is used to extract the key from each element in the collection. The method then uses the `Aggregate` method to find the element with the maximum key.
Note that this method throws an exception if the input collection is empty. If you want to handle this case differently, you can modify the method to return `default(T)` or throw a custom exception.
Also, this method uses the `CompareTo` method to compare the keys. This means that the keys must implement the `IComparable<TKey>` interface. If the keys do not implement this interface, you will need to modify the method to use a different comparison method.
Enhanced response (with ZPI)
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
public static class EnumerableExtensions
{
/// <summary>
/// Returns the element with the maximum key.
/// </summary>
/// <typeparam name="T">The type of the elements in the sequence.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The sequence of elements.</param>
/// <param name="keySelector">A function to extract the key from each element.</param>
/// <returns>The element with the maximum key.</returns>
/// <exception cref="ArgumentNullException">Thrown if the source or keySelector is null.</exception>
/// <exception cref="InvalidOperationException">Thrown if the source sequence is empty.</exception>
public static T MaxBy<T, TKey>(this IEnumerable<T> source, Func<T, TKey> keySelector)
where TKey : IComparable<TKey>
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (keySelector == null)
{
throw new ArgumentNullException(nameof(keySelector));
}
return source.Aggregate((max, current) =>
keySelector(max).CompareTo(keySelector(current)) > 0 ? max : current);
}
}
```
This method uses the `Aggregate` method to iterate over the sequence and keep track of the element with the maximum key. It throws an exception if the source sequence is empty, as there is no maximum element in this case. The `where TKey : IComparable<TKey>` constraint ensures that the key type implements the `IComparable<T>` interface, which is required for comparing keys.
Note that if there are multiple elements with the same maximum key, this method will return the first one it encounters. If you need to return all elements with the maximum key, you would need to modify the method accordingly.
This implementation requires more text to explain the usage and potential edge cases.
Grader chain
Each grader reads the same baseline + enhanced pair and casts an independent verdict. Disagreement is the framework's own signal — not noise to be averaged away.
V1samba/Meta-Llama-3.3-70B-Instruct
BetterV4qwen2.5-coder-3b-instruct
BetterV5openrouter/qwen/qwen3-235b-a22b-2507
BetterV7qwen2.5-coder-7b-instruct
SameV8openai/gpt-4.1
SameV9anthropic/claude-opus-4-7
SameV12openai/gpt-4o
SameComments
Our grader said what it said. What do you say? Comment as a guest below.
No comments yet. Be the first to say what you make of this round.