Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
WeetHet committed Aug 7, 2024
1 parent 07b7fb4 commit bcae2de
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 4 deletions.
1 change: 0 additions & 1 deletion 051-remove-vowels.dfy
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 5 additions & 0 deletions 092-any_int.dfy
Original file line number Diff line number Diff line change
@@ -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;
}
44 changes: 44 additions & 0 deletions 095-check_dict_case.dfy
Original file line number Diff line number Diff line change
@@ -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<string, string>) 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;
}
27 changes: 27 additions & 0 deletions 152-compare.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
method Compare(scores: array<int>, guesses: array<int>) returns (result: array<int>)
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
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bcae2de

Please sign in to comment.