File tree Expand file tree Collapse file tree 1 file changed +26
-3
lines changed
Expand file tree Collapse file tree 1 file changed +26
-3
lines changed Original file line number Diff line number Diff line change 11module
22
3- def digitSum : Unit :=
4- ()
3+ def digitSum (s : String) : Nat :=
4+ s.chars.filter Char.isUpper
5+ |>.map Char.toNat
6+ |>.fold (init := 0 ) (· + ·)
7+
8+ example : digitSum "" = 0 := by cbv
9+ example : digitSum "abAB" = 131 := by cbv
10+ example : digitSum "abcCd" = 67 := by cbv
11+ example : digitSum "helloE" = 69 := by cbv
12+ example : digitSum "woArBld" = 131 := by cbv
13+ example : digitSum "aAaaaXa" = 153 := by cbv
14+
15+ theorem digitSum_eq {s : String} :
16+ digitSum s = ((s.toList.filter Char.isUpper).map Char.toNat).sum := by
17+ simp [digitSum, ← Std.Iter.foldl_toList, List.sum_eq_foldl]
18+
19+ theorem digitSum_empty : digitSum "" = 0 := by simp [digitSum_eq]
20+
21+ theorem digitSum_push_of_isUpper {s : String} {c : Char} (h : c.isUpper = true ) :
22+ digitSum (s.push c) = digitSum s + c.toNat := by
23+ simp [digitSum_eq, List.filter_cons_of_pos h]
24+
25+ theorem digitSum_push_of_isUpper_eq_false {s : String} {c : Char} (h : c.isUpper = false ) :
26+ digitSum (s.push c) = digitSum s := by
27+ simp [digitSum_eq, List.filter_cons_of_neg (Bool.not_eq_true _ ▸ h)]
528
629/-!
730## Prompt
@@ -50,4 +73,4 @@ def check(candidate):
5073 assert candidate("You arE Very Smart") == 327, "Error"
5174
5275```
53- -/
76+ -/
You can’t perform that action at this time.
0 commit comments