Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Commit

Permalink
add Problem 28A
Browse files Browse the repository at this point in the history
  • Loading branch information
Seasawher committed Jun 16, 2024
1 parent 732c6ec commit 5af6dad
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Src/Problem28.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/- # Problem 28A
(Intermediate 🌟🌟) Sorting a list of lists according to length of sublists.
-/
variable {α : Type}

def orderedInsert (a : α) (ord : α → Nat) : List α → List α
| [] => [a]
| b :: l =>
if ord a ≤ ord b then a :: b :: l
else b :: orderedInsert a ord l

/-- insertion sort -/
def insertionSort (ord : α → Nat) : List α → List α
| [] => []
| b :: l => orderedInsert b ord <| insertionSort ord l

#guard insertionSort (fun x => x) [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
= [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]

-- You can use this!
#check insertionSort

def lsort (l : List (List α)) : List (List α) :=
-- sorry
insertionSort (fun l => l.length) l
-- sorry

-- The following codes are for test and you should not edit these.

#guard lsort [["a", "b", "c"], ["a", "b"], ["a"]]
= [["a"], ["a", "b"], ["a", "b", "c"]]

#guard lsort [[3, 1, 4], [1, 5, 9, 2], [6, 5, 3, 5], [1]]
= [[1], [3, 1, 4], [1, 5, 9, 2], [6, 5, 3, 5]]
1 change: 1 addition & 0 deletions md/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* [25: Random permutation](./build/Problem25.md)
* [26: Generate combinations](./build/Problem26.md)
* [27: Group into disjoint subsets](./build/Problem27.md)
* [28A: Sorting a list of lists](./build/Problem28.md)

# 31-41: Arithmetic

Expand Down

0 comments on commit 5af6dad

Please sign in to comment.