-
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.
Merge pull request #12 from alex28sh/93,112
93, 112
- Loading branch information
Showing
25 changed files
with
909 additions
and
331 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
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 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,27 @@ | ||
from typing import cast, List, Dict, Set, Optional, Union, Tuple | ||
from nagini_contracts.contracts import * | ||
|
||
def solve(n : int) -> int: | ||
Requires((n) >= (0)) | ||
Ensures(Result() >= 0) | ||
Ensures((Result()) == (popcount(n))) | ||
r = int(0) # type : int | ||
d_0_m_ = int(0) # type : int | ||
d_0_m_ = n | ||
r = 0 | ||
while (d_0_m_) > (0): | ||
Invariant(((0) <= (d_0_m_)) and ((d_0_m_) <= (n))) | ||
Invariant(r >= 0) | ||
Invariant(((r) + (popcount(d_0_m_))) == (popcount(n))) | ||
r = (r) + ((d_0_m_ % 2)) | ||
d_0_m_ = (d_0_m_ // 2) | ||
return r | ||
|
||
@Pure | ||
def popcount(n : int) -> int : | ||
Requires(n >= 0) | ||
if n == 0: | ||
return 0 | ||
else: | ||
return (n % 2) + popcount(n // 2) | ||
|
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.