Skip to content

Commit

Permalink
16, 36, 161
Browse files Browse the repository at this point in the history
  • Loading branch information
alex28sh committed Aug 30, 2024
1 parent 5b3553e commit 68b4c8e
Show file tree
Hide file tree
Showing 8 changed files with 370 additions and 104 deletions.
46 changes: 46 additions & 0 deletions Bench/016-count_distinct_characters.py
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
57 changes: 57 additions & 0 deletions Bench/036-fizz_buzz.py
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)
76 changes: 76 additions & 0 deletions Bench/161-solve.py
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)
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Current status:
- [x] 13. greatest_common_divisor
- [ ] 14. all_prefixes
- [ ] 15. string_sequence
- [ ] 16. count_distinct_characters
- [x] 16. count_distinct_characters
- [ ] 17. parse_music
- [ ] 18. how_many_times
- [ ] 19. sort_numbers
Expand All @@ -39,7 +39,7 @@ Current status:
- [x] 33. sort_third
- [x] 34. unique
- [x] 35. max_element
- [ ] 36. fizz_buzz
- [x] 36. fizz_buzz
- [x] 37. sort_even
- [ ] 38. encode_cyclic
- [ ] 39. prime_fib
Expand Down Expand Up @@ -78,7 +78,7 @@ Current status:
- [x] 72. will_it_fly
- [x] 73. smallest_change
- [ ] 74. total_match
- [x] 75. is_multiply_prime
- [ ] 75. is_multiply_prime
- [ ] 76. is_simple_power
- [ ] 77. iscube
- [x] 78. hex_key
Expand Down Expand Up @@ -164,6 +164,6 @@ Current status:
- [ ] 158. find_max
- [x] 159. eat
- [ ] 160. do_algebra
- [ ] 161. solve
- [x] 161. solve
- [ ] 162. string_to_md5
- [x] 163. generate_integers
96 changes: 0 additions & 96 deletions WIP/029-filter_by_prefix.py

This file was deleted.

15 changes: 11 additions & 4 deletions WIP/077-is_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,28 @@ def cube__root(N : int) -> int:
# Ensures(Forall(int, lambda d_0_r_: Implies(0 <= d_0_r_ and d_0_r_ < r, cube(d_0_r_) < N)))
r = int(0) # type : int
r = 0
Assert(Forall(int, lambda x: x < (x + 1)))
# Assert(Forall(int, lambda x: x < (x + 1)))
# Assert(Forall(int, lambda x: Implies(0 <= x and x < N, x * x < (x + 1) * (x + 1))))
Assert(Forall(int, lambda x: x * x * x < (x + 1) * (x + 1) * (x + 1)))
# Assert(Forall(int, lambda x: x * x * x < (x + 1) * (x + 1) * (x + 1)))
# Assert(Forall(int, lambda x: (Implies(0 <= x and x < N, cube(x) < cube(x + 1)), [[cube(x)]])))
while (cube((r) + (1))) <= (N):
Invariant(N >= 0)
Invariant(r >= 0 and r <= N)
Invariant((cube(r)) <= (N))
# Invariant(Forall(int, lambda x: (Implies(0 <= x, cube(x) < cube(x + 1)), [[cube(x)]])))
# Invariant(Forall(int, lambda x: (Implies(0 <= x and x < r, cube(x) < cube(r)), [[cube(x) < cube(r)]])))
Invariant(Forall(int, lambda x: (Implies(0 <= x and x <= N, cube_less(x, x + 1)), [[cube_less(x, x + 1)]])))
Invariant(Forall(int, lambda x: (Implies(0 <= x and x < r, cube_less(x, r)), [[cube_less(x, r)]])))
Invariant(cube_less(r, r + 1))
# Invariant(Forall(int, lambda x: (Implies(r < x and x <= N, cube_less(r, x)), [[]])))
# Invariant(Forall(int, lambda x: Forall(int, lambda y : (Implies(0 <= x and x < y, cube(x) < cube(y)), [[cube(x) < cube(y)]]))))
# Invariant(Forall(int, lambda d_0_r_: (Implies(0 <= d_0_r_ and d_0_r_ < r, cube(d_0_r_) < N), [[cube(d_0_r_)]])))
r = (r) + (1)
return r

@Pure
def cube_less(a : int, b : int) -> bool:
Requires(a >= 0)
Requires(b >= 0)
return cube(a) < cube(b)

def is__cube(n : int) -> bool:
Requires(n >= (0))
Expand Down
Loading

0 comments on commit 68b4c8e

Please sign in to comment.