Specification: Find the name of the youngest person older than 35, returning null if none exists.
Example input/output:
Input:
people
age | name |
---|---|
35 | John |
36 | Mary |
37 | Jane |
Output:
Mary
Python - Imperative
def youngest_over_35(people): over_35 = [] for person in people: if person['age'] > 35: over_35.append(person) youngest = None for person in over_35: if youngest is None or person['age'] < youngest['age']: youngest = person return youngest['name'] if youngest is not None else None
Python - Functional
def youngest_over_35(people): over_35 = [p for p in people if p['age'] > 35] youngest = min(over_35, default=None, key=lambda p: p['age']) return youngest['name'] if youngest is not None else None
Python - Pandas
def youngest_over_35(people): over_35 = people[people.age > 35] if len(over_35) == 0: return None else: youngest = over_35.loc[over_35.age.idxmin()] return youngest['name']
R - Tidyverse
youngest_over_35 <- function(people) { people %>% filter(age > 35) %>% slice(which.min(age)) %>% pull(name) }
SQL - SQLite
SELECT name FROM people WHERE age = ( SELECT MIN(age) FROM people WHERE age > 35) LIMIT 1
Datalog - Souffle
youngest_over_35(Name) :- people(Age, Name), Age = min Age : { people(Age, _), Age > 35 }.
Q - kdb+
old_enough: select from people where age > 35 youngest_over_35: (first select name from old_enough where age = min(age)) `name