diff --git a/Bench/026-remove_duplicates.py b/Bench/026-remove_duplicates.py new file mode 100644 index 0000000..d5b42fd --- /dev/null +++ b/Bench/026-remove_duplicates.py @@ -0,0 +1,44 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def remove__duplicates(a : List[int]) -> List[int]: + Requires(Acc(list_pred(a), 1/2)) + Ensures(Acc(list_pred(a), 1/2)) + Ensures(Acc(list_pred(Result()))) + Ensures(len(a) == len(Old(a))) + Ensures(len(a) >= len(Result())) + Ensures(Forall(int, lambda d_2_i_: + Implies(((0) <= (d_2_i_)) and ((d_2_i_) < (len(a))) and (count__rec(a, a[d_2_i_], len(a)) == 1), Exists(int, lambda d_3_i_: d_3_i_ >= 0 and d_3_i_ < len(Result()) and Result()[d_3_i_] == a[d_2_i_])))) + result = list([int(0)] * 0) # type : List[int] + result = [] + d_4_i_ = int(0) # type : int + d_4_i_ = 0 + a_old = list(a) + + while (d_4_i_) < (len(a)): + Invariant(Acc(list_pred(result))) + Invariant(Acc(list_pred(a), 1/2)) + Invariant(Acc(list_pred(a_old), 1/2)) + Invariant(len(a) == len(a_old)) + Invariant(Forall(int, lambda d_3_i_: (Implies(d_3_i_ >= 0 and d_3_i_ < len(a), a_old[d_3_i_] == a[d_3_i_])))) + Invariant(((0) <= (d_4_i_)) and ((d_4_i_) <= (len(a)))) + Invariant(len(result) <= d_4_i_) + Invariant(Forall(int, lambda d_2_i_: + (Implies(((0) <= (d_2_i_)) and ((d_2_i_) < (d_4_i_)) and (count__rec(a, a[d_2_i_], len(a)) == 1), Exists(int, lambda d_3_i_: d_3_i_ >= 0 and d_3_i_ < len(result) and result[d_3_i_] == a[d_2_i_])), + [[a[d_2_i_]]]))) + d_8_cnt_ = int(0) # type : int + d_8_cnt_ = count__rec(a, (a)[d_4_i_], len(a)) + if (d_8_cnt_) == (1): + result = (result) + [(a)[d_4_i_]] + Assert(count__rec(a, result[len(result) - 1], len(a)) == 1) + d_4_i_ = (d_4_i_) + (1) + return result + +@Pure +def count__rec(a : List[int], x : int, i : int) -> int : + Requires(Acc(list_pred(a), 1/2)) + Requires(((0) <= (i)) and ((i) <= (len(a)))) + if (i) == 0: + return 0 + else: + return (((a)[i - 1]) == (x)) + (count__rec(a, x, (i) - (1))) diff --git a/Bench/049-modp.py b/Bench/049-modp.py new file mode 100644 index 0000000..ffdc44a --- /dev/null +++ b/Bench/049-modp.py @@ -0,0 +1,28 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +@Pure +def modp__rec(n : int, p : int) -> int : + Requires((p) > (0)) + Requires((n) >= (0)) + if (n) == (0): + return (1 % p) + elif True: + return ((modp__rec((n) - (1), p)) * (2)) % p + +def modp(n : int, p : int) -> int: + Requires((p) > (0)) + Requires((n) >= (0)) + Ensures((Result()) == (modp__rec(n, p))) + r = int(0) # type : int + r = (1 % p) + d_0_i_ = int(0) # type : int + d_0_i_ = 0 + while (d_0_i_) < (n): + Invariant(((0) <= (d_0_i_)) and ((d_0_i_) <= (n))) + Invariant(Forall(int, lambda d_0_i_: (Implies(d_0_i_ >= 0 and d_0_i_ < n, modp__rec(d_0_i_ + 1, p) == (modp__rec(d_0_i_, p) * 2) % p)))) + Invariant((r) == (modp__rec(d_0_i_, p))) + Assert(modp__rec(d_0_i_ + 1, p) == (modp__rec(d_0_i_, p) * 2) % p) + r = (((r) * (2)) % p) + d_0_i_ = (d_0_i_) + (1) + return r diff --git a/Bench/064-vowel_count.py b/Bench/064-vowel_count.py new file mode 100644 index 0000000..2e72391 --- /dev/null +++ b/Bench/064-vowel_count.py @@ -0,0 +1,42 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def vowel__count(s : List[int]) -> int: + Requires(Acc(list_pred(s))) + Ensures(Acc(list_pred(s))) + Ensures((Result()) >= (0)) + Ensures(Result() == ((count_fun(0, len(s), s)) + ((1 if ((len(s)) > (0)) and (((s)[(len(s)) - (1)]) == (121)) else 0)))) + count = int(0) # type : int + count = 0 + d_1_i_ = int(0) # type : int + d_1_i_ = 0 + while (d_1_i_) < (len(s)): + Invariant(Acc(list_pred(s))) + Invariant(((0) <= (d_1_i_)) and ((d_1_i_) <= (len(s)))) + Invariant(count >= 0) + Invariant(Forall(int, lambda d_0_i_: + (Implies(d_0_i_ >= 0 and d_0_i_ < len(s), count_fun(0, d_0_i_ + 1, s) == count_fun(0, d_0_i_, s) + (1 if is__vowel(s[d_0_i_]) else 0)), [[count_fun(0, d_0_i_ + 1, s)]]))) + Invariant((count) == (count_fun(0, d_1_i_, s))) + Assert(count_fun(0, d_1_i_ + 1, s) == count_fun(0, d_1_i_, s) + (1 if is__vowel(s[d_1_i_]) else 0)) + if is__vowel((s)[d_1_i_]): + count = (count) + (1) + d_1_i_ = (d_1_i_) + (1) + count = (count) + ((1 if ((len(s)) > (0)) and (((s)[(len(s)) - (1)]) == (121)) else 0)) + return count + +@Pure +def count_fun(i : int, j : int, s : List[int]) -> int: + Requires(Acc(list_pred(s))) + Requires(((0) <= (i)) and ((i) <= (j)) and ((j) <= (len(s)))) + Ensures((Result()) >= (0)) + if i == j: + return 0 + else: + if is__vowel((s)[j - 1]): + return (1) + (count_fun(i, j - 1, s)) + else: + return count_fun(i, j - 1, s) + +@Pure +def is__vowel(c : int) -> bool : + return ((((((((((c) == (97)) or ((c) == (101))) or ((c) == (105))) or ((c) == (111))) or ((c) == (117))) or ((c) == (65))) or ((c) == (69))) or ((c) == (73))) or ((c) == (79))) or ((c) == (85)) diff --git a/Bench/069-search.py b/Bench/069-search.py new file mode 100644 index 0000000..fae2dcb --- /dev/null +++ b/Bench/069-search.py @@ -0,0 +1,30 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +@Pure +def freq_req(i : int, j : int, s : List[int], x : int) -> int: + Requires(Acc(list_pred(s))) + Requires(0 <= i and i <= j and j <= len(s)) + if i == j: + return 0 + else: + return ((s)[j - 1] == x) + (freq_req(i, j - 1, s, x)) + +def freq(s : List[int], x : int) -> int: + Requires(Acc(list_pred(s))) + Ensures(Acc(list_pred(s))) + Ensures(Result() == freq_req(0, len(s), s, x)) + count = int(0) # type : int + count = 0 + d_1_i_ = int(0) # type : int + d_1_i_ = 0 + while (d_1_i_) < (len(s)): + Invariant(Acc(list_pred(s))) + Invariant(((0) <= (d_1_i_)) and ((d_1_i_) <= (len(s)))) + Invariant(Forall(int, lambda y : (Implies(y >= 0 and y < len(s), freq_req(0, y + 1, s, x) == freq_req(0, y, s, x) + (s[y] == x)), [[freq_req(0, y + 1, s, x)]]))) + Invariant(count == freq_req(0, d_1_i_, s, x)) + Assert(freq_req(0, d_1_i_ + 1, s, x) == freq_req(0, d_1_i_, s, x) + (s[d_1_i_] == x)) + if ((s)[d_1_i_]) == (x): + count = (count) + (1) + d_1_i_ = (d_1_i_) + (1) + return count diff --git a/Bench/073-smallest_change.py b/Bench/073-smallest_change.py new file mode 100644 index 0000000..84923c4 --- /dev/null +++ b/Bench/073-smallest_change.py @@ -0,0 +1,34 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +@Pure +def smallest__change__fun(s : List[int], i : int, j : int) -> int: + Requires(Acc(list_pred(s))) + Requires(((0) <= (i)) and ((i) <= (j)) and ((j) <= (len(s) // 2))) + Ensures((Result()) >= (0)) + if i == j: + return 0 + else: + if (s)[j - 1] != (s)[len(s) - j]: + return 1 + (smallest__change__fun(s, i, j - 1)) + else: + return smallest__change__fun(s, i, j - 1) + +def smallest__change(s : List[int]) -> int: + Requires(Acc(list_pred(s))) + Ensures(Acc(list_pred(s))) + Ensures((Result()) == (smallest__change__fun(s, 0, len(s) // 2))) + c = int(0) # type : int + c = 0 + d_1_i_ = int(0) # type : int + d_1_i_ = 0 + while (d_1_i_) < ((len(s) // 2)): + Invariant(Acc(list_pred(s))) + Invariant(((0) <= (d_1_i_)) and ((d_1_i_) <= ((len(s) // 2)))) + Invariant(Forall(int, lambda d_0_i_: (Implies(d_0_i_ >= 0 and d_0_i_ < len(s) // 2, smallest__change__fun(s, 0, d_0_i_ + 1) == (smallest__change__fun(s, 0, d_0_i_) + 1 if (s)[d_0_i_] != (s)[len(s) - d_0_i_ - 1] else smallest__change__fun(s, 0, d_0_i_))), [[smallest__change__fun(s, 0, d_0_i_ + 1)]]))) + Invariant(c == smallest__change__fun(s, 0, d_1_i_)) + Assert(smallest__change__fun(s, 0, d_1_i_ + 1) == (smallest__change__fun(s, 0, d_1_i_) + 1 if (s)[d_1_i_] != (s)[len(s) - d_1_i_ - 1] else smallest__change__fun(s, 0, d_1_i_))) + if ((s)[d_1_i_]) != ((s)[((len(s)) - (1)) - (d_1_i_)]): + c = (c) + (1) + d_1_i_ = (d_1_i_) + (1) + return c \ No newline at end of file diff --git a/Bench/089-encrypt.py b/Bench/089-encrypt.py new file mode 100644 index 0000000..f4fe5ca --- /dev/null +++ b/Bench/089-encrypt.py @@ -0,0 +1,40 @@ +from typing import cast, List, Dict, Set, Optional, Union, Tuple +from nagini_contracts.contracts import * + +@Pure +def rot__sym(c : int) -> int : + Requires(c >= 0 and c <= 25) + Ensures(Result() >= 0 and Result() <= 25) + d_0_alph_ = c - 0 # type : int + return ((d_0_alph_) + ((2) * (2))) % 26 + +def encrypt(s : List[int]) -> List[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 (((0) <= ((s)[d_1_i_])) and (((s)[d_1_i_]) <= (25))))) + Ensures(Acc(list_pred(s))) + Ensures(Acc(list_pred(Result()))) + Ensures((len(Result())) == (len(s))) + Ensures(Forall(int, lambda d_1_i_: + not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(s)))) or (((0) <= ((s)[d_1_i_])) and (((s)[d_1_i_]) <= (25))))) + Ensures(Forall(int, lambda d_1_i_: + not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(Result())))) or (((0) <= ((Result())[d_1_i_])) and (((Result())[d_1_i_]) <= (25))))) + Ensures(Forall(int, lambda d_2_i_: + not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(s)))) or (((Result())[d_2_i_]) == (rot__sym((s)[d_2_i_]))))) + r : List[int] = [] # type : List[int] + d_3_i_ = int(0) # type : int + d_3_i_ = 0 + while (d_3_i_) < (len(s)): + Invariant(Acc(list_pred(s))) + Invariant(Acc(list_pred(r))) + Invariant(((0) <= (d_3_i_)) and ((d_3_i_) <= (len(s)))) + Invariant((len(r)) == (d_3_i_)) + Invariant(Forall(int, lambda d_1_i_: + not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(s)))) or (((0) <= ((s)[d_1_i_])) and (((s)[d_1_i_]) <= (25))))) + Invariant(Forall(int, lambda d_1_i_: + not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(r)))) or (((0) <= ((r)[d_1_i_])) and (((r)[d_1_i_]) <= (25))))) + Invariant(Forall(int, lambda d_4_j_: + not (((0) <= (d_4_j_)) and ((d_4_j_) < (d_3_i_))) or (((r)[d_4_j_]) == (rot__sym((s)[d_4_j_]))))) + r = (r) + [rot__sym((s)[d_3_i_])] + d_3_i_ = (d_3_i_) + (1) + return r diff --git a/Bench/108-count_nums.py b/Bench/108-count_nums.py new file mode 100644 index 0000000..5897484 --- /dev/null +++ b/Bench/108-count_nums.py @@ -0,0 +1,48 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def count__nums(s : List[int]) -> int: + Requires(Acc(list_pred(s))) + Ensures(Acc(list_pred(s))) + Ensures(Result() == get_positive(0, len(s), s)) + cnt = int(0) # type : int + cnt = 0 + d_1_i_ = int(0) # type : int + d_1_i_ = 0 + while (d_1_i_) < (len(s)): + Invariant(Acc(list_pred(s))) + Invariant(((0) <= (d_1_i_)) and ((d_1_i_) <= (len(s)))) + Invariant(Forall(int, lambda x : + (Implies(0 <= x and x < len(s), get_positive(0, x + 1, s) == ((digits__sum(s[x]) > 0) + get_positive(0, x, s))), + [[get_positive(0, x + 1, s)]]))) + Invariant((cnt) == (get_positive(0, d_1_i_, s))) + Assert(get_positive(0, d_1_i_ + 1, s) == (digits__sum(s[d_1_i_]) > 0) + get_positive(0, d_1_i_, s)) + if (digits__sum((s)[d_1_i_])) > (0): + cnt = (cnt) + (1) + d_1_i_ = (d_1_i_) + (1) + return cnt + +@Pure +def get_positive(i : int, j : int, s : List[int]) -> int: + Requires(Acc(list_pred(s))) + Requires(0 <= i and i <= j and j <= len(s)) + if i == j: + return 0 + else: + return (digits__sum(s[j - 1]) > 0) + get_positive(i, j - 1, s) + +@Pure +def digits__sum(x : int) -> int : + if abs(x) < 10: + return x % 10 + else: + return (10 - x % 10) + digits__sum(x // 10) + +@Pure +def abs(x : int) -> int : + Ensures((Result()) >= (0)) + Ensures((Result()) == (x) or (Result()) == (0) - (x)) + if (x) >= (0): + return x + elif True: + return (0) - (x) \ No newline at end of file diff --git a/Bench/114-minSubArraySum.py b/Bench/114-minSubArraySum.py new file mode 100644 index 0000000..da3898d --- /dev/null +++ b/Bench/114-minSubArraySum.py @@ -0,0 +1,56 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +@Pure +def Sum(a : List[int], s : int, t : int) -> int : + Requires(Acc(list_pred(a))) + Requires(((0) <= (s)) and ((s) <= (t)) and ((t) <= (len(a)))) + if s == t: + return 0 + else: + return (a)[t - 1] + (Sum(a, s, t - 1)) + +def minSubArraySum(a : List[int]) -> int: + Requires(Acc(list_pred(a))) + Ensures(Acc(list_pred(a))) + Ensures(Forall(int, lambda d_1_p_: + Forall(int, lambda d_2_q_: + not ((((0) <= (d_1_p_)) and ((d_1_p_) <= (d_2_q_))) and ((d_2_q_) <= (len(a)))) or ((Sum(a, d_1_p_, d_2_q_)) >= (Result()))))) + Ensures(Exists(int, lambda d_3_k_: + Exists(int, lambda d_4_m_: + ((((0) <= (d_3_k_)) and ((d_3_k_) <= (d_4_m_))) and ((d_4_m_) <= (len(a)))) and ((Result()) == (Sum(a, d_3_k_, d_4_m_)))))) + s = int(0) # type : int + d_5_k_ = int(0) # type : int + d_6_m_ = int(0) # type : int + d_7_n_ = int(0) # type : int + d_8_c_ = int(0) # type : int + d_9_t_ = int(0) # type : int + while (d_7_n_) < (len(a)): + Invariant(Acc(list_pred(a))) + Invariant(((0) <= (d_7_n_)) and ((d_7_n_) <= (len(a)))) + Invariant(Forall(int, lambda d_1_p_: (Implies(0 <= d_1_p_ and d_1_p_ < len(a), Sum(a, 0, d_1_p_ + 1) == Sum(a, 0, d_1_p_) + a[d_1_p_])))) + Invariant(Forall(int, lambda st: + Implies(st >= 0 and st < len(a), + Forall(int, lambda d_1_p_: (Implies(st <= d_1_p_ and d_1_p_ < len(a), Sum(a, st, d_1_p_ + 1) == Sum(a, st, d_1_p_) + a[d_1_p_])))))) + Invariant(((((0) <= (d_8_c_)) and ((d_8_c_) <= (d_7_n_))) and ((d_7_n_) <= (len(a))))) + Invariant(((d_9_t_) == (Sum(a, d_8_c_, d_7_n_)))) + Invariant(((((0) <= (d_5_k_)) and ((d_5_k_) <= (d_6_m_))) and ((d_6_m_) <= (d_7_n_)))) + Invariant(((s) == (Sum(a, d_5_k_, d_6_m_)))) + Invariant(Forall(int, lambda d_10_b_: + not (((0) <= (d_10_b_)) and ((d_10_b_) <= (d_7_n_))) or ((Sum(a, d_10_b_, d_7_n_)) >= (Sum(a, d_8_c_, d_7_n_))))) + Invariant(Sum(a, d_8_c_, d_7_n_) >= Sum(a, d_5_k_, d_6_m_)) + Invariant(Forall(int, lambda d_11_p_: + Forall(int, lambda d_12_q_: + (not ((((0) <= (d_11_p_)) and ((d_11_p_) <= (d_12_q_))) and ((d_12_q_) <= (d_7_n_))) or ((Sum(a, d_11_p_, d_12_q_)) >= (Sum(a, d_5_k_, d_6_m_))), [[Sum(a, d_11_p_, d_12_q_)]])))) + Assert(Sum(a, d_8_c_, d_7_n_ + 1) == Sum(a, d_8_c_, d_7_n_) + a[d_7_n_]) + d_9_t_ = (d_9_t_) + ((a)[d_7_n_]) + d_7_n_ = (d_7_n_) + (1) + if (d_9_t_) > (0): + d_8_c_ = d_7_n_ + d_9_t_ = 0 + elif (s) > (d_9_t_): + d_5_k_ = d_8_c_ + d_6_m_ = d_7_n_ + s = d_9_t_ + return s + diff --git a/Bench/128-prod_signs.py b/Bench/128-prod_signs.py new file mode 100644 index 0000000..4bc72a3 --- /dev/null +++ b/Bench/128-prod_signs.py @@ -0,0 +1,65 @@ +from typing import cast, List, Dict, Set, Optional, Union, Tuple +from nagini_contracts.contracts import * + +@Pure +def abs(x : int) -> int : + if (x) >= (0): + return x + elif True: + return (0) - (x) + +@Pure +def sum__abs(i : int, j : int, s : List[int]) -> int : + Requires(Acc(list_pred(s))) + Requires(0 <= i and i <= j and j <= len(s)) + if i == j: + return 0 + else: + return abs(s[j - 1]) + sum__abs(i, j - 1, s) + +@Pure +def count_negatives(i : int, j : int, s : List[int]) -> int: + Requires(Acc(list_pred(s))) + Requires(0 <= i and i <= j and j <= len(s)) + if i == j: + return 0 + else: + return (1 if s[j - 1] < 0 else 0) + count_negatives(i, j - 1, s) + +def prod__signs(numbers : List[int]) -> int: + Requires(Acc(list_pred(numbers))) + Ensures(Acc(list_pred(numbers))) + Ensures((abs(Result())) == (sum__abs(0, len(numbers), numbers))) + Ensures(Implies(count_negatives(0, len(numbers), numbers) % 2 == 1, Result() <= 0)) + Ensures(Implies(count_negatives(0, len(numbers), numbers) % 2 == 0, Result() >= 0)) + s = int(0) # type : int + s = 0 + d_3_i_ = int(0) # type : int + d_3_i_ = 0 + while (d_3_i_) < (len(numbers)): + Invariant(Acc(list_pred(numbers))) + Invariant(((0) <= (d_3_i_)) and ((d_3_i_) <= (len(numbers)))) + Invariant(Forall(int, lambda x : + (Implies(0 <= x and x < len(numbers), sum__abs(0, x + 1, numbers) == sum__abs(0, x, numbers) + abs(numbers[x])), + [[sum__abs(0, x + 1, numbers)]]))) + Invariant((s) == (sum__abs(0, d_3_i_, numbers))) + Invariant(s >= 0) + Assert(sum__abs(0, d_3_i_ + 1, numbers) == sum__abs(0, d_3_i_, numbers) + abs(numbers[d_3_i_])) + s = (s) + (abs((numbers)[d_3_i_])) + d_3_i_ = (d_3_i_) + (1) + d_3_i_ = 0 + d_4_cnt_ = int(0) # type : int + d_4_cnt_ = 0 + while (d_3_i_) < (len(numbers)): + Invariant(Acc(list_pred(numbers))) + Invariant(s == sum__abs(0, len(numbers), numbers)) + Invariant(s >= 0) + Invariant(((0) <= (d_3_i_)) and ((d_3_i_) <= (len(numbers)))) + Invariant(Forall(int, lambda x : Implies(0 <= x and x < len(numbers), count_negatives(0, x + 1, numbers) == count_negatives(0, x, numbers) + (1 if numbers[x] < 0 else 0)))) + Invariant((d_4_cnt_) == (count_negatives(0, d_3_i_, numbers))) + if ((numbers)[d_3_i_]) < (0): + d_4_cnt_ = (d_4_cnt_) + (1) + d_3_i_ = (d_3_i_) + (1) + if ((d_4_cnt_ % 2)) == (1): + s = (0) - (s) + return s diff --git a/README.md b/README.md index 172ce3a..d477d20 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Current status: - [x] 9. rolling_max - [x] 10. is_palindrome - [x] 11. string_xor -- [ ] 12. longest +- [x] 12. longest - [x] 13. greatest_common_divisor - [ ] 14. all_prefixes - [ ] 15. string_sequence @@ -29,7 +29,7 @@ Current status: - [x] 23. strlen - [x] 24. largest_divisor - [ ] 25. factorize -- [ ] 26. remove_duplicates +- [x] 26. remove_duplicates - [x] 27. flip_case - [ ] 28. concatenate - [x] 29. filter_by_prefix @@ -52,7 +52,7 @@ Current status: - [x] 46. fib4 - [ ] 47. median - [x] 48. is_palindrome -- [ ] 49. modp +- [x] 49. modp - [ ] 50. encode_shift - [x] 51. remove_vowels - [x] 52. below_threshold @@ -67,16 +67,16 @@ Current status: - [ ] 61. correct_bracketing - [x] 62. derivative - [x] 63. fibfib -- [ ] 64. vowels_count +- [x] 64. vowels_count - [ ] 65. circular_shift - [x] 66. digitSum - [ ] 67. fruit_distribution - [x] 68. pluck -- [ ] 69. search +- [x] 69. search - [x] 70. strange_sort_list - [ ] 71. triangle_area - [x] 72. will_it_fly -- [ ] 73. smallest_change +- [x] 73. smallest_change - [ ] 74. total_match - [x] 75. is_multiply_prime - [ ] 76. is_simple_power @@ -92,7 +92,7 @@ Current status: - [ ] 86. anti_shuffle - [ ] 87. get_row - [x] 88. sort_array -- [ ] 89. encrypt +- [x] 89. encrypt - [ ] 90. next_smallest - [ ] 91. is_bored - [x] 92. any_int @@ -101,7 +101,7 @@ Current status: - [ ] 95. check_dict_case - [x] 96. count_up_to - [x] 97. multiply -- [ ] 98. count_upper +- [x] 98. count_upper - [ ] 99. closest_integer - [x] 100. make_a_pile - [ ] 101. words_string @@ -111,13 +111,13 @@ Current status: - [ ] 105. by_length - [x] 106. f - [ ] 107. even_odd_palindrome -- [ ] 108. count_nums +- [x] 108. count_nums - [ ] 109. move_one_ball - [x] 110. exchange - [ ] 111. histogram - [ ] 112. reverse_delete - [ ] 113. odd_count -- [ ] 114. minSubArraySum +- [x] 114. minSubArraySum - [ ] 115. max_fill - [x] 116. sort_array - [ ] 117. select_words @@ -131,7 +131,7 @@ Current status: - [ ] 125. split_words - [x] 126. is_sorted - [x] 127. intersection -- [ ] 128. prod_signs +- [x] 128. prod_signs - [ ] 129. minPath - [ ] 130. tri - [ ] 131. digits @@ -139,7 +139,7 @@ Current status: - [x] 133. sum_squares - [ ] 134. check_if_last_char_is_a_letter - [x] 135. can_arrange -- [ ] 136. largest_smallest_integers +- [x] 136. largest_smallest_integers - [ ] 137. compare_one - [x] 138. is_equal_to_sum_even - [x] 139. special_factorial diff --git a/WIP/026-remove_duplicates.py b/WIP/026-remove_duplicates.py index b10a187..5d58d87 100644 --- a/WIP/026-remove_duplicates.py +++ b/WIP/026-remove_duplicates.py @@ -9,6 +9,8 @@ def remove__duplicates(a : List[int]) -> List[int]: Ensures(Acc(list_pred(Result()))) Ensures(len(a) == len(Old(a))) Ensures(len(a) >= len(Result())) + Ensures(Forall(int, lambda d_2_i_: + Implies(((0) <= (d_2_i_)) and ((d_2_i_) < (len(a))) and (count__rec(a, a[d_2_i_], len(a)) == 1), Exists(int, lambda d_3_i_: d_3_i_ >= 0 and d_3_i_ < len(Result()) and Result()[d_3_i_] == a[d_2_i_])))) Ensures(Forall(int, lambda d_1_i_: not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(Result())))) or ((count__rec(a, (Result())[d_1_i_], len((Result())))) == (1)))) # Ensures(Forall(int, lambda d_2_i_: @@ -37,6 +39,9 @@ def remove__duplicates(a : List[int]) -> List[int]: Invariant(Forall(int, lambda d_3_i_: (Implies(d_3_i_ >= 0 and d_3_i_ < len(a), a_old[d_3_i_] == a[d_3_i_])))) Invariant(((0) <= (d_4_i_)) and ((d_4_i_) <= (len(a)))) Invariant(len(result) <= d_4_i_) + Invariant(Forall(int, lambda d_2_i_: + (Implies(((0) <= (d_2_i_)) and ((d_2_i_) < (d_4_i_)) and (count__rec(a, a[d_2_i_], len(a)) == 1), Exists(int, lambda d_3_i_: d_3_i_ >= 0 and d_3_i_ < len(result) and result[d_3_i_] == a[d_2_i_])), + [[a[d_2_i_]]]))) Invariant(Forall(int, lambda d_5_j_: (Implies(((0) <= (d_5_j_)) and ((d_5_j_) < (len(result))), (count__rec(a, (result)[d_5_j_], len(a))) == (1)), [[count__rec(a, (result)[d_5_j_], len(a))]]))) # Invariant(Forall(int, lambda d_6_j_: @@ -44,12 +49,10 @@ def remove__duplicates(a : List[int]) -> List[int]: # Invariant(Forall(int, lambda d_7_j_: # not (((0) <= (d_7_j_)) and ((d_7_j_) < (len(d_3_res_)))) or (((d_3_res_)[d_7_j_]) in (list((a)[:d_4_i_:]))))) d_8_cnt_ = int(0) # type : int - d_8_cnt_ = count_my(a, (a)[d_4_i_]) + d_8_cnt_ = count__rec(a, (a)[d_4_i_], len(a)) if (d_8_cnt_) == (1): - Assert(d_4_i_ < len(a_old)) - Assert(len(a) == len(a_old)) - Assert(count__rec(a, (a)[d_4_i_], len(a)) == 1) result = (result) + [(a)[d_4_i_]] + Assert(count__rec(a, result[len(result) - 1], len(a)) == 1) d_4_i_ = (d_4_i_) + (1) return result diff --git a/WIP/069-search.py b/WIP/069-search.py deleted file mode 100644 index 9c6823c..0000000 --- a/WIP/069-search.py +++ /dev/null @@ -1,36 +0,0 @@ -@staticmethod -def freq(s : List[int], x : int) -> int: - Requires(Acc(list_pred(s))) - Ensures(Acc(list_pred(s))) - Ensures(def iife0_(): - coll0_ = set() # type : Set[int] - compr_0_ = int(0) # type : int - for compr_0_ in range(0, len(s)): - d_0_i_: int = compr_0_ - if (((0) <= (d_0_i_)) and ((d_0_i_) < (len(s)))) and (((s)[d_0_i_]) == (x)): - coll0_ = coll0_.union(set([d_0_i_])) - return set(coll0_) - (Result()) == (len(iife0_() - ))) - count = int(0) # type : int - count = 0 - d_1_i_ = int(0) # type : int - d_1_i_ = 0 - def iife1_(): - coll1_ = set() # type : Set[int] - compr_1_ = int(0) # type : int - for compr_1_ in range(0, d_1_i_): - d_3_j_: int = compr_1_ - if (((0) <= (d_3_j_)) and ((d_3_j_) < (d_1_i_))) and (((s)[d_3_j_]) == (x)): - coll1_ = coll1_.union(set([d_3_j_])) - return set(coll1_) - while (d_1_i_) < (len(s)): - Invariant(Acc(list_pred(s))) - Invariant(((0) <= (d_1_i_)) and ((d_1_i_) <= (len(s)))) - Invariant((count) == (len(d_2_good_))) - Invariant((d_2_good_) == (iife1_() - )) - if ((s)[d_1_i_]) == (x): - count = (count) + (1) - d_1_i_ = (d_1_i_) + (1) - return count diff --git a/WIP/074-total_match.py b/WIP/074-total_match.py index d784998..cadfd41 100644 --- a/WIP/074-total_match.py +++ b/WIP/074-total_match.py @@ -7,18 +7,14 @@ # Requires(Acc(list_pred(list1))) # Requires(Forall(list1, lambda x: Acc(list_pred(x)))) # Requires(Forall(list2, lambda x: Acc(list_pred(x)))) -# # Requires(Forall(int, lambda x: Implies(x >= 0 and x < len(list1), Acc(list_pred(list1[x]))))) -# # Requires(Forall(int, lambda x: Implies(x >= 0 and x < len(list2), Acc(list_pred(list2[x]))))) # Ensures(Acc(list_pred(list2))) # Ensures(Acc(list_pred(list1))) # Ensures(Forall(list1, lambda x: Acc(list_pred(x)))) # Ensures(Forall(list2, lambda x: Acc(list_pred(x)))) -# # Ensures(Forall(int, lambda x: Implies(x >= 0 and x < len(list1), Acc(list_pred(list1[x]))))) -# # Ensures(Forall(int, lambda x: Implies(x >= 0 and x < len(list2), Acc(list_pred(list2[x]))))) # Ensures(Acc(list_pred(Result()))) # Ensures(Forall(int, lambda x: Implies(x >= 0 and x < len(Result()), Acc(list_pred(Result()[x]))))) # Ensures(((len(Result())) == (len(list1))) or ((len(Result())) == (len(list2)))) -# Ensures(((Result()) == (list1)) or ((Result()) == (list2))) +# # Ensures(((Result()) == (list1)) or ((Result()) == (list2))) # Ensures((sum__chars__rec(0, len(Result()), Result())) <= (sum__chars__rec(0, len(list1), list1))) # Ensures((sum__chars__rec(0, len(Result()), Result())) <= (sum__chars__rec(0, len(list2), list2))) # Ensures(not ((sum__chars__rec(0, len(list1), list1)) == (sum__chars__rec(0, len(list2), list2))) or ((Result()) == (list1))) @@ -51,15 +47,17 @@ def SumChars(lst : List[List[int]]) -> int: sum = 0 d_3_i_ = int(0) # type : int d_3_i_ = 0 + Assert(Forall(int, lambda d_0_i_: (Implies(((0) <= (d_0_i_)) and ((d_0_i_) < (len(lst))), sum__chars__rec(0, d_0_i_ + 1, lst) == (sum__chars__rec(0, d_0_i_, lst) + len(lst[d_0_i_]))), + [[sum__chars__rec(0, d_0_i_ + 1, lst)]]))) while (d_3_i_) < (len(lst)): Invariant(Acc(list_pred(lst), 1/2)) Invariant(Forall(lst, lambda x: Acc(list_pred(x), 1/2))) Invariant(((0) <= (d_3_i_)) and ((d_3_i_) <= (len(lst)))) - Invariant(Forall(int, lambda d_0_i_: (Implies(((0) <= (d_0_i_)) and ((d_0_i_) < (len(lst))), sum__chars__rec(0, d_0_i_ + 1, lst) == (sum__chars__rec(0, d_0_i_, lst) + len(lst[d_0_i_]))), - [[sum__chars__rec(0, d_0_i_ + 1, lst) == (sum__chars__rec(0, d_0_i_, lst) + len(lst[d_0_i_]))]]))) + Invariant(lst == Old(lst)) # Invariant((sum) == (sum__chars__rec(0, d_3_i_, lst))) - # Assert(sum__chars__rec(0, d_3_i_ + 1, lst) == sum__chars__rec(0, d_3_i_, lst) + len(lst[d_3_i_])) + Assert(Forall(int, lambda d_0_i_: (Implies(((0) <= (d_0_i_)) and ((d_0_i_) < (len(lst))), sum__chars__rec(0, d_0_i_ + 1, lst) == (sum__chars__rec(0, d_0_i_, lst) + len(lst[d_0_i_]))), + [[sum__chars__rec(0, d_0_i_ + 1, lst)]]))) + Assert(sum__chars__rec(0, d_3_i_ + 1, lst) == sum__chars__rec(0, d_3_i_, lst) + len(lst[d_3_i_])) sum = (sum) + (len((lst)[d_3_i_])) d_3_i_ = (d_3_i_) + (1) - # Assert((list) == (list((list)[:leSequencen(list):]))) return sum diff --git a/WIP/115-max_fill.py b/WIP/115-max_fill.py index 163ecf6..1459524 100644 --- a/WIP/115-max_fill.py +++ b/WIP/115-max_fill.py @@ -15,19 +15,17 @@ def max__fill(grid : List[List[int]], capacity : int) -> int: d_2_i_ = int(0) # type : int d_2_i_ = 0 cnt = 0 + lst_h : List[int] = [] while (d_2_i_) < (len(grid)): Invariant(capacity > 0) Invariant(Acc(list_pred(grid), 1/2)) + Invariant(Acc(list_pred(lst_h))) Invariant(Forall(grid, lambda grid0: Acc(list_pred(grid0), 1/2))) Invariant(((0) <= (d_2_i_)) and ((d_2_i_) <= (len(grid)))) - # Invariant(Forall(int, lambda d_0_i_: - # (Implies(d_0_i_ >= 0 and d_0_i_ < len(grid), - # psum2(0, d_0_i_ + 1, grid, capacity) == (psum2(0, d_0_i_, grid, capacity) + (psum(0, len(grid[d_0_i_]), grid[d_0_i_]) + capacity - 1) // capacity)), [[]]))) - # Invariant(cnt == (psum2(0, d_2_i_, grid, capacity))) - Assume(Forall(int, lambda d_0_i_: - (Implies(d_0_i_ >= 0 and d_0_i_ < len(grid), - psum2(0, d_0_i_ + 1, grid, capacity) == (psum2(0, d_0_i_, grid, capacity) + (psum(0, len(grid[d_0_i_]), grid[d_0_i_]) + capacity - 1) // capacity)), - [[grid[d_0_i_]]]))) + Invariant(len(lst_h) == d_2_i_) + Invariant(Forall(int, lambda d_0_i_: + (Implies(d_0_i_ >= 0 and d_0_i_ < len(lst_h), lst_h[d_0_i_] == psum(0, len(grid[d_0_i_]), grid[d_0_i_])) + , [[grid[d_0_i_]]]))) d_4_j_ = int(0) # type : int d_4_j_ = 0 d_5_sum__j_ = int(0) # type : int @@ -36,29 +34,56 @@ def max__fill(grid : List[List[int]], capacity : int) -> int: Invariant(capacity > 0) Invariant(Acc(list_pred(grid), 1/2)) Invariant(Forall(grid, lambda grid0: Acc(list_pred(grid0), 1/2))) + Invariant(d_2_i_ == Old(d_2_i_)) + Invariant(capacity == Old(capacity)) + Invariant(grid == Old(grid)) Invariant(((0) <= (d_2_i_)) and ((d_2_i_) < (len(grid)))) Invariant(((0) <= (d_4_j_)) and ((d_4_j_) <= (len((grid)[d_2_i_])))) - Invariant(Forall(int, lambda d_0_i_: - (Implies(d_0_i_ >= 0 and d_0_i_ < len(grid), - psum2(0, d_0_i_ + 1, grid, capacity) == (psum2(0, d_0_i_, grid, capacity) + (psum(0, len(grid[d_0_i_]), grid[d_0_i_]) + capacity - 1) // capacity)), - [[grid[d_0_i_]]]))) - Invariant((psum2(0, d_2_i_ + 1, grid, capacity)) == (psum2(0, d_2_i_, grid, capacity) + (psum(0, len(grid[d_2_i_]), grid[d_2_i_]) + capacity - 1) // capacity)) - # Invariant((psum(0, d_4_j_ + 1, grid[d_2_i_])) == ((psum(0, d_4_j_, grid[d_2_i_]) + (grid[d_2_i_])[d_4_j_]))) - # Invariant(Forall(int, lambda x: - # (not (((0) <= (x)) and ((x) < (len(grid[d_2_i_])))) or - # ((psum(0, x + 1, grid[d_2_i_])) == (psum(0, x, grid[d_2_i_]) + grid[d_2_i_][x])), [[psum(0, x + 1, grid[d_2_i_])]]))) - # Invariant(cnt == (psum2(0, d_2_i_, grid, capacity))) - # Invariant((d_5_sum__j_) == (psum(0, d_4_j_, grid[d_2_i_]))) - # Assert((psum(0, d_4_j_ + 1, grid[d_2_i_])) == ((psum(0, d_4_j_, grid[d_2_i_]) + (grid[d_2_i_])[d_4_j_]))) + Invariant(Acc(list_pred(lst_h))) + Invariant(len(lst_h) == d_2_i_) + Invariant(Forall(int, lambda d_0_i_: + (Implies(d_0_i_ >= 0 and d_0_i_ < len(lst_h), lst_h[d_0_i_] == psum(0, len(grid[d_0_i_]), grid[d_0_i_])) + , [[grid[d_0_i_]]]))) + Invariant(Forall(int, lambda x: + (not (((0) <= (x)) and ((x) < (len(grid[d_2_i_])))) or + ((psum(0, x + 1, grid[d_2_i_])) == (psum(0, x, grid[d_2_i_]) + grid[d_2_i_][x])), [[psum(0, x + 1, grid[d_2_i_])]]))) + Invariant((d_5_sum__j_) == (psum(0, d_4_j_, grid[d_2_i_]))) + Assert((psum(0, d_4_j_ + 1, grid[d_2_i_])) == ((psum(0, d_4_j_, grid[d_2_i_]) + (grid[d_2_i_])[d_4_j_]))) d_5_sum__j_ = (d_5_sum__j_) + (((grid)[d_2_i_])[d_4_j_]) d_4_j_ = (d_4_j_) + (1) - d_6_current__el_ = int(0) # type : int - d_6_current__el_ = ((((d_5_sum__j_) + (capacity)) - (1)) // capacity) - Assert((psum2(0, d_2_i_ + 1, grid, capacity)) == (psum2(0, d_2_i_, grid, capacity) + (psum(0, len(grid[d_2_i_]), grid[d_2_i_]) + capacity - 1) // capacity)) - cnt = (cnt) + (d_6_current__el_) + Assert(d_5_sum__j_ == psum(0, len(grid[d_2_i_]), grid[d_2_i_])) + lst_h = lst_h + [d_5_sum__j_] d_2_i_ = (d_2_i_) + (1) + + d_2_i_ = 0 + while d_2_i_ < len(grid): + Invariant(capacity > 0) + Invariant(Acc(list_pred(lst_h), 1/2)) + Invariant(Acc(list_pred(grid), 1/2)) + Invariant(len(lst_h) == len(grid)) + Invariant(Forall(grid, lambda grid0: Acc(list_pred(grid0), 1/2))) + Invariant(0 <= d_2_i_ and d_2_i_ <= len(grid)) + Invariant(Forall(int, lambda d_0_i_: + (Implies(d_0_i_ >= 0 and d_0_i_ < len(lst_h), lst_h[d_0_i_] == psum(0, len(grid[d_0_i_]), grid[d_0_i_])) + , [[grid[d_0_i_]]]))) + Invariant(Forall(int, lambda d_0_i_: + (Implies(d_0_i_ >= 0 and d_0_i_ < len(grid), + psum2(0, d_0_i_ + 1, grid, capacity) == (((psum(0, len(grid[d_0_i_]), grid[d_0_i_]) + capacity - 1) // capacity) + psum2(0, d_0_i_, grid, capacity))), + [[psum2(0, d_0_i_ + 1, grid, capacity)]]))) + Assert(lst_h[d_2_i_] == psum(0, len(grid[d_2_i_]), grid[d_2_i_])) + # Assert(psum2(0, d_2_i_ + 1, grid, capacity) == psum2(0, d_2_i_, grid, capacity) + (psum(0, len(grid[d_2_i_]), grid[d_2_i_]) + capacity - 1) // capacity) + cnt = cnt + ((lst_h[d_2_i_] + capacity - 1) // capacity) + d_2_i_= d_2_i_ + 1 return cnt +# Invariant(Forall(int, lambda d_0_i_: +# (Implies(d_0_i_ >= 0 and d_0_i_ < len(grid), +# psum2(0, d_0_i_ + 1, grid, capacity) == (psum2(0, d_0_i_, grid, capacity) + (psum(0, len(grid[d_0_i_]), grid[d_0_i_]) + capacity - 1) // capacity)), +# [[grid[d_0_i_]]]))) +# Invariant((psum2(0, d_2_i_ + 1, grid, capacity)) == (psum2(0, d_2_i_, grid, capacity) + (psum(0, len(grid[d_2_i_]), grid[d_2_i_]) + capacity - 1) // capacity)) +# Invariant((psum(0, d_4_j_ + 1, grid[d_2_i_])) == ((psum(0, d_4_j_, grid[d_2_i_]) + (grid[d_2_i_])[d_4_j_]))) +# Invariant(cnt == (psum2(0, d_2_i_, grid, capacity))) + @Pure def psum2(i : int, j : int, s : List[List[int]], capacity : int) -> int : Requires(Acc(list_pred(s), 1/2))