Skip to content

Commit fe00673

Browse files
committed
correcao do assignment 4
- implementacao do BST insert2 que recebe funcao `key` - implementacao parcial do Bucketsort
1 parent 10cd50b commit fe00673

File tree

4 files changed

+103
-33
lines changed

4 files changed

+103
-33
lines changed

Fad/Assignment04.lean

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ end
9292

9393
section
9494
open Chapter5.Quicksort (qsort₂)
95-
open Chapter4.BST2 (search mkTree mkTree₁)
95+
open Chapter4.BST2 (search mkTree mkTree₁ mkTree₂)
9696

9797
structure Book where
9898
title : String
@@ -108,15 +108,23 @@ instance : LT Book where
108108
instance : DecidableRel (fun (a b : Book) => a < b) :=
109109
fun a b => Float.decLt a.price b.price
110110

111-
def books := List.range 12 |>.map (λ n => Book.mk s!"{n}" n.toFloat)
112-
113-
#eval mkTree₁ books |>.toFormat
111+
def books := List.range 12
112+
|>.map (λ n => Book.mk s!"{n}" n.toFloat)
114113

115114
/- Both `search` and `mkTree₁` need to use the same field for
116115
insertion and lookup.-/
117116

118-
-- #eval mkTree₁ books
119-
#eval search (fun x => x.title) "10" (mkTree₁ books)
117+
instance : Ord Float where
118+
compare a b :=
119+
if a < b then Ordering.lt
120+
else if a > b then Ordering.gt
121+
else Ordering.eq
122+
123+
/-
124+
#eval
125+
let tb := mkTree₂ (·.price) books
126+
search (·.price) 10 tb
127+
-/
120128

121129
end
122130

Fad/Chapter1.lean

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,17 @@ def map (f : a → b) : List a → List b
1616
| [] => []
1717
| (x :: xs) => f x :: map f xs
1818

19-
-- #eval map (· + 10) [1,2,3]
20-
21-
example : map (· * 10) [1,2,3] = [10,20,30] := rfl
22-
19+
example : map (· * 10) [1,2,3] = [10,20,30] :=
20+
rfl
2321

24-
theorem map_equal (a : List α) (f : α → β): map f a = List.map f a := by
22+
theorem map_equal {α β} (a : List α) (f : α → β)
23+
: map f a = List.map f a := by
2524
induction a with
2625
| nil => rfl
2726
| cons a as ih =>
2827
simp [List.map, map]
2928
rw [ih]
3029

31-
32-
3330
def makeInc (n : Nat) : (Nat → Nat) :=
3431
fun x => x + n
3532

Fad/Chapter4.lean

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ inductive Tree (a : Type) : Type
238238
| node : Nat → (Tree a) → a → (Tree a) → Tree a
239239
deriving Nonempty, Inhabited
240240

241-
/-
242241
open Std.Format in
243242

244243
def Tree.toFormat [ToString a] : (t : Tree a) → Std.Format
@@ -249,7 +248,7 @@ def Tree.toFormat [ToString a] : (t : Tree a) → Std.Format
249248

250249
instance [ToString a] : Repr (Tree a) where
251250
reprPrec e _ := e.toFormat
252-
-/
251+
253252

254253
def Tree.height : Tree a → Nat
255254
| .null => 0
@@ -305,7 +304,6 @@ def insert : (x : a) -> Tree a -> Tree a
305304
if x < y then balance (insert x l) y r else
306305
if x > y then balance l y (insert x r) else .node h l y r
307306

308-
309307
def mkTree : (xs : List a) → Tree a :=
310308
List.foldr insert (.null : Tree a)
311309

@@ -346,6 +344,34 @@ def insert₁ : (x : a) -> Tree a -> Tree a
346344
def mkTree₁ : List a → Tree a :=
347345
List.foldr insert₁ Tree.null
348346

347+
def insert₂ {b : Type} [Ord b] (key: a → b)
348+
: (x : a) -> Tree a -> Tree a
349+
| x, .null => node .null x .null
350+
| x, .node h l y r =>
351+
match compare (key x) (key y) with
352+
| .lt => gbalance (insert₂ key x l) y r
353+
| .gt => gbalance l y (insert₂ key x r)
354+
| .eq => .node h l y r
355+
356+
def mkTree₂ {b : Type} [Ord b] (key: a → b)
357+
: List a → Tree a :=
358+
List.foldr (insert₂ key) Tree.null
359+
360+
361+
/- how to deal with list with duplicated elements -/
362+
363+
instance : Ord (Nat × Nat) where
364+
compare a b :=
365+
match compare a.1 b.1 with
366+
| .eq => compare a.2 b.2
367+
| x => x
368+
369+
/-
370+
#eval
371+
(λ xs => mkTree₂ id xs.zipIdx |> Tree.flatten |>.map (·.1))
372+
[1,3,2,1,2]
373+
-/
374+
349375
def sort : List a → List a :=
350376
Tree.flatten ∘ mkTree₁
351377

Fad/Chapter5.lean

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import Fad.«Chapter1-Ex»
33

44
namespace Chapter5
55

