diff --git a/have_same_contents.md b/have_same_contents.md new file mode 100644 index 000000000..d29afa78e --- /dev/null +++ b/have_same_contents.md @@ -0,0 +1,22 @@ +--- +title: have_same_contents +tags: list,intermediate +--- + +Returns `True` if two lists contain the same elements regardless of order, `False` otherwise. + +Use `set()` on combination of both lists to find the unique values. +Iterate over them with a `for` loop comparing the `count()` of each unique value in each array. +Return `False` if the counts do not match for any element, `True` otherwise. + +```py +def have_same_contents(a, b): + for v in set(a + b): + if a.count(v) != b.count(v): + return False + return True +``` + +```py +have_same_contents([1, 2, 4], [2, 4, 1]) # True +``` diff --git a/includes_all.md b/includes_all.md new file mode 100644 index 000000000..d2a3eab41 --- /dev/null +++ b/includes_all.md @@ -0,0 +1,21 @@ +--- +title: includes_all +tags: utility,intermediate +--- + +Returns `True` if all the elements in `values` are included in `lst`, `False` otherwise. + +Check if every value in `values` is contained in `lst` using a `for` loop, returning `False` if any one value is not found, `True` otherwise. + +```py +def includes_all(lst, values): + for v in values: + if v not in lst: + return False + return True +``` + +```py +includes_all([1, 2, 3, 4], [1, 4]) # True +includes_all([1, 2, 3, 4], [1, 5]) # False +``` diff --git a/includes_any.md b/includes_any.md new file mode 100644 index 000000000..12c511973 --- /dev/null +++ b/includes_any.md @@ -0,0 +1,21 @@ +--- +title: includes_any +tags: list,intermediate +--- + +Returns `True` if any the elements in `values` are included in `lst`, `False` otherwise. + +Check if any value in `values` is contained in `lst` using a `for` loop, returning `True` if any one value is found, `False` otherwise. + +```py +def includes_any(lst, values): + for v in values: + if v in lst: + return True + return False +``` + +```py +includes_any([1, 2, 3, 4], [2, 9]) # True +includes_any([1, 2, 3, 4], [8, 9]) # False +```