New test case (half-siblings) for Relative Distance

Currently, Relative Distance mentions that children can have one or two parents, but the test cases do not reflect the implications of this rule for the code.

I tried creating an issue on GitHub but it was closed automatically. How can I add the new test case?

A solution that currently passes all tests, but shouldn’t

A Java solution that only looks at a single branch of ancestors and therefore misses connections:

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

class RelativeDistance {

    // <Child, Parent>
    Map<String, String> parents;

    RelativeDistance(Map<String, List<String>>  familyTree) {
        parents = new HashMap<>();

        for (Map.Entry<String, List<String>> entry : familyTree.entrySet()) {
            for (String child : entry.getValue())
                parents.put(child, entry.getKey());
        }
    }

    int degreeOfSeparation(String personA, String personB) {
        List<String> ancestorsA = new ArrayList<>();
        List<String> ancestorsB = new ArrayList<>();

        String nextA = personA;
        ancestorsA.add(nextA);
        while (parents.containsKey(nextA)) {
            nextA = parents.get(nextA);
            ancestorsA.add(nextA);
        }

        String nextB = personB;
        ancestorsB.add(nextB);
        while (parents.containsKey(nextB)) {
            nextB = parents.get(nextB);
            ancestorsB.add(nextB);
        }

        if (ancestorsA.contains(personB)) {
            return ancestorsA.indexOf(personB);
        } else if (ancestorsB.contains(personA)) {
            return ancestorsB.indexOf(personA);
        } else {
            for (String ancestor : ancestorsA) {
                if (ancestorsB.contains(ancestor)) {
                    return ancestorsA.indexOf(ancestor) + ancestorsB.indexOf(ancestor) - 1;
                }
            }
        }

        return -1;
    }
}

Counter example

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Charles  β”‚     β”‚ Andreas  β”‚     β”‚ William  β”‚
β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”˜     β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜
     β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
       β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”
       β”‚  James   β”‚       β”‚ Elizabeth β”‚
       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

The solution is not able to find the relative distance (1) between James and Elizabeth. Instead, it returns unrelated (-1).

Suggested test case

Family tree

Charles: [James]
Andreas: [James, Elizabeth]
William: [Elizabeth]

Expected output

degreeOfSeparation(James, Elizabeth) = 1

Relevance

The exercise appears to steer towards a BFS search algorithm. However, currently solutions that only check a single branch of ancestors pass all the tests.

See Suggesting Exercise Improvements

Is your suggestion specific to the Java track, or are you suggesting we add a test case to problem-specifications ? Please don’t create a problem-specifications PR unless/until there is consensus (three +1 votes).

… of maintainers :slightly_smiling_face:

Thanks for sharing the page. I have read it. To answer your question, the suggested test holds in general, for problem-specifications.

I believe the change is worth it, because currently the point of the exercise (search algorithm) can be easily missed by students. The new test case may invalidate existing solutions that took a trivial approach, but I believe it’s fair to those students and they can learn from it. The instructions explain the possibility of having two parents as one of four requirements, so that should be represented in the tests.

Where can I see the number of +1 votes / get the possibility for maintainers to vote?

It’s not a formal vote … and maintainer roles are not explicitly exposed. Propose a change (this thread) and people will comment. Maintainers that agree with this proposal will say so here.

1 Like

GH Issue: New test case (half-siblings) for Relative Distance Β· Issue #2700 Β· exercism/problem-specifications Β· GitHub

Looks like a valid addition. +1 from me.

Seems good +1