30
6
Note this is a question primarily focusing on data-structures
Introduction
Bacefook wants people to be friendlier! As such, they are implementing a new system to suggest friends! Your task is to help Bacefook to implement their new suggesting system.
Specifications:
Your program must be a REPL (read-eval-print loop) supporting 3 types of command: FRIEND
, SUGGEST
and KNOW
.
FRIEND X Y
- Specifies that X
and Y
are friends in the social network.
If X is friends with Y, then Y is friends with X
Can, but doesn't have to have output
X is always friends with X
KNOW X Y
- Output a truthy value if X and Y are friends, falsy otherwise
KNOW X X
will always output a truthy value
SUGGEST X Y
- Output a truthy value if X and Y should be friends, falsy otherwise. X and Y should be friends if:
X and Y are not friends
X and Y have at least 1 friend in common
You are allowed to replace FRIEND
, SUGGEST
and KNOW
with your own strings, but you must mention what string you have replaced each command with.
Your program can take in input/produce output in any way desirable, as long as it is reasonably easy to recognize how it works.
The number of people in the social network N
is between 1 and 100,000, but there may be any number of "friend links" (edges).
If you haven't noticed yet, this is a graph searching problem. The (likely) easiest (and possibly fastest) data structure to implement this in would be an adjacency matrix.
Test cases
FRIEND A B
FRIEND A C
FRIEND B D
SUGGEST A B -> Falsy, as they are friends
SUGGEST A D -> Truthy, as they share B as a common friend
SUGGEST C D -> Falsy, they do not share a common friend
KNOW D B -> Truthy, they are friends
KNOW B C -> Falsy, not friends
=============
FRIEND Tom Tim
KNOW Tom Tim -> Truthy
KNOW Tim Tom -> Truthy
KNOW Tom Kit -> Falsy
=============
KNOW Tim Kit -> Falsy
FRIEND Tim Tom
KNOW Tim Kit -> Falsy
FRIEND Tom Kit
SUGGEST Tim Kit -> Truthy
=============
FRIEND X Y
SUGGEST X Y -> Falsy since X is friends with X
Here's some more test cases in image form
Win condition
This is code-golf, shortest code wins!
So for example, can we start by inputting a list of all people in the network, like
{A, B, C, D}
? – Greg Martin – 2017-03-29T05:21:04.667@GregMartin Nope. That's the fun part of the challenge :) – Thunda – 2017-03-29T05:41:05.037
2Having the test cases in text form would be way more helpful. – Greg Martin – 2017-03-29T06:41:26.840
@GregMartin Would you rather I put them such that all of the input statements are in 1 block and all of the output statements are in another for easy comparison? Or the arrows are fine? – Thunda – 2017-03-29T07:09:27.800
Thanks! While my personal preference is to separate the input statements from the output statements, I feel I'm in the PPCG minority and more people prefer the arrow versions that you've made. – Greg Martin – 2017-03-29T07:13:45.583
This challenge screams Prolog. – orlp – 2017-03-29T07:51:44.913
When you say "your program must be a REPL" does that mean it could use the REPL of the language's interpreter, as is done in the Prolog answer? or does the program have to implement the reading, evaluating, and printing itself? the wording does not make it perfectly clear. – quintopia – 2017-03-29T08:48:39.387
1Can we have output after the FRIEND command? – ovs – 2017-03-29T08:52:55.067
@ovs I'll... go with yes – Thunda – 2017-03-29T10:48:58.120
@quintopia It can use the language's interpreter. Useful in languages like Haskell or in this case Prolog, but an extra problem for C-type languages. – Thunda – 2017-03-29T10:57:42.130
7
SUGGEST UK EU
. – WBT – 2017-03-29T17:27:34.9371@Thunda in Python, using the built-in REPL requires two extra characters in the command. Should languages like this add those extra bytes to the total length of the program? – quintopia – 2017-03-29T18:24:57.200
@quintopia Yes. – Thunda – 2017-03-30T02:35:41.050
@thedarkwanderer
FRIEND X Y SUGGEST X X
should return falsy because X is friends with X, it's inferred fromKNOW X X
is truthy. I'll add in a clarification. – Thunda – 2017-03-30T06:21:26.523@Thunda ah, I figured know X X being truthy was merely special behaviour. Makes more sense. – Please stop being evil – 2017-03-30T07:45:37.340
@WBT
==> null
– OldBunny2800 – 2017-04-03T20:14:40.250