11module
2+ import Std.Data.String.ToNat
23
3- def fruit_distribution : Unit :=
4- ()
4+ def fruitDistribution (s : String) (n : Nat) : Nat :=
5+ n - ((s.split ' ' ).filterMap String.Slice.toNat?).fold (init := 0 ) (· + ·)
6+
7+ example : fruitDistribution "5 apples and 6 oranges" 19 = 8 := by native_decide
8+ example : fruitDistribution "5 apples and 6 oranges" 21 = 10 := by native_decide
9+ example : fruitDistribution "0 apples and 1 oranges" 3 = 2 := by native_decide
10+ example : fruitDistribution "1 apples and 0 oranges" 3 = 2 := by native_decide
11+ example : fruitDistribution "2 apples and 3 oranges" 100 = 95 := by native_decide
12+ example : fruitDistribution "2 apples and 3 oranges" 5 = 0 := by native_decide
13+ example : fruitDistribution "1 apples and 100 oranges" 120 = 19 := by native_decide
14+
15+ inductive Word
16+ | number : Nat → Word
17+ | nonNumber : (s : String) →
18+ (hs : s.isNat = false := by simp [← Bool.not_eq_true, String.isNat_iff]) →
19+ (hs' : ' ' ∉ s.toList := by simp) → Word
20+
21+ def Word.toNat? : Word → Option Nat
22+ | .number n => some n
23+ | .nonNumber .. => none
24+
25+ def Word.toString : Word → String
26+ | .number n => ToString.toString n
27+ | .nonNumber s _ _ => s
28+
29+ @[simp]
30+ theorem Word.toNat?_comp_toString : String.toNat? ∘ Word.toString = Word.toNat? := by
31+ ext1 w
32+ match w with
33+ | .number n => simp [Word.toNat?, Word.toString]
34+ | .nonNumber s hs hs' => simp [Word.toNat?, Word.toString, hs]
35+
36+ @[simp]
37+ theorem Word.space_notin_toList_toString {w : Word} : ' ' ∉ w.toString.toList := by
38+ match w with
39+ | .number n =>
40+ simp only [toString, Nat.toString_eq_repr, Nat.toList_repr, ↓Char.isValue]
41+ intro h
42+ simpa using Nat.isDigit_of_mem_toDigits (by decide) (by decide) h
43+ | .nonNumber _ _ hs' => exact hs'
44+
45+ @[simp]
46+ theorem String.isNat_empty : "" .isNat = false := by
47+ simp [← Bool.not_eq_true, isNat_iff]
48+
49+ @[simp]
50+ theorem String.toNat?_empty : "" .toNat? = none := by
51+ simp
52+
53+ theorem fruitDistribution_intercalate_toString (l : List Word) (n : Nat) :
54+ fruitDistribution (" " .intercalate (l.map Word.toString)) n = n - (l.filterMap Word.toNat?).sum := by
55+ rw [fruitDistribution]
56+ simp +instances only [String.reduceToSingleton]
57+ rw [← Std.Iter.foldl_toList, Std.Iter.toList_filterMap, ← String.Slice.toNat?_comp_copy,
58+ ← List.filterMap_map, String.toList_split_intercalate (by simp), ← List.sum_eq_foldl]
59+ simp only [List.map_eq_nil_iff]
60+ split <;> simp_all
61+
62+ theorem fruitDistribution_sentence {n₁ n₂ n : Nat} :
63+ fruitDistribution (toString n₁ ++ " apples and " ++ toString n₂ ++ " oranges" ) n = n - n₁ - n₂ := by
64+ have := fruitDistribution_intercalate_toString [.number n₁, .nonNumber "apples" , .nonNumber "and" , .number n₂, .nonNumber "oranges" ] n
65+ simp only [List.map_cons, Word.toString, Nat.toString_eq_repr, List.map_nil,
66+ String.intercalate_cons_cons, String.reduceAppend, String.intercalate_singleton, Word.toNat?,
67+ Option.some.injEq, List.filterMap_cons_some, List.filterMap_cons_none, List.filterMap_nil,
68+ List.sum_cons, List.sum_nil, Nat.add_zero, ← Nat.sub_sub] at this
69+ rw [← this]
70+ congr 1
71+ simp [← String.toList_inj]
572
673/-!
774## Prompt
@@ -10,10 +77,10 @@ def fruit_distribution : Unit :=
1077
1178def fruit_distribution(s,n):
1279 """
13- In this task, you will be given a string that represents a number of apples and oranges
14- that are distributed in a basket of fruit this basket contains
15- apples, oranges, and mango fruits. Given the string that represents the total number of
16- the oranges and apples and an integer that represent the total number of the fruits
80+ In this task, you will be given a string that represents a number of apples and oranges
81+ that are distributed in a basket of fruit this basket contains
82+ apples, oranges, and mango fruits. Given the string that represents the total number of
83+ the oranges and apples and an integer that represent the total number of the fruits
1784 in the basket return the number of the mango fruits in the basket.
1885 for examble:
1986 fruit_distribution("5 apples and 6 oranges", 19) ->19 - 5 - 6 = 8
@@ -47,4 +114,4 @@ def check(candidate):
47114 assert candidate("2 apples and 3 oranges",5) == 0
48115 assert candidate("1 apples and 100 oranges",120) == 19
49116```
50- -/
117+ -/
0 commit comments