From 4874a2d22d791aa23beaaa06b0a44c8e2d272381 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 16 Mar 2020 19:48:15 +0200 Subject: [PATCH] Add is_contained_in --- snippets/is_contained_in.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 snippets/is_contained_in.md diff --git a/snippets/is_contained_in.md b/snippets/is_contained_in.md new file mode 100644 index 000000000..93550ddf9 --- /dev/null +++ b/snippets/is_contained_in.md @@ -0,0 +1,20 @@ +--- +title: is_contained_in +tags: list,intermediate +--- + +Returns `True` if the elements of the first λιστ are contained in the second one regardless of order, `False` otherwise. + +Use `count()` to check if any value in `a` has more occurences than it has in `b`, returing `False` if any such value is found, `True` otherwise. + +```py +def is_contained_in(a, b): + for v in set(a): + if a.count(v) > b.count(v): + return False + return True +``` + +```py +is_contained_in([1, 4], [2, 4, 1]) # True +```