diff --git a/126-is_sorted.dfy b/126-is_sorted.dfy new file mode 100644 index 0000000..44ca115 --- /dev/null +++ b/126-is_sorted.dfy @@ -0,0 +1,65 @@ +method is_sorted(a: seq) 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, 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, x: int): int { + |set i | 0 <= i < |a| && a[i] == x| +} diff --git a/146_specialFilter.dfy b/146_specialFilter.dfy new file mode 100644 index 0000000..ef32af3 --- /dev/null +++ b/146_specialFilter.dfy @@ -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) returns (r: seq) + 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; + } +} diff --git a/README.md b/README.md index 15e6e8d..c518cb8 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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