A day that works for both
- O(n²) · Very hard
- Full plan
- Python
- JavaScript
- lists
- loops
- booleans
Problem
Ana and Luis want to meet this week. Ana wrote down the days she has free in one list, and Luis wrote his own in another. Before thinking about where, there is something simpler to settle: is there a day both of them have free?
Write a function that takes two lists and returns true if they share at least one element, and false if they share none.
With "Monday", "Tuesday", "Thursday" on one side and "Friday", "Tuesday", "Sunday" on the other you return true, because Tuesday is in both.
The lists can hold numbers or text, they don't come sorted, and they can come empty. If either one is empty there is nothing to share, so you return false.
Hint: take each element of the first list and look for it in the second one. The moment you find one that is in both you already know the answer and you can return it right there: no need to check what is left.
Examples
They share a number
[1, 2, 3, 4, 5], [5, 6, 7, 8, 9] → true
Nothing in common
[1, 2, 3, 4, 5], [6, 7, 8, 9] → false
The day that works for both
["Monday", "Tuesday", "Thursday"], ["Friday", "Tuesday", "Sunday"] → true
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def any_shared(one, other):
passJavaScript
function anyShared(one, other) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.