Skip to content

Commit

Permalink
38,56,90,119,132 (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex28sh authored Jan 6, 2025
1 parent 9894ba7 commit 20e5d30
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 4 deletions.
44 changes: 44 additions & 0 deletions 038-encode_cyclic.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
method encode_cyclic(s: seq<int>) returns (res: seq<int>)
// post-conditions-start
ensures |s| == |res|
ensures forall i :: 0 <= i < |s| - |s| % 3 ==> (i % 3 == 0 ==> res[i] == s[i + 1])
ensures forall i :: 0 <= i < |s| - |s| % 3 ==> (i % 3 == 1 ==> res[i] == s[i + 1])
ensures forall i :: 0 <= i < |s| - |s| % 3 ==> (i % 3 == 2 ==> res[i] == s[i - 2])
ensures forall i :: |s| - |s| % 3 <= i < |s| ==> (res[i] == s[i])
// post-conditions-end
{
// impl-start
res := s;
var i := 0;
while i + 2 < |s|
// invariants-start
invariant 0 <= i <= |s|
invariant |s| == |res|
invariant i % 3 == 0
invariant forall j :: i <= j < |s| ==> (res[j] == s[j])
invariant forall j :: 0 <= j < i ==> (j % 3 == 0 ==> res[j] == s[j + 1])
invariant forall j :: 0 <= j < i ==> (j % 3 == 1 ==> res[j] == s[j + 1])
invariant forall j :: 0 <= j < i ==> (j % 3 == 2 ==> res[j] == s[j - 2])
// invariants-end
{
res := res[i := s[i + 1]];
res := res[i + 1 := s[i + 2]];
res := res[i + 2 := s[i]];
i := i + 3;
}
// impl-end
}

method decode_cyclic(s: seq<int>) returns (res: seq<int>)
// post-conditions-start
ensures |s| == |res|
ensures forall i :: |s| - |s| % 3 <= i < |s| ==> (res[i] == s[i])
ensures forall i :: 0 <= i < |s| - |s| % 3 ==> (i % 3 == 0 ==> res[i] == s[i + 2])
ensures forall i :: 0 <= i < |s| - |s| % 3 ==> (i % 3 == 1 ==> res[i] == s[i - 1])
// post-conditions-end
{
// impl-start
var intermediate := encode_cyclic(s);
res := encode_cyclic(intermediate);
// impl-end
}
40 changes: 40 additions & 0 deletions 056-correct_bracketing.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
function CalcBal(s: seq<int>, i: int, j: int) : int
requires 0 <= i <= j <= |s|
{
if i == j then 0
else (if s[j - 1] == 0 then 1 else -1) + CalcBal(s, i, j - 1)
}

method correct_bracketing(s: seq<int>) returns (b: bool)
// pre-conditions-start
requires forall i :: 0 <= i < |s| ==> s[i] == 0 || s[i] == 1
// pre-conditions-end
// post-conditions-start
ensures (forall i :: 0 <= i <= |s| ==> CalcBal(s, 0, i) >= 0) ==> b
ensures b ==> (forall i :: 0 <= i <= |s| ==> CalcBal(s, 0, i) >= 0)
// post-conditions-end
{
// impl-start
var i := 0;
var depth := 0;
b := true;
while i < |s|
// invariants-start
invariant 0 <= i <= |s|
invariant depth == CalcBal(s, 0, i)
invariant (forall j :: 0 <= j <= i ==> CalcBal(s, 0, j) >= 0) ==> b
invariant b ==> (forall j :: 0 <= j <= i ==> CalcBal(s, 0, j) >= 0)
// invariants-end
{
if s[i] == 0 {
depth := depth + 1;
} else {
depth := depth - 1;
}
if depth < 0 {
b := false;
}
i := i + 1;
}
// impl-end
}
57 changes: 57 additions & 0 deletions 090-next_smallest.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
datatype Option<T> = None | Some(T)


function getVal(mx : Option<int>) : int
requires mx != None
{
match mx {
case Some(n) => n
}
}

method rolling_max(s: seq<int>) returns (res: Option<int>)
// post-conditions-start
ensures res == None <==> |s| < 2
ensures res != None ==> exists x :: 0 <= x < |s| && s[x] == getVal(res)
ensures forall x, y :: 0 <= x < y < |s| ==> s[x] <= getVal(res) || s[y] <= getVal(res)
// post-conditions-end
{
// impl-start
if |s| < 2 {
return None;
}

res := None;
var mx := s[0];
var i := 1;

while i < |s|
// invariants-start
invariant 0 <= i <= |s|
invariant i > 1 ==> res != None
invariant exists x :: 0 <= x < |s| && s[x] == mx
invariant res != None ==> exists x :: 0 <= x < |s| && s[x] == getVal(res)
invariant i == 1 ==> res == None
invariant i > 1 ==> getVal(res) <= mx
invariant forall x :: 0 <= x < i ==> s[x] == mx || (res != None && s[x] <= getVal(res))
invariant i > 1 ==> forall x, y :: 0 <= x < y < i ==> s[x] <= getVal(res) || s[y] <= getVal(res)
// invariants-end
{
if s[i] > mx {
res := Some(mx);
mx := s[i];
} else {
match res {
case None => res := Some(s[i]);
case Some(n1) =>
{
if (s[i] > n1) {
res := Some(s[i]);
}
}
}
}
i := i + 1;
}
// impl-end
}
67 changes: 67 additions & 0 deletions 119-match_parens.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
function CalcBal(s: seq<int>, i: int, j: int, acc: int) : int
requires 0 <= i <= j <= |s|
{
if i == j then acc
else (if s[j - 1] == 0 then 1 else -1) + CalcBal(s, i, j - 1, acc)
}

method checkFixed(s1: seq<int>, s2: seq<int>) returns (b: bool)
// pre-conditions-start
requires forall i :: 0 <= i < |s1| ==> s1[i] == 0 || s1[i] == 1
requires forall i :: 0 <= i < |s2| ==> s2[i] == 0 || s2[i] == 1
// pre-conditions-end
// post-conditions-start
ensures b ==> forall i :: 0 <= i <= |s1| ==> CalcBal(s1, 0, i, 0) >= 0
ensures b ==> forall i :: 0 <= i <= |s2| ==> CalcBal(s1, 0, |s1|, 0) + CalcBal(s2, 0, i, 0) >= 0
ensures !b ==> (exists i :: 0 <= i <= |s1| && CalcBal(s1, 0, i, 0) < 0) || (exists i :: 0 <= i <= |s2| && CalcBal(s1, 0, |s1|, 0) + CalcBal(s2, 0, i, 0) < 0)
// post-conditions-end
{
// impl-start
var bal := 0;
var i := 0;
while i < |s1|
// invariants-start
invariant 0 <= i <= |s1|
invariant bal == CalcBal(s1, 0, i, 0)
invariant forall j :: 0 <= j < i ==> CalcBal(s1, 0, j, 0) >= 0
// invariants-end
{
if s1[i] == 0 {
bal := bal + 1;
} else {
bal := bal - 1;
}
if bal < 0 {
// assert-start
assert CalcBal(s1, 0, i + 1, 0) < 0;
// assert-end
return false;
}
i := i + 1;
}

i := 0;
while i < |s2|
// invariants-start
invariant 0 <= i <= |s2|
invariant bal == CalcBal(s1, 0, |s1|, 0) + CalcBal(s2, 0, i, 0)
invariant forall j :: 0 <= j < i ==> CalcBal(s1, 0, |s1|, 0) + CalcBal(s2, 0, j, 0) >= 0
// invariants-end
{
if s2[i] == 0 {
bal := bal + 1;
} else {
bal := bal - 1;
}
if bal < 0 {
// assert-start
assert CalcBal(s1, 0, |s1|, 0) + CalcBal(s2, 0, i + 1, 0) < 0;
assert exists i :: 0 <= i <= |s2| && CalcBal(s1, 0, |s1|, 0) + CalcBal(s2, 0, i, 0) < 0;
// assert-end
return false;
}
i := i + 1;
}
return true;
// impl-end
}
49 changes: 49 additions & 0 deletions 132-is_nested.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
method is_nested(s: seq<int>) returns (res: bool)
// post-conditions-start
ensures res == exists x, y, z, w :: 0 <= x < y < z < w < |s| && s[x] == 0 && s[y] == 0 && s[z] == 1 && s[w] == 1
// post-conditions-end
{
// impl-start
var bal := 0;
var i := 0;

while i < |s| && bal < 2
// invariants-start
invariant 0 <= i <= |s|
invariant (bal >= 1) == (exists x :: 0 <= x < i && s[x] == 0)
invariant (bal >= 2) == (exists x, y :: 0 <= x < y < i && s[x] == 0 && s[y] == 0)
invariant !(exists x, y, z :: 0 <= x < y < z < i && s[x] == 0 && s[y] == 0 && s[z] == 1)
// invariants-end
{
if s[i] == 0 {
bal := bal + 1;
}
i := i + 1;
}

if bal < 2 {
return false;
}

while i < |s| && bal > 0
// invariants-start
invariant 0 <= i <= |s|
invariant 0 <= bal <= 2
invariant exists x, y :: 0 <= x && x < y && y < i && s[x] == 0 && s[y] == 0
invariant (bal <= 1) == (exists x, y, z :: 0 <= x < y < z < i && s[x] == 0 && s[y] == 0 && s[z] == 1)
invariant (bal == 0) == (exists x, y, z, w :: 0 <= x < y < z < w < i && s[x] == 0 && s[y] == 0 && s[z] == 1 && s[w] == 1)
// invariants-end
{
if s[i] == 1 {
bal := bal - 1;
}
i := i + 1;
}

if bal > 0 {
return false;
}

return true;
// impl-end
}
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Current status:
- [x] 35. max_element
- [x] 36. fizz_buzz
- [x] 37. sort_even - sorting
- [ ] 38. encode_cyclic
- [x] 38. encode_cyclic
- [ ] 39. ~prime_fib~ -- unbounded loop, probably impossible
- [x] 40. triples_sum_to_zero
- [x] 41. car_race_collision
Expand All @@ -58,7 +58,7 @@ Current status:
- [x] 53. add
- [x] 54. same_chars
- [x] 55. fib
- [ ] 56. correct_bracketing
- [x] 56. correct_bracketing
- [x] 57. monotonic
- [x] 58. common
- [x] 59. largest_prime_factor
Expand Down Expand Up @@ -92,7 +92,7 @@ Current status:
- [x] 87. get_row - sorting
- [x] 88. sort_array - sorting
- [x] 89. encrypt
- [ ] 90. next_smallest - nullable
- [x] 90. next_smallest - nullable
- [ ] 91. is_bored - complex strings
- [x] 92. any_int
- [x] 93. encode
Expand Down Expand Up @@ -121,7 +121,7 @@ Current status:
- [x] 116. sort_array - sorting
- [ ] 117. select_words - complex strings
- [x] 118. get_closest_vowel
- [ ] 119. match_parens
- [x] 119. match_parens
- [x] 120. maximum - sorting
- [x] 121. solution - sum
- [x] 122. add_elements
Expand Down

0 comments on commit 20e5d30

Please sign in to comment.