11module
2+ import Std
23
3- def vowels_count : Unit :=
4- ()
4+ open Std
5+
6+ def vowelsList : List Char :=
7+ ['a' , 'e' , 'i' , 'o' , 'u' , 'A' , 'E' , 'I' , 'O' , 'U' ]
8+
9+ def vowels : HashSet Char :=
10+ .ofList vowelsList
11+
12+ def vowelsCount (s : String) : Nat :=
13+ let nVowels := s.chars.filter (· ∈ vowels) |>.length
14+ let atEnd := if s.back?.any (fun c => c == 'y' || c == 'Y' ) then 1 else 0
15+ nVowels + atEnd
16+
17+ example : vowelsCount "abcde" = 2 := by native_decide
18+ example : vowelsCount "Alone" = 3 := by native_decide
19+ example : vowelsCount "key" = 2 := by native_decide
20+ example : vowelsCount "bye" = 1 := by native_decide
21+ example : vowelsCount "keY" = 2 := by native_decide
22+ example : vowelsCount "bYe" = 1 := by native_decide
23+ example : vowelsCount "ACEDY" = 3 := by native_decide
24+
25+ theorem vowelsCount_eq {s : String} :
26+ vowelsCount s = (s.toList.filter (· ∈ vowelsList)).length +
27+ (if s.toList.getLast? = some 'y' ∨ s.toList.getLast? = some 'Y' then 1 else 0 ) := by
28+ simp only [vowelsCount, ne_eq, ← Iter.length_toList_eq_length,
29+ Iter.toList_filter, String.toList_chars, ↓Char.isValue]
30+ simp only [vowels, HashSet.mem_ofList, List.contains_eq_mem, decide_eq_true_eq, ↓Char.isValue,
31+ Option.any_eq_true, Bool.or_eq_true, beq_iff_eq, Nat.add_left_cancel_iff]
32+ simp [and_comm, String.back?_eq]
533
634/-!
735## Prompt
@@ -54,4 +82,4 @@ def check(candidate):
5482 assert True, "This prints if this assert fails 2 (also good for debugging!)"
5583
5684```
57- -/
85+ -/
0 commit comments