11module
22
3- def common : Unit :=
4- ()
3+ public import Std
4+ open Std
5+
6+ public section
7+
8+ /-! ## Implementation -/
9+
10+ def common (xs ys : List Nat) : List Nat :=
11+ let xsSet : TreeSet Nat := .ofList xs
12+ let ysSet : TreeSet Nat := .ofList ys
13+ ysSet.filter (· ∈ xsSet) |>.toList
14+
15+ /-! ## Tests -/
16+
17+ set_option cbv.warning false
18+
19+ example : common [1 , 4 , 3 , 34 , 653 , 2 , 5 ] [5 , 7 , 1 , 5 , 9 , 653 , 121 ] = [1 , 5 , 653 ] := by cbv
20+ example : common [5 , 3 , 2 , 8 ] [3 , 2 ] = [2 , 3 ] := by cbv
21+ example : common [4 , 3 , 2 , 8 ] [3 , 2 , 4 ] = [2 , 3 , 4 ] := by cbv
22+ example : common [4 , 3 , 2 , 8 ] [] = [] := by cbv
23+
24+ /-! ## Verification -/
25+
26+ theorem pairwise_lt_common :
27+ (common xs ys).Pairwise (· < ·) := by
28+ grind [common, TreeSet.ordered_toList, compare_eq_lt]
29+
30+ theorem distinct_common :
31+ (common xs ys).Pairwise (· ≠ ·) := by
32+ grind [common, TreeSet.distinct_toList]
33+
34+ theorem mem_common_iff :
35+ x ∈ common xs ys ↔ x ∈ xs ∧ x ∈ ys := by
36+ grind [common]
537
638/-!
739## Prompt
@@ -45,4 +77,4 @@ def check(candidate):
4577 assert candidate([4, 3, 2, 8], []) == []
4678
4779```
48- -/
80+ -/
0 commit comments