-
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.
Minor fixes, implementation of 018-how_many_times (#52)
Minor fixes to 011-string_xor, 012-longest, 020-find-closest_elements, 021-rescale_to_unit. Stylistic change of rewriting `while` to `for` when the latter would be more idiomatic.
- Loading branch information
Showing
20 changed files
with
117 additions
and
181 deletions.
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
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
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
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
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
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 |
---|---|---|
@@ -1,42 +1,42 @@ | ||
method xor(a : char, b : char) returns (result : char) | ||
// post-conditions-start | ||
ensures result == (if a == b then '0' else '1') | ||
// post-conditions-end | ||
predicate represents_byte(a: char) | ||
{ | ||
// impl-start | ||
if (a == b) { | ||
result := '0'; | ||
} else { | ||
result := '1'; | ||
} | ||
// impl-end | ||
a in "01" | ||
} | ||
|
||
function char_xor(a: char, b: char): char | ||
requires represents_byte(a) | ||
requires represents_byte(b) | ||
{ | ||
if (a == b) then | ||
'0' | ||
else | ||
'1' | ||
} | ||
|
||
method string_xor(a: string, b: string) returns (result: string) | ||
// pre-conditions-start | ||
requires |a| == |b| | ||
requires forall i :: 0 <= i < |a| ==> (a[i] == '0' || a[i] == '1') | ||
requires forall i :: 0 <= i < |b| ==> (b[i] == '0' || b[i] == '1') | ||
requires forall i :: 0 <= i < |a| ==> represents_byte(a[i]) | ||
requires forall i :: 0 <= i < |b| ==> represents_byte(b[i]) | ||
// pre-conditions-end | ||
// post-conditions-start | ||
ensures |result| == |a| | ||
ensures forall i :: 0 <= i < |result| ==> (result[i] == '0' || result[i] == '1') | ||
ensures forall i :: 0 <= i < |result| ==> result[i] == (if a[i] == b[i] then '0' else '1') | ||
ensures forall i :: 0 <= i < |result| ==> represents_byte(result[i]) | ||
ensures forall i :: 0 <= i < |result| ==> result[i] == char_xor(a[i], b[i]) | ||
// post-conditions-end | ||
{ | ||
// impl-start | ||
result := ""; | ||
var i : int := 0; | ||
while (i < |a|) | ||
for i := 0 to |a| | ||
// invariants-start | ||
invariant i >= 0 && i <= |a| | ||
invariant |result| == i | ||
invariant forall j :: 0 <= j < i ==> result[j] == (if a[j] == b[j] then '0' else '1') | ||
invariant forall i :: 0 <= i < |a| ==> represents_byte(a[i]) | ||
invariant forall i :: 0 <= i < |b| ==> represents_byte(b[i]) | ||
invariant forall j :: 0 <= j < i ==> result[j] == char_xor(a[j], b[j]) | ||
// invariants-end | ||
{ | ||
var bitResult := if a[i] == b[i] then '0' else '1'; | ||
var bitResult := char_xor(a[i], b[i]); | ||
result := result + [bitResult]; | ||
i := i + 1; | ||
} | ||
// impl-end | ||
} |
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
Oops, something went wrong.