-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
370 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from typing import cast, List, Dict, Set, Optional, Union, Tuple | ||
from nagini_contracts.contracts import * | ||
|
||
@Pure | ||
def contains_char(s : List[int], c : int, i : int, j : int) -> bool: | ||
Requires(Acc(list_pred(s))) | ||
Requires(Forall(int, lambda d_0_i_: | ||
not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(s)))) or (((97) <= ((s)[d_0_i_])) and (((s)[d_0_i_]) <= (122))))) | ||
Requires(0 <= i and i <= j and j <= len(s)) | ||
Requires(((97) <= (c)) and ((c) <= (122))) | ||
if i == j: | ||
return False | ||
else: | ||
return s[j - 1] == c or contains_char(s, c, i, j - 1) | ||
|
||
@Pure | ||
def count_chars_inter(s : List[int], c : int) -> int: | ||
Requires(Acc(list_pred(s))) | ||
Requires(Forall(int, lambda d_0_i_: | ||
not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(s)))) or (((97) <= ((s)[d_0_i_])) and (((s)[d_0_i_]) <= (122))))) | ||
Requires(((97) <= (c)) and ((c) <= (123))) | ||
if c == 97: | ||
return 0 | ||
else: | ||
return count_chars_inter(s, c - 1) + (1 if contains_char(s, c - 1, 0, len(s)) else 0) | ||
|
||
def count_distinct_characters(s : List[int]) -> int: | ||
Requires(Acc(list_pred(s))) | ||
Requires(Forall(int, lambda d_1_i_: | ||
not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(s)))) or (((97) <= ((s)[d_1_i_])) and (((s)[d_1_i_]) <= (122))))) | ||
Ensures(Acc(list_pred(s))) | ||
Ensures(Forall(int, lambda d_1_i_: | ||
not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(s)))) or (((97) <= ((s)[d_1_i_])) and (((s)[d_1_i_]) <= (122))))) | ||
Ensures((Result()) == count_chars_inter(s, 123)) | ||
c = int(0) # type : int | ||
d_2_i_ = int(97) # type : int | ||
while (d_2_i_) <= (122): | ||
Invariant(Acc(list_pred(s))) | ||
Invariant(((97) <= (d_2_i_)) and ((d_2_i_) <= (123))) | ||
Invariant(Forall(int, lambda d_1_i_: | ||
not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(s)))) or (((97) <= ((s)[d_1_i_])) and (((s)[d_1_i_]) <= (122))))) | ||
Invariant(c == count_chars_inter(s, d_2_i_)) | ||
if contains_char(s, d_2_i_, 0, len(s)): | ||
c = c + 1 | ||
d_2_i_ = d_2_i_ + 1 | ||
return c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from typing import cast, List, Dict, Set, Optional, Union | ||
from nagini_contracts.contracts import * | ||
|
||
def fizz__buzz(n : int) -> int: | ||
Requires(n >= 0) | ||
Ensures(Result() >= 0) | ||
Ensures((Result()) == fizz_buzz_fun(n)) | ||
result = int(0) # type : int | ||
result = 0 | ||
d_1_i_ = int(0) # type : int | ||
d_1_i_ = 0 | ||
while (d_1_i_) < (n): | ||
Invariant(((0) <= (d_1_i_)) and ((d_1_i_) <= (n))) | ||
Invariant(Forall(int, lambda x : (Implies(x >= 0 and x < n, | ||
fizz_buzz_fun(x + 1) == (count7__r(x) if ((x % 11 == 0) or (x % 13 == 0)) else 0) + fizz_buzz_fun(x)), [[fizz_buzz_fun(x + 1)]]))) | ||
Invariant(result == fizz_buzz_fun(d_1_i_)) | ||
if (((d_1_i_ % 11)) == (0)) or (((d_1_i_ % 13)) == (0)): | ||
d_4_cnt_ = int(0) # type : int | ||
d_4_cnt_ = count7(d_1_i_) | ||
result = (result) + (d_4_cnt_) | ||
d_1_i_ = (d_1_i_) + (1) | ||
return result | ||
|
||
@Pure | ||
def fizz_buzz_fun(n : int) -> int: | ||
Requires(n >= 0) | ||
Ensures(Result() >= 0) | ||
if n == 0: | ||
return 0 | ||
else: | ||
return (count7__r(n - 1) if ((n - 1) % 11 == 0 or (n - 1) % 13 == 0) else 0) + fizz_buzz_fun(n - 1) | ||
|
||
|
||
def count7(x : int) -> int: | ||
Requires(x >= 0) | ||
Ensures(Result() >= 0) | ||
Ensures((Result()) == (count7__r(x))) | ||
count = int(0) # type : int | ||
count = 0 | ||
d_6_y_ = int(0) # type : int | ||
d_6_y_ = x | ||
while (d_6_y_) > (0): | ||
Invariant(((0) <= (d_6_y_)) and ((d_6_y_) <= (x))) | ||
Invariant(((count) + (count7__r(d_6_y_))) == (count7__r(x))) | ||
if ((d_6_y_ % 10)) == (7): | ||
count = (count) + (1) | ||
d_6_y_ = d_6_y_ // 10 | ||
return count | ||
|
||
@Pure | ||
def count7__r(x : int) -> int : | ||
Requires(x >= 0) | ||
Ensures(Result() >= 0) | ||
if x == 0: | ||
return 0 | ||
else: | ||
return (x % 10 == 7) + count7__r(x // 10) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from typing import List | ||
from nagini_contracts.contracts import * | ||
|
||
def solve(s : List[int]) -> List[int]: | ||
Requires(Acc(list_pred(s), 1/2)) | ||
Ensures(Acc(list_pred(Result()), 1/2)) | ||
Ensures(Acc(list_pred(s), 1/2)) | ||
Ensures((len(s)) == (len(Result()))) | ||
Ensures(Implies(Forall(int, lambda d_0_i_: | ||
not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(s)))) or (not(is__alpha((s)[d_0_i_])))), Forall(int, lambda d_1_i_: | ||
not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(s)))) or (((s)[d_1_i_]) == ((Result())[((len(Result())) - (1)) - (d_1_i_)]))))) | ||
Ensures(Implies(Exists(int, lambda d_2_i_: | ||
(((0) <= (d_2_i_)) and ((d_2_i_) < (len(s)))) and (is__alpha((s)[d_2_i_]))), Forall(int, lambda d_3_i_: | ||
not (((0) <= (d_3_i_)) and ((d_3_i_) < (len(Result())))) or ((((Result())[d_3_i_]) == (flip__case((s)[d_3_i_])) if is__alpha((s)[d_3_i_]) else ((Result())[d_3_i_]) == ((s)[d_3_i_])))))) | ||
t : List[int] = [] | ||
d_4_flag_ = False # type : bool | ||
d_5_i_ = int(0) # type : int | ||
d_5_i_ = 0 | ||
while (d_5_i_) < (len(s)): | ||
Invariant(Acc(list_pred(t))) | ||
Invariant(Acc(list_pred(s), 1/2)) | ||
Invariant(((0) <= (d_5_i_)) and ((d_5_i_) <= (len(s)))) | ||
Invariant((len(t)) == (d_5_i_)) | ||
Invariant(Implies(d_4_flag_, Exists(int, lambda d_6_j_: | ||
(((0) <= (d_6_j_)) and ((d_6_j_) < (d_5_i_))) and (is__alpha((s)[d_6_j_]))))) | ||
Invariant(Implies(Exists(int, lambda d_6_j_: | ||
(((0) <= (d_6_j_)) and ((d_6_j_) < (d_5_i_))) and (is__alpha((s)[d_6_j_]))), d_4_flag_)) | ||
Invariant(Implies(not(d_4_flag_), Forall(int, lambda d_7_j_: | ||
Implies(((0) <= (d_7_j_)) and (d_7_j_) < (d_5_i_), not(is__alpha((s)[d_7_j_])))))) | ||
Invariant(Implies(Forall(int, lambda d_7_j_: | ||
Implies(((0) <= (d_7_j_)) and (d_7_j_) < (d_5_i_), not(is__alpha((s)[d_7_j_])))), not(d_4_flag_))) | ||
Invariant(Forall(int, lambda d_7_j_: | ||
(Implies(((0) <= (d_7_j_)) and ((d_7_j_) < (d_5_i_)), ((t)[d_7_j_]) == ((flip__case((s)[d_7_j_]) if is__alpha((s)[d_7_j_]) else (s)[d_7_j_]))), [[]]))) | ||
if is__alpha((s)[d_5_i_]): | ||
t = (t) + [flip__case((s)[d_5_i_])] | ||
d_4_flag_ = True | ||
else: | ||
t = (t) + [(s)[d_5_i_]] | ||
d_5_i_ = (d_5_i_) + (1) | ||
if not(d_4_flag_): | ||
t = reverse(t) | ||
return t | ||
|
||
def reverse(str : List[int]) -> List[int]: | ||
Requires(Acc(list_pred(str), 1/2)) | ||
Ensures(Acc(list_pred(str), 1/2)) | ||
Ensures(Acc(list_pred(Result()))) | ||
Ensures(str == Old(str)) | ||
Ensures((len(Result())) == (len(str))) | ||
Ensures(Forall(int, lambda d_11_k_: | ||
not (((0) <= (d_11_k_)) and ((d_11_k_) < (len(str)))) or (((Result())[d_11_k_]) == ((str)[((len(str)) - (1)) - (d_11_k_)])))) | ||
rev = list([int(0)] * 0) # type : List[int] | ||
rev = [] | ||
d_12_i_ = int(0) # type : int | ||
d_12_i_ = 0 | ||
while (d_12_i_) < (len(str)): | ||
Invariant(Acc(list_pred(str), 1/2)) | ||
Invariant(Acc(list_pred(rev))) | ||
Invariant(((d_12_i_) >= (0)) and ((d_12_i_) <= (len(str)))) | ||
Invariant((len(rev)) == (d_12_i_)) | ||
Invariant(Forall(int, lambda d_13_k_: | ||
not (((0) <= (d_13_k_)) and ((d_13_k_) < (d_12_i_))) or (((rev)[d_13_k_]) == ((str)[(len(str) - (1)) - (d_13_k_)])))) | ||
rev = (rev) + [(str)[(len(str) - (d_12_i_)) - (1)]] | ||
d_12_i_ = (d_12_i_) + (1) | ||
return rev | ||
|
||
@Pure | ||
def is__alpha(c : int) -> bool : | ||
return (((97) <= (c)) and ((c) <= (122))) or (((65) <= (c)) and ((c) <= (90))) | ||
|
||
@Pure | ||
def flip__case(c : int) -> int : | ||
if ((97) <= (c)) and ((c) <= (122)): | ||
return ((c) - (97)) + (65) | ||
elif True: | ||
return ((c) - (65)) + (97) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.