Skip to content

Commit

Permalink
041, 045, 046, 135, 136, 138 (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
WeetHet authored Aug 22, 2024
1 parent 6649c49 commit 5920048
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 6 deletions.
6 changes: 6 additions & 0 deletions 041-car_race_collision.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
method car_race_collision(n: int) returns (cnt: int)
requires n >= 0
ensures cnt == n * n
{
cnt := n * n;
}
6 changes: 6 additions & 0 deletions 045-triangle_area.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
method CalculateTriangleArea(a: real, h: real) returns (area: real)
requires h >= 0.0 && a >= 0.0
ensures area == (h * a) / 2.0
{
area := (h * a) / 2.0;
}
39 changes: 39 additions & 0 deletions 046-fib4.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function fib4_rec(n: nat): nat
decreases n
{
if n == 0 || n == 1 || n == 2 then 0
else if n == 3 then 1
else fib4_rec(n - 1) + fib4_rec(n - 2) + fib4_rec(n - 3) + fib4_rec(n - 4)
}

method fib4(n: nat) returns (result: nat)
ensures result == fib4_rec(n)
{
if n == 0 || n == 1 || n == 2 {
return 0;
}

if n == 3 {
return 1;
}

var a, b, c, d := 0, 0, 0, 1;
var i := 4;

while i <= n
invariant 4 <= i <= n + 1
invariant a == fib4_rec(i-4)
invariant b == fib4_rec(i-3)
invariant c == fib4_rec(i-2)
invariant d == fib4_rec(i-1)
{
var temp := d + c + b + a;
a := b;
b := c;
c := d;
d := temp;
i := i + 1;
}

return d;
}
21 changes: 21 additions & 0 deletions 135_can_arrange.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
method can_arrange(arr: seq<int>) returns (pos: int)
requires |arr| > 0
requires forall i, j :: 0 <= i < j < |arr| ==> arr[i] != arr[j]
ensures pos == -1 ==> forall i :: 1 <= i < |arr| ==> arr[i] >= arr[i - 1]
ensures pos >= 0 ==> 1 <= pos < |arr| && arr[pos] < arr[pos - 1]
ensures pos >= 0 ==> forall i :: pos < i < |arr| ==> arr[i] >= arr[i - 1]
{
var i := 1;
pos := -1;
while i < |arr|
invariant 1 <= i <= |arr|
invariant pos == -1 ==> forall j :: 1 <= j < i ==> arr[j] >= arr[j - 1]
invariant pos >= 0 ==> 1 <= pos < i && arr[pos] < arr[pos - 1]
invariant pos >= 0 ==> forall j :: pos < j < i ==> arr[j] >= arr[j - 1]
{
if arr[i] < arr[i - 1] {
pos := i;
}
i := i + 1;
}
}
41 changes: 41 additions & 0 deletions 136-largest_smallest_integers.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
datatype Option<T> = None | Some(value: T)

function get_value(o: Option<int>): int
requires o.Some?
ensures get_value(o) == o.value
{
o.value
}

method largest_smallest_integers(arr: seq<int>) returns (a: Option<int>, b: Option<int>)
ensures a.None? ==> forall i :: 0 <= i < |arr| ==> arr[i] >= 0
ensures a.Some? ==> get_value(a) in arr && get_value(a) < 0
ensures a.Some? ==> forall i :: 0 <= i < |arr| && arr[i] < 0 ==> arr[i] <= get_value(a)

ensures b.None? ==> forall i :: 0 <= i < |arr| ==> arr[i] <= 0
ensures b.Some? ==> get_value(b) in arr && get_value(b) > 0
ensures b.Some? ==> forall i :: 0 <= i < |arr| && arr[i] > 0 ==> arr[i] >= get_value(b)
{
var i := 0;
a := None;
b := None;
while i < |arr|
invariant 0 <= i <= |arr|
invariant a.None? ==> forall j :: 0 <= j < i ==> arr[j] >= 0
invariant a.Some? ==> get_value(a) in arr && get_value(a) < 0
invariant a.Some? ==> forall j :: 0 <= j < i && arr[j] < 0 ==> arr[j] <= get_value(a)

invariant b.None? ==> forall j :: 0 <= j < i ==> arr[j] <= 0
invariant b.Some? ==> get_value(b) in arr && get_value(b) > 0
invariant b.Some? ==> forall j :: 0 <= j < i && arr[j] > 0 ==> arr[j] >= get_value(b)
{
if arr[i] < 0 && (a.None? || arr[i] >= get_value(a)) {
a := Some(arr[i]);
}

if arr[i] > 0 && (b.None? || arr[i] <= get_value(b)) {
b := Some(arr[i]);
}
i := i + 1;
}
}
5 changes: 5 additions & 0 deletions 138_is_equal_to_sum_even.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
method is_equal_to_sum_even(n: int) returns (b : bool)
ensures b <==> n % 2 == 0 && n >= 8 // 2 + 2 + 2 + (n - 6)
{
b := n % 2 == 0 && n >= 8;
}
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ Current status:
- [ ] 38. encode_cyclic
- [ ] 39. ~prime_fib~ -- unbounded loop, probably impossible
- [x] 40. triples_sum_to_zero
- [ ] 41. car_race_collision
- [x] 41. car_race_collision
- [x] 42. incr_list
- [x] 43. pairs_sum_to_zero
- [ ] 44. change_base
- [ ] 45. triangle_area
- [ ] 46. fib4
- [x] 45. triangle_area
- [x] 46. fib4
- [ ] 47. median
- [x] 48. is_palindrome
- [ ] 49. modp
Expand Down Expand Up @@ -137,10 +137,10 @@ Current status:
- [ ] 132. is_nested
- [x] 133. sum_squares
- [ ] 134. check_if_last_char_is_a_letter
- [ ] 135. can_arrange
- [ ] 136. largest_smallest_integers
- [x] 135. can_arrange
- [x] 136. largest_smallest_integers
- [ ] 137. compare_one - ill-typed
- [ ] 138. is_equal_to_sum_even
- [x] 138. is_equal_to_sum_even
- [x] 139. special_factorial
- [ ] 140. fix_spaces - complex strings
- [ ] 141. file_name_check
Expand Down

0 comments on commit 5920048

Please sign in to comment.