6+
-- ## Section 5.1 Quicksort
7+
68
namespace Quicksort
79

810
variable {a : Type} [h₁ : LT a] [h₂ : DecidableRel (α := a) (· < ·)]
@@ -58,9 +60,9 @@ def qsort₂ [Ord a] (f : a → a → Ordering) : List a → List a
5860
#eval qsort₂ compare ['c','a','b']
5961
-/
6062

61-
6263
end Quicksort
6364

65+
-- ## Section 5.2 MergeSort
6466

6567
namespace Mergesort
6668

@@ -189,35 +191,29 @@ def mkPair (n : Nat) (xs : List a) : (Tree a × List a) :=
189191
def mkTree₁ (as : List a) : Tree a :=
190192
mkPair as.length as |>.1
191193

192-
193194
def msort₂ (xs : List a) : List a :=
194195
(Tree.flatten ∘ mkTree₁) xs
195196

196-
197197
def pairWith (f : a → a → a) : List a → List a
198198
| [] => []
199199
| [x] => [x]
200200
| (x :: y :: xs) => f x y :: pairWith f xs
201201

202-
203202
def mkTree₂ : List a → Tree a
204203
| [] => .null
205204
| as =>
206205
unwrap (until' single (pairWith .node) (as.map .leaf))
207206
|>.getD .null
208207

209-
210208
def msort₃ (xs : List a) : List a :=
211209
(Tree.flatten ∘ mkTree₂) xs
212210

213-
214211
def msort₄ : List a → List a
215212
| [] => []
216213
| as =>
217214
unwrap (until' single (pairWith merge) (as.map wrap))
218215
|>.getD []
219216

220-
221217
def msort₅ : List a → List a
222218
| [] => []
223219
| xs =>
@@ -232,36 +228,79 @@ def msort₅ : List a → List a
232228
let runs := List.foldr op []
233229
unwrap (until' single (pairWith merge) (runs xs)) |>.getD []
234230

235-
236231
end Mergesort
237232

238233

234+
-- ## Section 5.3 Heapsort
239235

240236
namespace Heapsort
241237

242-
inductive Tree (α : Type) : Type
243-
| null : Tree α
244-
| node : α → Tree α → Tree α → Tree α
245-
deriving Inhabited
238+
variable {a : Type} [LE a] [ToString a]
246239

240+
inductive Tree (a : Type) : Type
241+
| null : Tree a
242+
| node : a → Tree a → Tree a → Tree a
243+
deriving Inhabited
247244

248-
def flatten [LE a] [DecidableRel (α := a) (· ≤ ·)] : Tree a → List a
245+
def flatten [DecidableRel (α := a) (· ≤ ·)] : Tree a → List a
249246
| Tree.null => []
250247
| Tree.node x u v => x :: Mergesort.merge (flatten u) (flatten v)
251248

252-
253249
open Std.Format in
254250

255-
def Tree.toFormat [ToString α] : (t: Tree α) → Std.Format
251+
def Tree.toFormat : (t: Tree a) → Std.Format
256252
| .null => Std.Format.text "."
257253
| .node x t₁ t₂ =>
258254
bracket "(" (f!"{x}" ++
259255
line ++ nest 2 t₁.toFormat ++ line ++ nest 2 t₂.toFormat) ")"
260256

261-
instance [ToString a] : Repr (Tree a) where
257+
instance : Repr (Tree a) where
262258
reprPrec e _ := Tree.toFormat e
263259

264-
265260
end Heapsort
266261

262+
263+
-- ## Section 5.4 Bucketsort and Radixsort
264+
265+
namespace Bucketsort
266+
267+
variable {α : Type}
268+
269+
inductive Tree (α : Type)
270+
| leaf : α → Tree α
271+
| node : List (Tree α) → Tree α
272+
deriving Repr
273+
274+
275+
def Tree.flatten (r : Tree (List α)) : List α :=
276+
match r with
277+
| .leaf v => v
278+
| .node ts =>
279+
List.flatten <| ts.map flatten
280+
281+
def ptn₀ {α β : Type} [BEq β] (rng : List β)
282+
(f : α → β) (xs : List α) : List (List α) :=
283+
rng.map (λ m => xs.filter (λ x => f x == m))
284+
285+
def mkTree {α β : Type} [BEq β]
286+
(rng : List β)
287+
(ds : List (α → β)) (xs : List α) : Tree (List α) :=
288+
match ds with
289+
| [] => .leaf xs
290+
| d :: ds' =>
291+
.node ((ptn₀ rng d xs).map (mkTree rng ds'))
292+
293+
def bsort₀ {β : Type} [BEq β] (rng : List β)
294+
(ds : List (α → β)) (xs : List α) : List α :=
295+
Tree.flatten (mkTree rng ds xs)
296+
297+
/-
298+
#eval bsort₀ "abc".toList
299+
[fun s => s.toList[0]!,fun s => s.toList[1]!]
300+
["ba", "ab", "aab", "bba"]
301+
-/
302+
303+
end Bucketsort
304+
305+
267306
end Chapter5

0 commit comments

Comments
 (0)