This repository has been archived by the owner on Jan 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters