Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Q14,Q15 #9

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Src.lean
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Src.Problem1
import Src.Problem10
import Src.Problem14
import Src.Problem15
import Src.Problem2
import Src.Problem3
import Src.Problem31
Expand Down
22 changes: 22 additions & 0 deletions Src/Problem14.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/-
# Problem 14
Duplicate the elements of a list.
-/

variable {α : Type}

def dupli (l : List α) : List α :=
-- sorry
match l with
| [] => []
| a :: b => a :: a :: dupli b
-- sorry

-- The following code is a test case and you should not change it.

example : dupli [1, 2, 3] = [1, 1, 2, 2, 3, 3] := by rfl

example : dupli ([] : List α) = [] := by rfl

example : dupli ['a', 'b', 'c', 'c', 'd']
= ['a', 'a', 'b', 'b', 'c', 'c', 'c', 'c', 'd', 'd'] := by rfl
29 changes: 29 additions & 0 deletions Src/Problem15.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/-
# Problem 15
Replicate the elements of a list a given number of times.
-/

variable {α : Type}

def repli (l : List α) (n : Nat) : List α :=
-- sorry
match l with
| [] => []
| a :: b => (solorepl a n) ++ repli b n
where solorepl (x : α) (n : Nat) : List α :=
match n with
| 0 => []
| m + 1 => x :: solorepl x m
-- sorry

-- The following code is a test case and you should not change it.

example : repli [1, 2, 3] 3 = [1, 1, 1, 2, 2, 2, 3, 3, 3] := by rfl

example : repli [1, 2, 3] 2 = [1, 1, 2, 2, 3, 3] := by rfl

example : repli [1, 2, 3] 1 = [1, 2, 3] := by rfl

example : repli [1, 2, 3] 0 = [] := by rfl

example : repli ([] : List α) 255 = [] := by rfl
5 changes: 5 additions & 0 deletions md/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
* [9: Pack consecutive duplicates](./build/Problem9.md)
* [10: Run-length encoding](./build/Problem10.md)

# 11-20: Lists, continued

* [14: Duplicate elements](./build/Problem14.md)
* [15: Replicate elements K times](./build/Problem15.md)

# 31-40: Arithmetic

* [31: Prime number detection](./build/Problem31.md)
Expand Down
Loading