From 649cc160e9cb63ec15b970a6b7a9393326773db5 Mon Sep 17 00:00:00 2001 From: alex28sh Date: Wed, 28 Aug 2024 16:56:31 +0200 Subject: [PATCH] 50, 93, 121 --- Bench/050-encode_shift.py | 56 +++++++++++++++++++++++++++++++++++++++ Bench/093-encode.py | 47 ++++++++++++++++++++++++++++++++ Bench/121-solution.py | 27 +++++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 Bench/050-encode_shift.py create mode 100644 Bench/093-encode.py create mode 100644 Bench/121-solution.py diff --git a/Bench/050-encode_shift.py b/Bench/050-encode_shift.py new file mode 100644 index 0000000..72249c0 --- /dev/null +++ b/Bench/050-encode_shift.py @@ -0,0 +1,56 @@ +from typing import cast, List, Dict, Set, Optional, Union, Tuple +from nagini_contracts.contracts import * + +@Pure +def encode__char(c : int) -> int : + return (c - 97 + 5) % 26 + 97 + +@Pure +def decode__char(c : int) -> int : + return ((c) - (97) - (5)) % 26 + 97 + +def encode__shift(s : List[int]) -> List[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))))) + Ensures(Acc(list_pred(Result()))) + Ensures(Acc(list_pred(s))) + Ensures((len(s)) == (len(Result()))) + Ensures(Forall(int, lambda d_1_i_: + not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(s)))) or (((Result())[d_1_i_]) == (encode__char((s)[d_1_i_]))))) + t : List[int] = [] # type : List[int] + d_2_i_ = int(0) # type : int + d_2_i_ = 0 + while (d_2_i_) < (len(s)): + Invariant(Acc(list_pred(t))) + Invariant(Acc(list_pred(s))) + Invariant(((0) <= (d_2_i_)) and ((d_2_i_) <= (len(s)))) + Invariant((len(t)) == (d_2_i_)) + Invariant(Forall(int, lambda d_3_j_: + not (((0) <= (d_3_j_)) and ((d_3_j_) < (d_2_i_))) or (((t)[d_3_j_]) == (encode__char((s)[d_3_j_]))))) + t = (t) + [encode__char((s)[d_2_i_])] + d_2_i_ = (d_2_i_) + (1) + return t + +def decode__shift(s : List[int]) -> List[int]: + Requires(Acc(list_pred(s))) + Requires(Forall(int, lambda d_4_i_: + not (((0) <= (d_4_i_)) and ((d_4_i_) < (len(s)))) or (((97) <= ((s)[d_4_i_])) and (((s)[d_4_i_]) <= (122))))) + Ensures(Acc(list_pred(Result()))) + Ensures(Acc(list_pred(s))) + Ensures((len(s)) == (len(Result()))) + Ensures(Forall(int, lambda d_5_i_: + not (((0) <= (d_5_i_)) and ((d_5_i_) < (len(s)))) or (((Result())[d_5_i_]) == (decode__char((s)[d_5_i_]))))) + t : List[int] = [] + d_6_i_ = int(0) # type : int + d_6_i_ = 0 + while (d_6_i_) < (len(s)): + Invariant(Acc(list_pred(t))) + Invariant(Acc(list_pred(s))) + Invariant(((0) <= (d_6_i_)) and ((d_6_i_) <= (len(s)))) + Invariant((len(t)) == (d_6_i_)) + Invariant(Forall(int, lambda d_7_j_: + not (((0) <= (d_7_j_)) and ((d_7_j_) < (d_6_i_))) or (((t)[d_7_j_]) == (decode__char((s)[d_7_j_]))))) + t = (t) + [decode__char((s)[d_6_i_])] + d_6_i_ = (d_6_i_) + (1) + return t diff --git a/Bench/093-encode.py b/Bench/093-encode.py new file mode 100644 index 0000000..c4ba40f --- /dev/null +++ b/Bench/093-encode.py @@ -0,0 +1,47 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def encode(s : List[int]) -> List[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))) or (((65) <= ((s)[d_0_i_])) and (((s)[d_0_i_]) <= (90)))))) + Ensures(Acc(list_pred(Result()))) + Ensures(Acc(list_pred(s))) + Ensures((len(s)) == (len(Result()))) + Ensures(Forall(int, lambda d_1_i_: + not ((((0) <= (d_1_i_)) and ((d_1_i_) < (len(s)))) and (is__vowel((s)[d_1_i_]))) or (((Result())[d_1_i_]) == (rot2(swap__case((s)[d_1_i_])))))) + Ensures(Forall(int, lambda d_2_i_: + not ((((0) <= (d_2_i_)) and ((d_2_i_) < (len(s)))) and (not(is__vowel((s)[d_2_i_])))) or (((Result())[d_2_i_]) == (swap__case((s)[d_2_i_]))))) + t : List[int] = [] + d_3_i_ = int(0) # type : int + d_3_i_ = 0 + while (d_3_i_) < (len(s)): + Invariant(Acc(list_pred(t))) + Invariant(Acc(list_pred(s))) + Invariant(((0) <= (d_3_i_)) and ((d_3_i_) <= (len(s)))) + Invariant((len(t)) == (d_3_i_)) + Invariant(Forall(int, lambda d_4_j_: + not ((((0) <= (d_4_j_)) and ((d_4_j_) < (d_3_i_))) and (is__vowel((s)[d_4_j_]))) or (((t)[d_4_j_]) == (rot2(swap__case((s)[d_4_j_])))))) + Invariant(Forall(int, lambda d_5_j_: + not ((((0) <= (d_5_j_)) and ((d_5_j_) < (d_3_i_))) and (not(is__vowel((s)[d_5_j_])))) or (((t)[d_5_j_]) == (swap__case((s)[d_5_j_]))))) + if is__vowel((s)[d_3_i_]): + t = (t) + [rot2(swap__case((s)[d_3_i_]))] + else: + t = (t) + [swap__case((s)[d_3_i_])] + d_3_i_ = (d_3_i_) + (1) + return t + +@Pure +def swap__case(c : int) -> int : + if ((97) <= (c)) and ((c) <= (122)): + return (65) + ((c) - (97)) + elif True: + return (97) + ((c) - (65)) + +@Pure +def rot2(c : int) -> int : + return (c + 2) + +@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/121-solution.py b/Bench/121-solution.py new file mode 100644 index 0000000..e80cc8d --- /dev/null +++ b/Bench/121-solution.py @@ -0,0 +1,27 @@ +from typing import cast, List, Dict, Set, Optional, Union, Tuple +from nagini_contracts.contracts import * + +@Pure +def psum(i : int, j : int, s : List[int]) -> int : + Requires(Acc(list_pred(s))) + Requires(0 <= i and i <= j + 1 and j < len(s)) + if i > j: + return 0 + else: + return ((s)[j] if ((((j) % 2) == 0) and ((s)[j] % 2 == 1)) else 0) + (psum(i, j - 1, s)) + +def add(v : List[int]) -> int: + Requires(Acc(list_pred(v))) + Ensures(Acc(list_pred(v))) + Ensures((Result()) == (psum(0, len(v) - 1, v))) + r = int(0) # type : int + r = 0 + d_2_k_ = int(0) # type : int + d_2_k_ = 0 + while (d_2_k_) < (len(v)): + Invariant(Acc(list_pred(v))) + Invariant(((0) <= (d_2_k_)) and ((d_2_k_) <= (len(v)))) + Invariant((r) == (psum(0, d_2_k_ - 1, v))) + r = (r) + (((v)[d_2_k_] if ((((d_2_k_) % 2) == 0) and ((v)[d_2_k_] % 2 == 1)) else 0)) + d_2_k_ = (d_2_k_) + (1) + return r