-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
method belowThreshold(l: seq<int>, t: int) returns (result: bool) | ||
ensures result <==> forall i :: 0 <= i < |l| ==> l[i] < t | ||
{ | ||
result := true; | ||
var i := 0; | ||
while i < |l| | ||
invariant 0 <= i <= |l| | ||
invariant result <==> forall j :: 0 <= j < i ==> l[j] < t | ||
{ | ||
if l[i] >= t { | ||
result := false; | ||
return; | ||
} | ||
i := i + 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
method any_int(a: int, b: int, c: int) returns (r: bool) | ||
ensures r == (a == b + c || b == a + c || c == a + b) | ||
{ | ||
r := a == b + c || b == a + c || c == a + b; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
predicate IsLowerCase(s: string) | ||
{ | ||
forall i :: 0 <= i < |s| ==> 'a' <= s[i] <= 'z' | ||
} | ||
|
||
predicate IsUpperCase(s: string) | ||
{ | ||
forall i :: 0 <= i < |s| ==> 'A' <= s[i] <= 'Z' | ||
} | ||
|
||
method CheckDictCase(dict: map<string, string>) returns (result: bool) | ||
ensures dict == map[] ==> !result | ||
ensures result ==> (forall k :: k in dict ==> IsLowerCase(k)) || (forall k :: k in dict ==> IsUpperCase(k)) | ||
ensures !result ==> dict == map[] || ((exists k :: k in dict && !IsLowerCase(k)) && (exists k :: k in dict && !IsUpperCase(k))) | ||
{ | ||
if |dict| == 0 { | ||
return false; | ||
} | ||
|
||
var allLower := true; | ||
var allUpper := true; | ||
|
||
var keys := dict.Keys; | ||
while keys != {} | ||
invariant allLower ==> forall j :: j in dict.Keys - keys ==> IsLowerCase(j) | ||
invariant allUpper ==> forall j :: j in dict.Keys - keys ==> IsUpperCase(j) | ||
invariant !allLower ==> exists j :: j in dict.Keys - keys && !IsLowerCase(j) | ||
invariant !allUpper ==> exists j :: j in dict.Keys - keys && !IsUpperCase(j) | ||
{ | ||
var k :| k in keys; | ||
if !IsLowerCase(k) { | ||
allLower := false; | ||
} | ||
if !IsUpperCase(k) { | ||
allUpper := false; | ||
} | ||
if !allLower && !allUpper { | ||
return false; | ||
} | ||
keys := keys - {k}; | ||
} | ||
|
||
result := allLower || allUpper; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
method Compare(scores: array<int>, guesses: array<int>) returns (result: array<int>) | ||
requires scores.Length == guesses.Length | ||
ensures result.Length == scores.Length | ||
ensures forall i :: 0 <= i < result.Length ==> | ||
result[i] == if scores[i] == guesses[i] then 0 else abs(scores[i] - guesses[i]) | ||
{ | ||
result := new int[scores.Length]; | ||
var i := 0; | ||
while i < scores.Length | ||
invariant 0 <= i <= scores.Length | ||
invariant result.Length == scores.Length | ||
invariant forall k :: 0 <= k < i ==> | ||
result[k] == if scores[k] == guesses[k] then 0 else abs(scores[k] - guesses[k]) | ||
{ | ||
if scores[i] == guesses[i] { | ||
result[i] := 0; | ||
} else { | ||
result[i] := abs(scores[i] - guesses[i]); | ||
} | ||
i := i + 1; | ||
} | ||
} | ||
|
||
function abs(x: int): int | ||
{ | ||
if x < 0 then -x else x | ||
} |