11module
22
3+ /-! ## Implementation -/
4+
35def strangeSortArray (xs : Array Int) : Array Int :=
46 let sorted := xs.mergeSort
57 Array.ofFn (n := sorted.size)
68 (fun i => if i.val % 2 = 0 then sorted[i.val / 2 ] else sorted[sorted.size - 1 - i.val / 2 ])
79
8- theorem t {xs : List Int} :
10+ /-! ## Tests -/
11+
12+ example : strangeSortArray #[1 , 2 , 3 , 4 ] = #[1 , 4 , 2 , 3 ] := by native_decide
13+ example : strangeSortArray #[5 , 6 , 7 , 8 , 9 ] = #[5 , 9 , 6 , 8 , 7 ] := by native_decide
14+ example : strangeSortArray #[1 , 2 , 3 , 4 , 5 ] = #[1 , 5 , 2 , 4 , 3 ] := by native_decide
15+ example : strangeSortArray #[5 , 6 , 7 , 8 , 9 , 1 ] = #[1 , 9 , 5 , 8 , 6 , 7 ] := by native_decide
16+ example : strangeSortArray #[5 , 5 , 5 , 5 ] = #[5 , 5 , 5 , 5 ] := by native_decide
17+ example : strangeSortArray #[] = #[] := by native_decide
18+ example : strangeSortArray #[1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] = #[1 , 8 , 2 , 7 , 3 , 6 , 4 , 5 ] := by native_decide
19+ example : strangeSortArray #[0 , 2 , 2 , 2 , 5 , 5 , -5 , -5 ] = #[-5 , 5 , -5 , 5 , 0 , 2 , 2 , 2 ] := by native_decide
20+ example : strangeSortArray #[111111 ] = #[111111 ] := by native_decide
21+
22+ /-! ## Missing API -/
23+
24+ theorem List.all_min?_mem {xs : List Int} :
925 xs.min?.all (· ∈ xs) := by
1026 simp only [Option.all_eq_true_iff_get]
1127 grind
1228
13- grind_pattern t => xs.min?
14-
15- -- missing api
29+ -- Helps grind to derive `b ∈ xs` from `xs.min? = some b`
30+ grind_pattern List.all_min?_mem => xs.min?
1631
1732theorem List.Perm.min_eq [LE α] [Min α] [Std.LawfulOrderMin α] [Std.IsLinearOrder α]
1833 {xs ys : List α} (h_perm : xs.Perm ys) (h : xs ≠ []) :
@@ -56,24 +71,6 @@ theorem Array.Perm.min?_eq [LE α] [Min α] [Std.LawfulOrderMin α] [Std.IsLinea
5671 xs.min? = ys.min? := by
5772 simp [← min?_toList, h_perm.toList.min?_eq]
5873
59- theorem List.foldl_concat [Max α] {xs : List α} :
60- (xs.concat x).foldl f init = f (xs.foldl f init) x := by
61- simp
62-
63- -- theorem List.max?_concat [Max α] {xs : List α} :
64- -- (xs.concat x).max? = xs.max?.elim x (fun maximum => max maximum x) := by
65- -- induction xs
66- -- sorry
67-
68- -- theorem List.max?_eq_getLast? [Max α] {xs : List α} (hp : xs.Pairwise (fun a b => max a b = b)) :
69- -- xs.max? = xs.getLast? := by
70- -- rw [← List.reverse_reverse (as := xs)]
71- -- generalize xs.reverse = xs
72- -- induction xs
73- -- · simp
74- -- · rename_i x xs ih
75- -- simp [List.max?_push, List.getLast?_cons]
76-
7774theorem List.max_eq_getLast [Max α] {xs : List α} (h) (hp : xs.Pairwise (fun a b => max a b = b)) :
7875 xs.max h = xs.getLast h := by
7976 rw [List.max.eq_def]
@@ -89,8 +86,8 @@ theorem List.max_eq_getLast [Max α] {xs : List α} (h) (hp : xs.Pairwise (fun a
8986 · simp
9087 rename_i ih
9188 rw [ih]
92- rw [List.reverse_cons, ← List.cons_append, pairwise_append] at hp <;> grind
93- grind -- what's going on?
89+ · rw [List.reverse_cons, ← List.cons_append, pairwise_append] at hp <;> grind
90+ · grind
9491
9592theorem List.max?_eq_getLast? [Max α] {xs : List α} (hp : xs.Pairwise (fun a b => max a b = b)) :
9693 xs.max? = xs.getLast? := by
@@ -216,15 +213,15 @@ theorem Array.head?_toList {xs : Array α} :
216213
217214@ [grind =]
218215theorem Array.count_drop_one [BEq α] {xs : Array α} {a : α} :
219- (xs.drop 1 ).count a = xs.count a - if xs[0 ]? == some a then 1 else 0 := by
220- have := List.count_tail (l := xs.toList) (a := a)
221- simp [← count_toList, List.take_of_length_le, this, head?_toList]
216+ (xs.drop 1 ).count a = xs.count a - if xs[0 ]? == some a then 1 else 0 := by
217+ have := List.count_tail (l := xs.toList) (a := a)
218+ simp [← count_toList, List.take_of_length_le, this, head?_toList]
222219
223220theorem Array.min?_eq_getElem?_zero {α : Type u} [Min α] {xs : Array α}
224221 (h : xs.toList.Pairwise (fun a b => min a b = a)) : xs.min? = xs[0 ]? := by
225222 simp [← min?_toList, List.min?_eq_head? h, List.head?_eq_getElem?]
226223
227- -- verification
224+ /-! ## Verification -/
228225
229226theorem strangeSortArray_empty :
230227 strangeSortArray #[] = #[] := by
0 commit comments