
"""
# This was GPT4:s first try which is not correct since it
# printed False on all instances
def grandparent(x, y, relationships):
    for rel1 in relationships:
        if rel1[0] == x:
            for rel2 in relationships:
                if rel2[0] == rel1[2] and rel2[2] == y:
                    return True
    return False


relationships = [
    ("mother", "ann", "amy"),
    ("mother", "ann", "andy"),
    ("mother", "amy", "amelia"),
    ("mother", "linda", "gavin"),
    ("father", "steve", "amy"),
    ("father", "steve", "andy"),
    ("father", "gavin", "amelia"),
    ("father", "andy", "spongebob"),
]

# Test cases
print(grandparent("ann", "amelia", relationships))  # True
print(grandparent("steve", "amelia", relationships))  # True
print(grandparent("ann", "spongebob", relationships))  # True
print(grandparent("steve", "spongebob", relationships))  # True
print(grandparent("linda", "amelia", relationships))  # True
print(grandparent("amy", "amelia", relationships))  # False
"""

"""
### Second try: The output is False on all positive examples and True
### on the negative,
def grandparent(data, x, y):
    def parent(p, c):
        return (p, c) in data['mother'] or (p, c) in data['father']

    for potential_parent in data['mother'] + data['father']:
        if parent(x, potential_parent[1]) and parent(potential_parent[0], y):
            return True
    return False


data = {
    'mother': [('ann', 'amy'), ('ann', 'andy'), ('amy', 'amelia'), ('linda', 'gavin')],
    'father': [('steve', 'amy'), ('steve', 'andy'), ('gavin', 'amelia'), ('andy', 'spongebob')],
}

positive_instances = [
    ('ann', 'amelia'),
    ('steve', 'amelia'),
    ('ann', 'spongebob'),
    ('steve', 'spongebob'),
    ('linda', 'amelia'),
]

negative_instances = [
    ('amy', 'amelia'),
]

for instance in positive_instances:
    print(grandparent(data, instance[0], instance[1]))  # Should all be True

for instance in negative_instances:
    print(grandparent(data, instance[0], instance[1]))  # Should all be False
"""

"""
# New version:
# This gives False on all instances. It's only the last that should
# be False.
def grandparent(data, x, y):
    def parent(p, c):
        return (p, c) in data['mother'] or (p, c) in data['father']

    for potential_parent in data['mother'] + data['father']:
        if parent(x, potential_parent[0]) and parent(potential_parent[1], y):
            return True
    return False


data = {
    'mother': [('ann', 'amy'), ('ann', 'andy'), ('amy', 'amelia'), ('linda', 'gavin')],
    'father': [('steve', 'amy'), ('steve', 'andy'), ('gavin', 'amelia'), ('andy', 'spongebob')],
}

positive_instances = [
    ('ann', 'amelia'),
    ('steve', 'amelia'),
    ('ann', 'spongebob'),
    ('steve', 'spongebob'),
    ('linda', 'amelia'),
]

negative_instances = [
    ('amy', 'amelia'),
]

for instance in positive_instances:
    print(grandparent(data, instance[0], instance[1]))  # Should all be True

for instance in negative_instances:
    print(grandparent(data, instance[0], instance[1]))  # Should all be False

"""


# Last version. Still incorrect:
# False
# False
# False
# False
# False
# True

def grandparent(data, x, y):
    def parent(p, c):
        return (p, c) in data['mother'] or (p, c) in data['father']

    for potential_parent in data['mother'] + data['father']:
        if parent(x, potential_parent[1]) and parent(potential_parent[0], y):
            return True
    return False


data = {
    'mother': [('ann', 'amy'), ('ann', 'andy'), ('amy', 'amelia'), ('linda', 'gavin')],
    'father': [('steve', 'amy'), ('steve', 'andy'), ('gavin', 'amelia'), ('andy', 'spongebob')],
}

positive_instances = [
    ('ann', 'amelia'),
    ('steve', 'amelia'),
    ('ann', 'spongebob'),
    ('steve', 'spongebob'),
    ('linda', 'amelia'),
]

negative_instances = [
    ('amy', 'amelia'),
]

for instance in positive_instances:
    print(grandparent(data, instance[0], instance[1]))  # Should all be True

for instance in negative_instances:
    print(grandparent(data, instance[0], instance[1]))  # Should all be False
