From bcae2de4f2b283515e5c4608ac39bce2d92eb695 Mon Sep 17 00:00:00 2001 From: WeetHet Date: Wed, 7 Aug 2024 20:45:53 +0300 Subject: [PATCH] More tests --- 051-remove-vowels.dfy | 1 - 092-any_int.dfy | 5 +++++ 095-check_dict_case.dfy | 44 +++++++++++++++++++++++++++++++++++++++++ 152-compare.dfy | 27 +++++++++++++++++++++++++ README.md | 6 +++--- 5 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 092-any_int.dfy create mode 100644 095-check_dict_case.dfy create mode 100644 152-compare.dfy diff --git a/051-remove-vowels.dfy b/051-remove-vowels.dfy index fba5625..a6ea988 100644 --- a/051-remove-vowels.dfy +++ b/051-remove-vowels.dfy @@ -1,4 +1,3 @@ - method remove_vowels(text : string) returns (s : string) ensures forall i : int :: i >= 0 && i < |s| ==> s[i] != 'a' && s[i] != 'e' && s[i] != 'i' && s[i] != 'o' && s[i] != 'u' ensures forall i : int :: i >= 0 && i < |s| ==> s[i] in text diff --git a/092-any_int.dfy b/092-any_int.dfy new file mode 100644 index 0000000..9eb4d66 --- /dev/null +++ b/092-any_int.dfy @@ -0,0 +1,5 @@ +method any_int(a: int, b: int, c: int) returns (r: bool) + ensures r == (a == b + c || b == a + c || c == a + b) +{ + r := a == b + c || b == a + c || c == a + b; +} \ No newline at end of file diff --git a/095-check_dict_case.dfy b/095-check_dict_case.dfy new file mode 100644 index 0000000..5bc9b7d --- /dev/null +++ b/095-check_dict_case.dfy @@ -0,0 +1,44 @@ +predicate IsLowerCase(s: string) +{ + forall i :: 0 <= i < |s| ==> 'a' <= s[i] <= 'z' +} + +predicate IsUpperCase(s: string) +{ + forall i :: 0 <= i < |s| ==> 'A' <= s[i] <= 'Z' +} + +method CheckDictCase(dict: map) returns (result: bool) + ensures dict == map[] ==> !result + ensures result ==> (forall k :: k in dict ==> IsLowerCase(k)) || (forall k :: k in dict ==> IsUpperCase(k)) + ensures !result ==> dict == map[] || ((exists k :: k in dict && !IsLowerCase(k)) && (exists k :: k in dict && !IsUpperCase(k))) +{ + if |dict| == 0 { + return false; + } + + var allLower := true; + var allUpper := true; + + var keys := dict.Keys; + while keys != {} + invariant allLower ==> forall j :: j in dict.Keys - keys ==> IsLowerCase(j) + invariant allUpper ==> forall j :: j in dict.Keys - keys ==> IsUpperCase(j) + invariant !allLower ==> exists j :: j in dict.Keys - keys && !IsLowerCase(j) + invariant !allUpper ==> exists j :: j in dict.Keys - keys && !IsUpperCase(j) + { + var k :| k in keys; + if !IsLowerCase(k) { + allLower := false; + } + if !IsUpperCase(k) { + allUpper := false; + } + if !allLower && !allUpper { + return false; + } + keys := keys - {k}; + } + + result := allLower || allUpper; +} \ No newline at end of file diff --git a/152-compare.dfy b/152-compare.dfy new file mode 100644 index 0000000..91c5bf3 --- /dev/null +++ b/152-compare.dfy @@ -0,0 +1,27 @@ +method Compare(scores: array, guesses: array) returns (result: array) + requires scores.Length == guesses.Length + ensures result.Length == scores.Length + ensures forall i :: 0 <= i < result.Length ==> + result[i] == if scores[i] == guesses[i] then 0 else abs(scores[i] - guesses[i]) +{ + result := new int[scores.Length]; + var i := 0; + while i < scores.Length + invariant 0 <= i <= scores.Length + invariant result.Length == scores.Length + invariant forall k :: 0 <= k < i ==> + result[k] == if scores[k] == guesses[k] then 0 else abs(scores[k] - guesses[k]) + { + if scores[i] == guesses[i] { + result[i] := 0; + } else { + result[i] := abs(scores[i] - guesses[i]); + } + i := i + 1; + } +} + +function abs(x: int): int +{ + if x < 0 then -x else x +} \ No newline at end of file diff --git a/README.md b/README.md index fd273d1..4e6735f 100644 --- a/README.md +++ b/README.md @@ -94,10 +94,10 @@ Current status: - [ ] 89. encrypt - [ ] 90. next_smallest - [ ] 91. is_bored -- [ ] 92. any_int +- [x] 92. any_int - [ ] 93. encode - [ ] 94. skjkasdkd -- [ ] 95. check_dict_case +- [x] 95. check_dict_case - [ ] 96. count_up_to - [ ] 97. multiply - [ ] 98. count_upper @@ -154,7 +154,7 @@ Current status: - [ ] 149. sorted_list_sum - [ ] 150. x_or_y - [ ] 151. double_the_difference -- [ ] 152. compare +- [x] 152. compare - [ ] 153. Strongest_Extension - [ ] 154. cycpattern_check - [ ] 155. even_odd_count