Skip to content

Commit

Permalink
50, 93, 121
Browse files Browse the repository at this point in the history
  • Loading branch information
alex28sh committed Aug 28, 2024
1 parent 2c9f3ce commit 649cc16
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
56 changes: 56 additions & 0 deletions Bench/050-encode_shift.py
Original file line number Diff line number Diff line change
@@ -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
47 changes: 47 additions & 0 deletions Bench/093-encode.py
Original file line number Diff line number Diff line change
@@ -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)))
27 changes: 27 additions & 0 deletions Bench/121-solution.py
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 *

@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

0 comments on commit 649cc16

Please sign in to comment.