Skip to content

Commit

Permalink
126, 146 (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
WeetHet authored Aug 22, 2024
1 parent 5920048 commit 8fe5d73
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
65 changes: 65 additions & 0 deletions 126-is_sorted.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
method is_sorted(a: seq<int>) returns (f: bool)
ensures f == forall i, j :: 0 <= i <= j < |a| ==> a[i] <= a[j] && forall i :: 0 <= i < |a| ==> count_set(a, a[i]) <= 2
{
if |a| == 0 {
return true;
}
var is_asc := true;
var i := 1;
while i < |a|
invariant 1 <= i <= |a|
invariant is_asc == forall j, k :: 0 <= j < k < i ==> a[j] <= a[k]
{
if a[i - 1] > a[i] {
is_asc := false;
}
i := i + 1;
}

if !is_asc {
return false;
}

i := 0;

var has_no_more_that_2 := true;
while i < |a|
invariant 0 <= i <= |a|
invariant has_no_more_that_2 == forall j :: 0 <= j < i ==> count_set(a, a[j]) <= 2
{
var count := count_set(a, a[i]);
if count > 2 {
has_no_more_that_2 := false;
}
i := i + 1;
}
return has_no_more_that_2;
}


method count_sorted(a: seq<int>, x: int, pos: int) returns (count: int)
requires forall i, j :: 0 <= i <= j < |a| ==> a[i] <= a[j]
requires 0 <= pos < |a|
requires a[pos] == x
requires pos == 0 || a[pos - 1] < x
ensures count == count_set(a, x)
{
count := 0;
var i := pos;
ghost var positions := {};

while i < |a| && a[i] == x
invariant 0 <= i <= |a|
invariant positions == set j | 0 <= j < i && a[j] == x
invariant count == |positions|
{
count := count + 1;
positions := positions + {i};
i := i + 1;
}
assert positions == set j | 0 <= j < |a| && a[j] == x;
}

function count_set(a: seq<int>, x: int): int {
|set i | 0 <= i < |a| && a[i] == x|
}
35 changes: 35 additions & 0 deletions 146_specialFilter.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
function first_digit(n: int): int
requires n >= 0
ensures 0 <= first_digit(n) < 10
{
if n < 10 then n else first_digit(n / 10)
}

function last_digit(n: int): int
requires n >= 0
ensures 0 <= last_digit(n) < 10
ensures last_digit(n) == n % 10
{
n % 10
}

method specialFilter(s: seq<int>) returns (r: seq<int>)
ensures forall i :: 0 <= i < |r| ==> r[i] > 10
ensures forall x :: x in r ==> x in s
ensures forall i :: 0 <= i < |r| ==> first_digit(r[i]) % 2 == 1 && last_digit(r[i]) % 2 == 1
{
var i := 0;
r := [];
while i < |s|
invariant 0 <= i <= |s|
invariant forall x :: x in r ==> x in s
invariant forall i :: 0 <= i < |r| ==> r[i] > 10
invariant forall x :: x in r ==> x in s
invariant forall i :: 0 <= i < |r| ==> first_digit(r[i]) % 2 == 1 && last_digit(r[i]) % 2 == 1
{
if s[i] > 10 && last_digit(s[i]) % 2 == 1 && first_digit(s[i]) % 2 == 1 {
r := r + [s[i]];
}
i := i + 1;
}
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ Current status:
- [ ] 123. get_odd_collatz - sorting
- [ ] 124. valid_date
- [ ] 125. split_words - complex strings
- [ ] 126. is_sorted
- [x] 126. is_sorted
- [x] 127. intersection
- [ ] 128. prod_signs
- [ ] 129. minPath
Expand All @@ -148,7 +148,7 @@ Current status:
- [ ] 143. words_in_sentence - complex strings
- [ ] 144. simplify
- [ ] 145. order_by_points - sorting
- [ ] 146. specialFilter
- [x] 146. specialFilter
- [ ] 147. get_max_triples
- [x] 148. bf
- [x] 149. sorted_list_sum
Expand Down

0 comments on commit 8fe5d73

Please sign in to comment.