forked from JetBrains-Research/HumanEval-Dafny
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path007-filter_by_substring.dfy
44 lines (42 loc) · 993 Bytes
/
007-filter_by_substring.dfy
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
method checkSubstring(s: string, sub: string) returns (result: bool)
{
// impl-start
result := false;
if (|sub| == 0)
{
result := true;
}
else if (|s| >= |sub|)
{
for i := 0 to |s| - |sub|
{
if (s[i..i + |sub|] == sub)
{
result := true;
}
}
}
// impl-end
}
method filter_by_substring(strings: seq<string>, substring: string) returns (res : seq<string>)
// post-conditions-start
ensures |res| <= |strings|
ensures (forall s :: s in res ==> s in strings)
// post-conditions-end
{
// impl-start
res := [];
for i := 0 to |strings|
// invariants-start
invariant |res| <= i
invariant (forall s :: s in res ==> s in strings)
// invariants-end
{
var check : bool := checkSubstring(strings[i], substring);
if (check)
{
res := res + [strings[i]];
}
}
// impl-end
}