-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathHumanEval101.lean
More file actions
159 lines (123 loc) · 4.27 KB
/
HumanEval101.lean
File metadata and controls
159 lines (123 loc) · 4.27 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
module
import Std
open Std Std.Do
set_option mvcgen.warning false
def delimiters : TreeSet Char := .ofList [' ', ',', '\t', '\n', '\r']
def wordsString (str : String) : Array String :=
str.split (· ∈ delimiters)
|>.filter (! ·.isEmpty)
|>.map String.Slice.toString
|>.toArray
def wordsString₂ (str : String) : Array String := Id.run do
let mut words : Array String := #[]
let mut currentWord : String := ""
for c in str.chars do
if c ∈ delimiters then
if ! currentWord.isEmpty then
words := words.push currentWord
currentWord := ""
else
currentWord := currentWord.push c
if ! currentWord.isEmpty then
words := words.push currentWord
return words
/-!
## Tests
-/
example : wordsString "Hi, my name is John" = #["Hi", "my", "name", "is", "John"] := by
native_decide
example : wordsString "One, two, three, four, five, six" =
#["One", "two", "three", "four", "five", "six"] := by
native_decide
example : wordsString "Hi, my name" = #["Hi", "my", "name"] := by
native_decide
example : wordsString "One,, two, three, four, five, six," =
#["One", "two", "three", "four", "five", "six"] := by
native_decide
example : wordsString "ahmed , gamal" = #["ahmed", "gamal"] := by
native_decide
example : wordsString "" = #[] := by
native_decide
/-!
# Verification
-/
@[grind =]
theorem not_contains_empty {c : Char} :
"".contains c = false := by
sorry
@[grind =]
theorem contains_push {s : String} {c d : Char} :
(s.push c).contains d = (s.contains d || c = d) := by
sorry
theorem not_contains_of_mem_delimiters {s : String} {c : Char} (h : c ∈ delimiters) :
(wordsString₂ s).all (! ·.contains c) := by
generalize hret : wordsString₂ s = ret
apply Id.of_wp_run_eq hret
mvcgen
case inv1 => exact ⇓⟨_, currentWord, words⟩ => ⌜¬ currentWord.contains c ∧ words.all (! ·.contains c)⌝
all_goals grind
@[simp, grind =]
theorem toList_chars_empty : "".chars.toList = [] := by
sorry
@[simp, grind =]
theorem isEmpty_empty : "".isEmpty := by
sorry
@[simp, grind =]
theorem wordsString₂_empty :
wordsString₂ "" = #[] := by
simp [wordsString₂, ← Iter.forIn_toList]
theorem wordsString₂_push_of_mem {s : String} {c : Char} (h : c ∈ delimiters) :
wordsString₂ (str.push c) = wordsString₂ str := by
sorry
theorem wordsString₂_push {s : String} {c : Char} :
wordsString₂ (str.push c) =
if c ∈ delimiters then
wordsString₂ str
else if str.startPos.get?.all (· ∈ delimiters) then
(wordsString₂ str).push (Char.toString c)
else
(wordsString₂ str).modify ((wordsString₂ str).size - 1) (·.push c) := by
sorry
theorem wordsString_eq_append {s t : String} {c : Char} (h : c ∈ delimiters) :
wordsString₂ (s ++ Char.toString c ++ t) = wordsString₂ s ++ wordsString₂ t := by
sorry
/-!
## Prompt
```python3
def words_string(s):
"""
You will be given a string of words separated by commas or spaces. Your task is
to split the string into words and return an array of the words.
For example:
words_string("Hi, my name is John") == ["Hi", "my", "name", "is", "John"]
words_string("One, two, three, four, five, six") == ["One", "two", "three", "four", "five", "six"]
"""
```
## Canonical solution
```python3
if not s:
return []
s_list = []
for letter in s:
if letter == ',':
s_list.append(' ')
else:
s_list.append(letter)
s_list = "".join(s_list)
return s_list.split()
```
## Tests
```python3
def check(candidate):
# Check some simple cases
assert True, "This prints if this assert fails 1 (good for debugging!)"
assert candidate("Hi, my name is John") == ["Hi", "my", "name", "is", "John"]
assert candidate("One, two, three, four, five, six") == ["One", "two", "three", "four", "five", "six"]
assert candidate("Hi, my name") == ["Hi", "my", "name"]
assert candidate("One,, two, three, four, five, six,") == ["One", "two", "three", "four", "five", "six"]
# Check some edge cases that are easy to work out by hand.
assert True, "This prints if this assert fails 2 (also good for debugging!)"
assert candidate("") == []
assert candidate("ahmed , gamal") == ["ahmed", "gamal"]
```
-/