Skip to content

Commit

Permalink
27, 75, 76, 80 (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
gt22 authored Aug 15, 2024
1 parent 9e7521d commit c0dc8ae
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 4 deletions.
29 changes: 29 additions & 0 deletions 027-flip_case.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
predicate lower(c: char)
{
'a' <= c <= 'z'
}

predicate upper(c: char)
{
'A' <= c <= 'Z'
}
predicate alpha(c: char)
{
lower(c) || upper(c)
}

function flip_char(c: char) : (C: char)
ensures lower(c) <==> upper(C)
ensures upper(c) <==> lower(C)
{
if lower(c) then c - 'a' + 'A' else
if upper(c) then c + 'a' - 'A' else c
}

function flip_case(s: string) : (S: string)
ensures |S| == |s|
ensures forall i :: 0 <= i < |s| ==> (lower(s[i]) <==> upper(S[i]))
ensures forall i :: 0 <= i < |s| ==> (upper(s[i]) <==> lower(S[i]))
{
seq(|s|, i requires 0 <= i < |s| => flip_char(s[i]))
}
31 changes: 31 additions & 0 deletions 075-is_multiply_prime.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
predicate Prime(p: nat)
{
p > 1 &&
forall k :: 1 < k < p ==> p % k != 0
}

method is_multiply_prime(x: nat) returns (ans : bool)
requires x > 1
ensures ans <==> exists a: nat, b: nat, c: nat :: Prime(a) && Prime(b) && Prime(c) && x == a * b * c
{
for a := 2 to x
invariant forall i: nat, j: nat, k: nat :: (Prime(i) && Prime(j) && Prime(k) && i < a) ==> x != i * j * k
{
if Prime(a) {
for b := 2 to x
invariant forall j: nat, k: nat :: (Prime(j) && Prime(k) && j < b) ==> x != a * j * k
{
if Prime(b) {
for c := 2 to x
invariant forall k: nat :: (Prime(k) && k < c) ==> x != a * b * k
{
if Prime(c) && x == a * b * c {
return true;
}
}
}
}
}
}
return false;
}
39 changes: 39 additions & 0 deletions 076-is_simple_power.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function power(x: nat, y: nat): nat {
if y == 0 then 1 else x * power(x, y-1)
}

lemma power_unit(y: nat)
ensures power(1, y) == 1
{}

lemma power_monotonic(x: nat, y: nat, j: nat)
requires x > 0
requires j >= y
ensures power(x, j) >= power(x, y)
{}

method is_simple_power(x: nat, n: int) returns (ans : bool)
requires x > 0
ensures ans <==> exists y :: n == power(x, y)
{
if(x == 1) {
assert forall y :: power(x, y) == 1 by { forall y { power_unit(y); } }
assert n == 1 ==> n == power(x, 1);
return n == 1;
}
var acc := 1;
var i := 0;
while(acc < n)
invariant acc == power(x, i)
invariant forall j : nat :: j < i ==> power(x, j) < n
{
acc := x * acc;
i := i + 1;
}
if(acc == n) {
return true;
} else {
assert forall j : nat :: j >= i ==> power(x, j) > n by { forall j | j > i { power_monotonic(x, i, j); } }
return false;
}
}
31 changes: 31 additions & 0 deletions 080-is_happy.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function ThreeDistinct(s: string, i: int): bool
requires 0 < i < |s| - 1
{
(s[i - 1] != s[i]) && (s[i] != s[i + 1]) && (s[i - 1] != s[i + 1])
}

predicate Happy(s: string)
{
|s| >= 3 &&
forall i :: 0 < i < |s| - 1 ==> ThreeDistinct(s, i)
}

method IsHappy(s: string) returns (happy : bool)
ensures happy <==> Happy(s)
{
if |s| < 3 {
return false;
}

var i := 1;
while(i < |s| - 1)
invariant 0 < i <= |s| - 1
invariant forall j :: 0 < j < i ==> ThreeDistinct(s, j)
{
if !ThreeDistinct(s, i) {
return false;
}
i := i + 1;
}
return true;
}
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Current status:
- [x] 24. largest_divisor
- [ ] 25. factorize
- [ ] 26. remove_duplicates
- [ ] 27. flip_case
- [x] 27. flip_case
- [ ] 28. concatenate
- [ ] 29. filter_by_prefix
- [x] 30. get_positive
Expand Down Expand Up @@ -77,12 +77,12 @@ Current status:
- [x] 72. will_it_fly
- [ ] 73. smallest_change
- [x] 74. total_match
- [ ] 75. is_multiply_prime
- [ ] 76. is_simple_power
- [x] 75. is_multiply_prime
- [x] 76. is_simple_power
- [x] 77. iscube
- [x] 78. hex_key
- [ ] 79. decimal_to_binary
- [ ] 80. is_happy
- [x] 80. is_happy
- [ ] 81. numerical_letter_grade
- [x] 82. prime_length
- [x] 83. starts_one_ends
Expand Down

0 comments on commit c0dc8ae

Please sign in to comment.