-
-
Notifications
You must be signed in to change notification settings - Fork 84
add relative-distance
#887
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Instructions | ||
|
||
Your task is to determine the degree of separation between two individuals in a family tree. | ||
|
||
- You will be given an input, with all parent names and their children. | ||
- Each name is unique, a child _can_ have one or two parents. | ||
- The degree of separation is defined as the shortest number of connections from one person to another. | ||
- If two individuals are not connected, return a value that represents "no known relationship." | ||
Please see the test cases for the actual implementation. | ||
|
||
## Example | ||
|
||
Given the following family tree: | ||
|
||
```text | ||
┌──────────┐ ┌──────────┐ ┌───────────┐ | ||
│ Helena │ │ Erdős │ │ Shusaku │ | ||
└───┬───┬──┘ └─────┬────┘ └──────┬────┘ | ||
┌───┘ └───────┐ └──────┬──────┘ | ||
▼ ▼ ▼ | ||
┌──────────┐ ┌────────┐ ┌──────────┐ | ||
│ Isla │ │ Tariq │ │ Kevin │ | ||
└────┬─────┘ └────┬───┘ └──────────┘ | ||
▼ ▼ | ||
┌─────────┐ ┌────────┐ | ||
│ Uma │ │ Morphy │ | ||
└─────────┘ └────────┘ | ||
``` | ||
|
||
The degree of separation between Tariq and Uma is 3 (Tariq → Helena → Isla → Uma). | ||
There's no known relationship between Isla and [Kevin][six-bacons], as there is no connection in the given data. | ||
The degree of separation between Uma and Isla is 1. | ||
|
||
~~~~exercism/note | ||
Isla and Tariq are siblings and have a separation of 1. | ||
Similarly, this implementation would report a separation of 2 from you to your father's brother. | ||
~~~~ | ||
|
||
[six-bacons]: https://en.m.wikipedia.org/wiki/Six_Degrees_of_Kevin_Bacon |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Introduction | ||
|
||
You've been hired to develop **Noble Knots**, the hottest new dating app for nobility! | ||
With centuries of royal intermarriage, things have gotten… _complicated_. | ||
To avoid any _oops-we're-twins_ situations, your job is to build a system that checks how closely two people are related. | ||
|
||
Noble Knots is inspired by Iceland's "[Islendinga-App][islendiga-app]," which is backed up by a database that traces all known family connections between Icelanders from the time of the settlement of Iceland. | ||
Your algorithm will determine the **degree of separation** between two individuals in the royal family tree. | ||
|
||
Will your app help crown a perfect match? | ||
|
||
[islendiga-app]: http://www.islendingaapp.is/information-in-english/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"authors": [ | ||
"BNAndras" | ||
], | ||
"files": { | ||
"solution": [ | ||
"relative-distance.lisp" | ||
], | ||
"test": [ | ||
"relative-distance-test.lisp" | ||
], | ||
"example": [ | ||
".meta/example.lisp" | ||
] | ||
}, | ||
"blurb": "Given a family tree, calculate the degree of separation.", | ||
"source": "vaeng", | ||
"source_url": "https://github.com/exercism/problem-specifications/pull/2537" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
(defpackage :relative-distance | ||
(:use :cl) | ||
(:export :degree-of-separation)) | ||
|
||
(in-package :relative-distance) | ||
|
||
(defun degree-of-separation (family-tree person-a person-b) | ||
(let ((neighbors (make-hash-table :test 'equal))) | ||
|
||
(dolist (entry family-tree) | ||
(let ((parent (car entry)) | ||
(children (cdr entry))) | ||
|
||
(unless (gethash parent neighbors) | ||
(setf (gethash parent neighbors) (make-hash-table :test 'equal))) | ||
|
||
(dolist (child children) | ||
(unless (gethash child neighbors) | ||
(setf (gethash child neighbors) (make-hash-table :test 'equal))) | ||
(setf (gethash child (gethash parent neighbors)) t) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
(setf (gethash parent (gethash child neighbors)) t)) | ||
|
||
(dolist (child-a children) | ||
(dolist (child-b children) | ||
(unless (equal child-a child-b) | ||
(setf (gethash child-a (gethash child-b neighbors)) t) | ||
(setf (gethash child-b (gethash child-a neighbors)) t)))))) | ||
|
||
(unless (and (gethash person-a neighbors) (gethash person-b neighbors)) | ||
(return-from degree-of-separation nil)) | ||
|
||
|
||
(let ((queue (list (cons person-a 0))) | ||
(visited (make-hash-table :test 'equal))) | ||
|
||
(setf (gethash person-a visited) t) | ||
|
||
(loop while queue do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Starting to review the function I was thinking: "oh I don't care |
||
(let* ((current (pop queue)) | ||
(current-person (car current)) | ||
(current-degree (cdr current))) | ||
|
||
(when (equal current-person person-b) | ||
(return-from degree-of-separation current-degree)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about you factor this |
||
|
||
(let ((current-neighbors (gethash current-person neighbors))) | ||
(when current-neighbors | ||
(maphash (lambda (neighbor _) | ||
(unless (gethash neighbor visited) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't you get a |
||
(setf (gethash neighbor visited) t) | ||
(setf queue (append queue (list (cons neighbor (1+ current-degree))))))) | ||
current-neighbors)))))) | ||
|
||
nil)) | ||
verdammelt marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# This is an auto-generated file. | ||
# | ||
# Regenerating this file via `configlet sync` will: | ||
# - Recreate every `description` key/value pair | ||
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications | ||
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) | ||
# - Preserve any other key/value pair | ||
# | ||
# As user-added comments (using the # character) will be removed when this file | ||
# is regenerated, comments can be added via a `comment` key. | ||
|
||
[4a1ded74-5d32-47fb-8ae5-321f51d06b5b] | ||
description = "Direct parent-child relation" | ||
|
||
[30d17269-83e9-4f82-a0d7-8ef9656d8dce] | ||
description = "Sibling relationship" | ||
|
||
[8dffa27d-a8ab-496d-80b3-2f21c77648b5] | ||
description = "Two degrees of separation, grandchild" | ||
|
||
[34e56ec1-d528-4a42-908e-020a4606ee60] | ||
description = "Unrelated individuals" | ||
|
||
[93ffe989-bad2-48c4-878f-3acb1ce2611b] | ||
description = "Complex graph, cousins" | ||
|
||
[2cc2e76b-013a-433c-9486-1dbe29bf06e5] | ||
description = "Complex graph, no shortcut, far removed nephew" | ||
|
||
[46c9fbcb-e464-455f-a718-049ea3c7400a] | ||
description = "Complex graph, some shortcuts, cross-down and cross-up, cousins several times removed, with unrelated family tree" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
;; Ensures that relative-distance.lisp and the testing library are always loaded | ||
(eval-when (:compile-toplevel :load-toplevel :execute) | ||
(load "relative-distance") | ||
(quicklisp-client:quickload :fiveam)) | ||
|
||
;; Defines the testing package with symbols from relative-distance and FiveAM in scope | ||
;; The `run-tests` function is exported for use by both the user and test-runner | ||
(defpackage :relative-distance-test | ||
(:use :cl :fiveam) | ||
(:export :run-tests)) | ||
|
||
;; Enter the testing package | ||
(in-package :relative-distance-test) | ||
|
||
;; Define and enter a new FiveAM test-suite | ||
(def-suite* relative-distance-suite) | ||
|
||
(test direct-parent-child-relation | ||
(let ((family-tree '(("Vera" . ("Tomoko")) | ||
("Tomoko" . ("Aditi")))) | ||
(person-a "Vera") | ||
(person-b "Tomoko")) | ||
(is (= 1 (relative-distance:degree-of-separation family-tree person-a person-b))))) | ||
|
||
(test sibling-relationship | ||
(let ((family-tree '(("Dalia" . ("Olga" "Yassin")))) | ||
(person-a "Olga") | ||
(person-b "Yassin")) | ||
(is (= 1 (relative-distance:degree-of-separation family-tree person-a person-b))))) | ||
|
||
(test two-degrees-of-separation-grandchild | ||
(let ((family-tree '(("Khadija" . ("Mateo")) | ||
("Mateo" . ("Rami")))) | ||
(person-a "Khadija") | ||
(person-b "Rami")) | ||
(is (= 2 (relative-distance:degree-of-separation family-tree person-a person-b))))) | ||
|
||
(test unrelated-individuals | ||
(let ((family-tree '(("Priya" . ("Rami")) | ||
("Kaito" . ("Elif")))) | ||
(person-a "Priya") | ||
(person-b "Kaito")) | ||
(is (equal NIL (relative-distance:degree-of-separation family-tree person-a person-b))))) | ||
|
||
(test complex-graph-cousins | ||
(let ((family-tree '(("Aiko" . ("Bao" "Carlos")) | ||
("Bao" . ("Dalia" "Elias")) | ||
("Carlos" . ("Fatima" "Gustavo")) | ||
("Dalia" . ("Hassan" "Isla")) | ||
("Elias" . ("Javier")) | ||
("Fatima" . ("Khadija" "Liam")) | ||
("Gustavo" . ("Mina")) | ||
("Hassan" . ("Noah" "Olga")) | ||
("Isla" . ("Pedro")) | ||
("Javier" . ("Quynh" "Ravi")) | ||
("Khadija" . ("Sofia")) | ||
("Liam" . ("Tariq" "Uma")) | ||
("Mina" . ("Viktor" "Wang")) | ||
("Noah" . ("Xiomara")) | ||
("Olga" . ("Yuki")) | ||
("Pedro" . ("Zane" "Aditi")) | ||
("Quynh" . ("Boris")) | ||
("Ravi" . ("Celine")) | ||
("Sofia" . ("Diego" "Elif")) | ||
("Tariq" . ("Farah")) | ||
("Uma" . ("Giorgio")) | ||
("Viktor" . ("Hana" "Ian")) | ||
("Wang" . ("Jing")) | ||
("Xiomara" . ("Kaito")) | ||
("Yuki" . ("Leila")) | ||
("Zane" . ("Mateo")) | ||
("Aditi" . ("Nia")) | ||
("Boris" . ("Oscar")) | ||
("Celine" . ("Priya")) | ||
("Diego" . ("Qi")) | ||
("Elif" . ("Rami")) | ||
("Farah" . ("Sven")) | ||
("Giorgio" . ("Tomoko")) | ||
("Hana" . ("Umar")) | ||
("Ian" . ("Vera")) | ||
("Jing" . ("Wyatt")) | ||
("Kaito" . ("Xia")) | ||
("Leila" . ("Yassin")) | ||
("Mateo" . ("Zara")) | ||
("Nia" . ("Antonio")) | ||
("Oscar" . ("Bianca")) | ||
("Priya" . ("Cai")) | ||
("Qi" . ("Dimitri")) | ||
("Rami" . ("Ewa")) | ||
("Sven" . ("Fabio")) | ||
("Tomoko" . ("Gabriela")) | ||
("Umar" . ("Helena")) | ||
("Vera" . ("Igor")) | ||
("Wyatt" . ("Jun")) | ||
("Xia" . ("Kim")) | ||
("Yassin" . ("Lucia")) | ||
("Zara" . ("Mohammed")))) | ||
(person-a "Dimitri") | ||
(person-b "Fabio")) | ||
(is (= 9 (relative-distance:degree-of-separation family-tree person-a person-b))))) | ||
|
||
(test complex-graph-no-shortcut-far-removed-nephew | ||
(let ((family-tree '(("Aiko" . ("Bao" "Carlos")) | ||
("Bao" . ("Dalia" "Elias")) | ||
("Carlos" . ("Fatima" "Gustavo")) | ||
("Dalia" . ("Hassan" "Isla")) | ||
("Elias" . ("Javier")) | ||
("Fatima" . ("Khadija" "Liam")) | ||
("Gustavo" . ("Mina")) | ||
("Hassan" . ("Noah" "Olga")) | ||
("Isla" . ("Pedro")) | ||
("Javier" . ("Quynh" "Ravi")) | ||
("Khadija" . ("Sofia")) | ||
("Liam" . ("Tariq" "Uma")) | ||
("Mina" . ("Viktor" "Wang")) | ||
("Noah" . ("Xiomara")) | ||
("Olga" . ("Yuki")) | ||
("Pedro" . ("Zane" "Aditi")) | ||
("Quynh" . ("Boris")) | ||
("Ravi" . ("Celine")) | ||
("Sofia" . ("Diego" "Elif")) | ||
("Tariq" . ("Farah")) | ||
("Uma" . ("Giorgio")) | ||
("Viktor" . ("Hana" "Ian")) | ||
("Wang" . ("Jing")) | ||
("Xiomara" . ("Kaito")) | ||
("Yuki" . ("Leila")) | ||
("Zane" . ("Mateo")) | ||
("Aditi" . ("Nia")) | ||
("Boris" . ("Oscar")) | ||
("Celine" . ("Priya")) | ||
("Diego" . ("Qi")) | ||
("Elif" . ("Rami")) | ||
("Farah" . ("Sven")) | ||
("Giorgio" . ("Tomoko")) | ||
("Hana" . ("Umar")) | ||
("Ian" . ("Vera")) | ||
("Jing" . ("Wyatt")) | ||
("Kaito" . ("Xia")) | ||
("Leila" . ("Yassin")) | ||
("Mateo" . ("Zara")) | ||
("Nia" . ("Antonio")) | ||
("Oscar" . ("Bianca")) | ||
("Priya" . ("Cai")) | ||
("Qi" . ("Dimitri")) | ||
("Rami" . ("Ewa")) | ||
("Sven" . ("Fabio")) | ||
("Tomoko" . ("Gabriela")) | ||
("Umar" . ("Helena")) | ||
("Vera" . ("Igor")) | ||
("Wyatt" . ("Jun")) | ||
("Xia" . ("Kim")) | ||
("Yassin" . ("Lucia")) | ||
("Zara" . ("Mohammed")))) | ||
(person-a "Lucia") | ||
(person-b "Jun")) | ||
(is (= 14 (relative-distance:degree-of-separation family-tree person-a person-b))))) | ||
|
||
(test complex-graph-some-shortcuts-cross-down-and-cross-up-cousins-several-times-removed-with-unrelated-family-tree | ||
(let ((family-tree '(("Aiko" . ("Bao" "Carlos")) | ||
("Bao" . ("Dalia")) | ||
("Carlos" . ("Fatima" "Gustavo")) | ||
("Dalia" . ("Hassan" "Isla")) | ||
("Fatima" . ("Khadija" "Liam")) | ||
("Gustavo" . ("Mina")) | ||
("Hassan" . ("Noah" "Olga")) | ||
("Isla" . ("Pedro")) | ||
("Javier" . ("Quynh" "Ravi")) | ||
("Khadija" . ("Sofia")) | ||
("Liam" . ("Tariq" "Uma")) | ||
("Mina" . ("Viktor" "Wang")) | ||
("Noah" . ("Xiomara")) | ||
("Olga" . ("Yuki")) | ||
("Pedro" . ("Zane" "Aditi")) | ||
("Quynh" . ("Boris")) | ||
("Ravi" . ("Celine")) | ||
("Sofia" . ("Diego" "Elif")) | ||
("Tariq" . ("Farah")) | ||
("Uma" . ("Giorgio")) | ||
("Viktor" . ("Hana" "Ian")) | ||
("Wang" . ("Jing")) | ||
("Xiomara" . ("Kaito")) | ||
("Yuki" . ("Leila")) | ||
("Zane" . ("Mateo")) | ||
("Aditi" . ("Nia")) | ||
("Boris" . ("Oscar")) | ||
("Celine" . ("Priya")) | ||
("Diego" . ("Qi")) | ||
("Elif" . ("Rami")) | ||
("Farah" . ("Sven")) | ||
("Giorgio" . ("Tomoko")) | ||
("Hana" . ("Umar")) | ||
("Ian" . ("Vera")) | ||
("Jing" . ("Wyatt")) | ||
("Kaito" . ("Xia")) | ||
("Leila" . ("Yassin")) | ||
("Mateo" . ("Zara")) | ||
("Nia" . ("Antonio")) | ||
("Oscar" . ("Bianca")) | ||
("Priya" . ("Cai")) | ||
("Qi" . ("Dimitri")) | ||
("Rami" . ("Ewa")) | ||
("Sven" . ("Fabio")) | ||
("Tomoko" . ("Gabriela")) | ||
("Umar" . ("Helena")) | ||
("Vera" . ("Igor")) | ||
("Wyatt" . ("Jun")) | ||
("Xia" . ("Kim")) | ||
("Yassin" . ("Lucia")) | ||
("Zara" . ("Mohammed")))) | ||
(person-a "Wyatt") | ||
(person-b "Xia")) | ||
(is (= 12 (relative-distance:degree-of-separation family-tree person-a person-b))))) | ||
|
||
(defun run-tests (&optional (test-or-suite 'relative-distance-suite)) | ||
"Provides human readable results of test run. Default to entire suite." | ||
(run! test-or-suite)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think factoring this out into a few functions might be good. for example the tree parsing could be its own function