forked from leanprover/human-eval-lean
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHumanEval2.lean
More file actions
86 lines (63 loc) · 1.59 KB
/
HumanEval2.lean
File metadata and controls
86 lines (63 loc) · 1.59 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
module
/-!
# Problem 2
## Implementation
-/
def truncateNumber (x : Rat) : Rat :=
x - x.floor
/-!
## Tests
-/
example : truncateNumber (7/2) = 1/2 := by cbv
example : truncateNumber (133/100) = 33/100 := by cbv
example : truncateNumber (123456/1000) = 456/1000 := by cbv
/-!
## Verification
-/
/--
{lean}`x.floor` is the largest integer less than or equal to `x`.
In other words, if {given}`k : Int` is less than or equal to `x`, then `k ≤ x.floor`.
-/
theorem le_floor_of_le_self {x : Rat} {k : Int} (h : k ≤ x) :
k ≤ x.floor :=
Rat.le_floor_iff.mpr h
/--
Every rational number `x` is the sum of `x.floor` and `truncateNumber x`.
-/
theorem floor_add_truncateNumber :
x.floor + truncateNumber x = x := by
grind [truncateNumber]
theorem zero_le_truncateNumber :
0 ≤ truncateNumber x := by
grind [Rat.floor_le, truncateNumber]
theorem truncateNumber_lt_one :
truncateNumber x < 1 := by
grind [Rat.lt_floor, truncateNumber]
/-!
## Prompt
```python3
def truncate_number(number: float) -> float:
""" Given a positive floating point number, it can be decomposed into
and integer part (largest integer smaller than given number) and decimals
(leftover part always smaller than 1).
Return the decimal part of the number.
>>> truncate_number(3.5)
0.5
"""
```
## Canonical solution
```python3
return number % 1.0
```
## Tests
```python3
METADATA = {
'author': 'jt',
'dataset': 'test'
}
def check(candidate):
assert candidate(3.5) == 0.5
assert abs(candidate(1.33) - 0.33) < 1e-6
assert abs(candidate(123.456) - 0.456) < 1e-6
```
-/