forked from leanprover/human-eval-lean
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHumanEval67.lean
More file actions
117 lines (98 loc) · 4.4 KB
/
HumanEval67.lean
File metadata and controls
117 lines (98 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
module
import Std.Data.String.ToNat
def fruitDistribution (s : String) (n : Nat) : Nat :=
n - ((s.split ' ').filterMap String.Slice.toNat?).fold (init := 0) (· + ·)
example : fruitDistribution "5 apples and 6 oranges" 19 = 8 := by native_decide
example : fruitDistribution "5 apples and 6 oranges" 21= 10 := by native_decide
example : fruitDistribution "0 apples and 1 oranges" 3 = 2 := by native_decide
example : fruitDistribution "1 apples and 0 oranges" 3 = 2 := by native_decide
example : fruitDistribution "2 apples and 3 oranges" 100 = 95 := by native_decide
example : fruitDistribution "2 apples and 3 oranges" 5 = 0 := by native_decide
example : fruitDistribution "1 apples and 100 oranges" 120 = 19 := by native_decide
inductive Word
| number : Nat → Word
| nonNumber : (s : String) →
(hs : s.isNat = false := by simp [← Bool.not_eq_true, String.isNat_iff]) →
(hs' : ' ' ∉ s.toList := by simp) → Word
def Word.toNat? : Word → Option Nat
| .number n => some n
| .nonNumber .. => none
def Word.toString : Word → String
| .number n => ToString.toString n
| .nonNumber s _ _ => s
@[simp]
theorem Word.toNat?_comp_toString : String.toNat? ∘ Word.toString = Word.toNat? := by
ext1 w
match w with
| .number n => simp [Word.toNat?, Word.toString]
| .nonNumber s hs hs' => simp [Word.toNat?, Word.toString, hs]
@[simp]
theorem Word.space_notin_toList_toString {w : Word} : ' ' ∉ w.toString.toList := by
match w with
| .number n =>
simp only [toString, Nat.toString_eq_repr, Nat.toList_repr, ↓Char.isValue]
intro h
simpa using Nat.isDigit_of_mem_toDigits (by decide) (by decide) h
| .nonNumber _ _ hs' => exact hs'
@[simp]
theorem String.isNat_empty : "".isNat = false := by
simp [← Bool.not_eq_true, isNat_iff]
@[simp]
theorem String.toNat?_empty : "".toNat? = none := by
simp
theorem fruitDistribution_intercalate_toString (l : List Word) (n : Nat) :
fruitDistribution (" ".intercalate (l.map Word.toString)) n = n - (l.filterMap Word.toNat?).sum := by
rw [fruitDistribution]
simp +instances only [String.reduceToSingleton]
rw [← Std.Iter.foldl_toList, Std.Iter.toList_filterMap, ← String.Slice.toNat?_comp_copy,
← List.filterMap_map, String.toList_split_intercalate (by simp), ← List.sum_eq_foldl]
simp only [List.map_eq_nil_iff]
split <;> simp_all
theorem fruitDistribution_sentence {n₁ n₂ n : Nat} :
fruitDistribution (toString n₁ ++ " apples and " ++ toString n₂ ++ " oranges") n = n - n₁ - n₂ := by
have := fruitDistribution_intercalate_toString [.number n₁, .nonNumber "apples", .nonNumber "and", .number n₂, .nonNumber "oranges"] n
simp only [List.map_cons, Word.toString, Nat.toString_eq_repr, List.map_nil,
String.intercalate_cons_cons, String.reduceAppend, String.intercalate_singleton, Word.toNat?,
Option.some.injEq, List.filterMap_cons_some, List.filterMap_cons_none, List.filterMap_nil,
List.sum_cons, List.sum_nil, Nat.add_zero, ← Nat.sub_sub] at this
rw [← this]
congr 1
simp [← String.toList_inj]
/-!
## Prompt
```python3
def fruit_distribution(s,n):
"""
In this task, you will be given a string that represents a number of apples and oranges
that are distributed in a basket of fruit this basket contains
apples, oranges, and mango fruits. Given the string that represents the total number of
the oranges and apples and an integer that represent the total number of the fruits
in the basket return the number of the mango fruits in the basket.
for examble:
fruit_distribution("5 apples and 6 oranges", 19) ->19 - 5 - 6 = 8
fruit_distribution("0 apples and 1 oranges",3) -> 3 - 0 - 1 = 2
fruit_distribution("2 apples and 3 oranges", 100) -> 100 - 2 - 3 = 95
fruit_distribution("100 apples and 1 oranges",120) -> 120 - 100 - 1 = 19
"""
```
## Canonical solution
```python3
lis = list()
for i in s.split(' '):
if i.isdigit():
lis.append(int(i))
return n - sum(lis)
```
## Tests
```python3
def check(candidate):
# Check some simple cases
assert candidate("5 apples and 6 oranges",19) == 8
assert candidate("5 apples and 6 oranges",21) == 10
assert candidate("0 apples and 1 oranges",3) == 2
assert candidate("1 apples and 0 oranges",3) == 2
assert candidate("2 apples and 3 oranges",100) == 95
assert candidate("2 apples and 3 oranges",5) == 0
assert candidate("1 apples and 100 oranges",120) == 19
```
-/