Sometimes I feel like the conversation is going down the wrong path and take it upon myself to steer it in another direction. And this conversation is going wrong real fast, so I need to change the subject in as few characters as possible.1
Given a list of sentences, return the sentences ordered such that each sentence "connects" to the next.
For the purposes of this challenge, a sentence is a string, starting with a capital letter, containing words separated by spaces (there won't be punctuation between words), and ending with a period.
Two sentences "connect" if they share at least two common words. For example, the sentences "Once upon a time giraffes roamed the land freely." and "Another species that roamed freely were elephants." connect because they share the words "roamed" and "freely", while the latter sentence does not connect with "I love eating sandwiches." because they do not share any words. Note that the first and last words of the sentence can be compared with the others as well, and the first word starts with a capital letter while the last word ends with a period.
The input is guaranteed to have a valid output. Both an output and its reverse will be valid, so you may output either or both. The input may have multiple valid orders.
This is code-golf, so shortest answer in bytes wins.
You must output the sorted list, but for the sake of brevity I will write the test cases as returning a list of zero-based indices of the original list.
"Once upon a time giraffes roamed the land freely." "An awful lot of stories start with the phrase once upon a time." "Another species that roamed freely were elephants." -> [1, 0, 2] or [2, 0, 1] "I hear that he is the worst." "Did you hear that Mr. Jones is running for president." "He should not be in charge of making our sandwiches." "The worst sandwiches in the world." -> [1, 0, 3, 2] or [2, 3, 0, 1] Please comment suggested test cases, I suck at this.
1 This is a totally logical explanation for why this is code-golf. 🙈 🙉