function_signature
stringlengths
15
86
docstring
stringlengths
23
1.07k
test_cases
stringlengths
36
501
problem_spec_nl
stringlengths
59
1.14k
problem_spec_formal_ground_truth
stringlengths
229
1.39k
problem_spec_formal_generated
stringlengths
87
156
isomorphism_theorem
stringlengths
94
188
isomorphism_proof
stringclasses
1 value
implementation_signature
stringlengths
36
77
implementation
stringlengths
5
942
test_cases_lean
stringlengths
27
566
correctness_theorem
stringlengths
63
121
correctness_proof
stringlengths
5
5.72k
helper_definitions
stringclasses
11 values
isomorphism_helper_lemmas
float64
correctness_helper_lemmas
float64
def words_string(s: string) -> List[string]
You will be given a string of words separated by commas or spaces. Your task is to split the string into words and return an array of the words.
[{"input": "Hi, my name is John", "expected_output": ["Hi", "my", "name", "is", "John"]}, {"input": "One, two, three, four, five, six", "expected_output": ["One", "two", "three", "four", "five", "six"]}]
def words_string(s: string) -> List[string] """You will be given a string of words separated by commas or spaces. Your task is to split the string into words and return an array of the words. """
def problem_spec -- function signature (implementation: String β†’ List String) -- inputs (s: String) := -- spec let spec (result: List String) := let chars := s.toList; let first := s.takeWhile (fun c => c β‰  ',' ∧ c β‰  ' '); (result = [] ↔ (βˆ€ x ∈ chars, x = ' ' ∨ x = ',') ∨ s = "") ∧ (result β‰  [] ↔ result = [first] ++ (implementation (s.drop (first.length + 1)))) -- program termination βˆƒ result, implementation s = result ∧ spec result
def generated_spec -- function signature (impl: String β†’ List String) -- inputs (s: String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ s, problem_spec impl s) ↔ (βˆ€ s, generated_spec impl s) :=
sorry
def implementation (s: String) : List String :=
List.map (fun s => (s.toList.filter (fun c => c != ',')).asString) ((s.splitOn).filter (fun s => s != "" ∧ s != ","))
-- #test implementation "Hi, my name is John" = ["Hi", "my", "name", "is", "John"] -- #test implementation "One, two, three, four, five, six" = ["One", "two", "three", "four", "five", "six"] -- #test implementation "Hi, my name" = ["Hi", "my", "name"] -- #test implementation "One,, two, three, four, five, six," = ["One", "two", "three", "four", "five", "six"] -- #test implementation "" = [] -- #test implementation "ahmed , gamal" = ["ahmed", "gamal"]
theorem correctness (s: String) : problem_spec implementation s :=
by sorry
null
null
null
def choose_num(x: int, y: int) -> int
This function takes two positive numbers x and y and returns the biggest even integer number that is in the range [x, y] inclusive. If there's no such number, then the function should return -1.
[{"input": "(12, 15)", "expected_output": 14}, {"input": "(13, 12)", "expected_output": -1}]
def choose_num(x: int, y: int) -> int """This function takes two positive numbers x and y and returns the biggest even integer number that is in the range [x, y] inclusive. If there's no such number, then the function should return -1. """
def problem_spec -- function signature (implementation: Int β†’ Int β†’ Int) -- inputs (x: Int) (y: Int) := -- spec let spec (result: Int) := (result = -1 ∨ (x ≀ result ∧ result ≀ y ∧ Even result)) ∧ (result = -1 ∨ (forall i: Int, (x ≀ i ∧ i ≀ y ∧ Even i) β†’ result β‰₯ i)) ∧ (result = -1 ↔ (x > y ∨ (x == y ∧ Odd x ∧ Odd y))) -- program termination βˆƒ result, implementation x y = result ∧ spec result
def generated_spec -- function signature (impl: Int β†’ Int β†’ Int) -- inputs (x: Int) (y: Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ x y, problem_spec impl x y) ↔ (βˆ€ x y, generated_spec impl x y) :=
sorry
def implementation (x: Int) (y: Int) : Int :=
sorry
-- #test implementation 12 15 = 14 -- #test implementation 13 12 = -1 -- #test implementation 33 12354 = 12354 -- #test implementation 5234 5233 = -1 -- #test implementation 6 29 = 28 -- #test implementation 27 10 = (-1) -- #test implementation 7 7 = -1 -- #test implementation 546 546 = 546
theorem correctness (x: Int) (y: Int) : problem_spec implementation x y :=
by sorry
null
null
null
def rounded_avg(n: nat, m: nat) -> Option[string]
You are given two positive integers n and m, and your task is to compute the average of the integers from n through m (including n and m). Round the answer to the nearest integer and convert that to binary. If n is greater than m, return none.
[{"input": "(1, 5)", "expected_output": "0b11"}, {"input": "(7, 5)", "expected_output": "None"}, {"input": "(10, 20)", "expected_output": "0b1111"}, {"input": "(20, 33)", "expected_output": "0b11010"}]
def rounded_avg(n: nat, m: nat) -> Option[string] """You are given two positive integers n and m, and your task is to compute the average of the integers from n through m (including n and m). Round the answer to the nearest integer and convert that to binary. If n is greater than m, return none. """
def problem_spec -- function signature (implementation: Nat β†’ Nat β†’ Option String) -- inputs (n: Nat) (m: Nat) := -- spec let spec (result: Option String) := (n > m ↔ result.isNone) ∧ (n ≀ m ↔ result.isSome) ∧ (n ≀ m β†’ (result.isSome ∧ let val := Option.getD result ""; let xs := List.Ico n (m+1); let avg := xs.sum / xs.length; (val.take 2 = "0b") ∧ (Nat.ofDigits 2 ((val.drop 2).toList.map (fun c => c.toNat - '0'.toNat)).reverse = avg))) -- program termination βˆƒ result, implementation n m = result ∧ spec result
def generated_spec -- function signature (impl: Nat β†’ Nat β†’ Option String) -- inputs (n: Nat) (m: Nat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ n m, problem_spec impl n m) ↔ (βˆ€ n m, generated_spec impl n m) :=
sorry
def implementation (n: Nat) (m: Nat) : Option String :=
if n > m then none else let xs := List.Ico n (m+1); let avg := xs.sum / xs.length; some ("0b" ++ (Nat.toDigits 2 avg).asString)
-- #test implementation 1 5 = some "0b11" -- #test implementation 7 13 = some "0b1010" -- #test implementation 964 977 = some "0b1111001010" -- #test implementation 996 997 = some "0b1111100100" -- #test implementation 185 546 = some "0b101101110" -- #test implementation 362 496 = some "0b110101101" -- #test implementation 350 902 = some "0b1001110010" -- #test implementation 197 233 = some "0b11010111" -- #test implementation 7 5 = none -- #test implementation 5 1 = none -- #test implementation 5 5 = some "0b101"
theorem correctness (n: Nat) (m: Nat) : problem_spec implementation n m :=
by sorry
null
null
null
def unique_digits(x: List[nat]) -> List[nat]
Given a list of positive integers x. return a sorted list of all elements that hasn't any even digit. Note: Returned list should be sorted in increasing order.
[{"input": [15, 33, 1422, 1], "expected_output": [1, 15, 33]}, {"input": [152, 323, 1422, 10], "expected_output": []}]
def unique_digits(x: List[nat]) -> List[nat] """Given a list of positive integers x. return a sorted list of all elements that hasn't any even digit. Note: Returned list should be sorted in increasing order. """
def problem_spec -- function signature (implementation: List Nat β†’ List Nat) -- inputs (x: List Nat) := -- spec let spec (result: List Nat) := let has_even_digits(i: Nat): Bool := (List.filter (fun d => Even d) (Nat.digits 10 i)).length > 0; (List.Sorted Nat.le result) ∧ (forall i, i ∈ result ↔ (i ∈ x ∧ !(has_even_digits i))) -- program termination βˆƒ result, implementation x = result ∧ spec result
def generated_spec -- function signature (impl: List Nat β†’ List Nat) -- inputs (x: List Nat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ x, problem_spec impl x) ↔ (βˆ€ x, generated_spec impl x) :=
sorry
def implementation (x: List Nat) : List Nat :=
sorry
-- #test implementation [15, 33, 1422, 1] = [1, 15, 33] -- #test implementation [152, 323, 1422, 10] = [] -- #test implementation [12345, 2033, 111, 151] = [111, 151] -- #test implementation [135, 103, 31] = [31, 135]
theorem correctness (x: List Nat) : problem_spec implementation x :=
by sorry
null
null
null
def by_length(arr: List[int]) -> List[string]
Given an array of integers, sort the integers that are between 1 and 9 inclusive, reverse the resulting array, and then replace each digit by its corresponding name from "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine".
[{"input": [2, 1, 1, 4, 5, 8, 2, 3], "expected_output": ["Eight", "Five", "Four", "Three", "Two", "Two", "One", "One"]}, {"input": [], "expected_output": []}, {"input": [1, -1, 55], "expected_output": ["One"]}]
def by_length(arr: List[int]) -> List[string] """Given an array of integers, sort the integers that are between 1 and 9 inclusive, reverse the resulting array, and then replace each digit by its corresponding name from "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine". """
def problem_spec -- function signature (implementation: List Int β†’ List String) -- inputs (arr: List Int) := -- spec let spec (result: List String) := let digits: List String := ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"]; (forall s: String, (s ∈ result β†’ s ∈ digits)) ∧ (arr.length β‰₯ result.length) ∧ (forall x: Nat, ((x: Int) ∈ arr ∧ 1 ≀ x ∧ x ≀ 9) β†’ (digits[x-1]! ∈ result)) ∧ (List.Sorted Int.le (List.map (fun (s: String) => (List.indexOf s digits) + 1) result).reverse) -- program termination βˆƒ result, implementation arr = result ∧ spec result
def generated_spec -- function signature (impl: List Int β†’ List String) -- inputs (arr: List Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ arr, problem_spec impl arr) ↔ (βˆ€ arr, generated_spec impl arr) :=
sorry
def implementation (arr: List Int) : List String :=
sorry
-- #test implementation [2, 1, 1, 4, 5, 8, 2, 3] = ["Eight", "Five", "Four", "Three", "Two", "Two", "One", "One"] -- #test implementation [] = [] -- #test implementation [1, -1 , 55] = ["One"] -- #test implementation [1, -1, 3, 2] = ["Three", "Two", "One"] -- #test implementation [9, 4, 8] = ["Nine", "Eight", "Four"]
theorem correctness (arr: List Int) : problem_spec implementation arr :=
by sorry
null
null
null
def f(n: int) -> List[int]
Implement the function f that takes n as a parameter, and returns a list of size n, such that the value of the element at index i is the factorial of i if i is even or the sum of numbers from 1 to i otherwise. i starts from 1. the factorial of i is the multiplication of the numbers from 1 to i (1 * 2 * ... * i).
[{"input": 5, "expected_output": [1, 2, 6, 24, 15]}]
def f(n: int) -> List[int] """Implement the function f that takes n as a parameter, and returns a list of size n, such that the value of the element at index i is the factorial of i if i is even or the sum of numbers from 1 to i otherwise. i starts from 1. the factorial of i is the multiplication of the numbers from 1 to i (1 * 2 * ... * i). """
def problem_spec -- function signature (implementation: Int β†’ List Int) -- inputs (n: Int) := -- spec let spec (result: List Int) := (result.length = n) ∧ (forall i: Nat, (1 ≀ i ∧ i ≀ n ∧ Even i) β†’ (result[i-1]! = Nat.factorial i)) ∧ (forall i: Nat, (1 ≀ i ∧ i ≀ n ∧ Odd i) β†’ (result[i-1]! = (List.range (i+1)).sum)) -- program termination βˆƒ result, implementation n = result ∧ spec result
def generated_spec -- function signature (impl: Int β†’ List Int) -- inputs (n: Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ n, problem_spec impl n) ↔ (βˆ€ n, generated_spec impl n) :=
sorry
def implementation (n: Int) : List Int :=
sorry
-- #test implementation 5 = [1, 2, 6, 24, 15] -- #test implementation 7 = [1, 2, 6, 24, 15, 720, 28] -- #test implementation 1 = [1] -- #test implementation 3 = [1, 2, 6]
theorem correctness (n: Int) : problem_spec implementation n :=
by sorry
null
null
null
def even_odd_palindrome(n: nat) -> (nat, nat)
Given a positive integer n, return a tuple that has the number of even and odd integer palindromes that fall within the range(1, n), inclusive.
[{"input": 3, "expected_output": "(1, 2)"}, {"input": 12, "expected_output": "(4, 6)"}]
def even_odd_palindrome(n: nat) -> (nat, nat) """Given a positive integer n, return a tuple that has the number of even and odd integer palindromes that fall within the range(1, n), inclusive. """
def problem_spec -- function signature (implementation: Nat β†’ Nat Γ— Nat) -- inputs (n: Nat) := -- spec let spec (result: Nat Γ— Nat) := let is_palindrome (k: Nat): Prop := List.Palindrome (Nat.digits 10 k); let even_palindrome (k: Nat): Prop := (Even k) ∧ (is_palindrome k); let odd_palindrome (k: Nat): Prop := (Odd k) ∧ (is_palindrome k); n > 0 β†’ (1 < n β†’ let impl_n_minus_1 := implementation (n - 1); ((even_palindrome n) β†’ result.1 = 1 + impl_n_minus_1.1) ∧ ((odd_palindrome n) β†’ result.2 = 1 + impl_n_minus_1.2) ∧ (Β¬ (odd_palindrome n) β†’ result.2 = impl_n_minus_1.2) ∧ (Β¬ (even_palindrome n) β†’ result.1 = impl_n_minus_1.1)) ∧ (n = 1 β†’ (result.1 = 0) ∧ (result.2 = 1)); -- program termination βˆƒ result, implementation n = result ∧ spec result
def generated_spec -- function signature (impl: Nat β†’ Nat Γ— Nat) -- inputs (n: Nat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ n, problem_spec impl n) ↔ (βˆ€ n, generated_spec impl n) :=
sorry
def implementation (n: Nat) : Nat Γ— Nat :=
sorry
-- #test implementation 123 = (8, 13) -- #test implementation 12 = (4, 6) -- #test implementation 3 = (1, 2) -- #test implementation 63 = (6, 8) -- #test implementation 25 = (5, 6) -- #test implementation 19 = (4, 6) -- #test implementation 9 = (4, 5)
theorem correctness (n: Nat) : problem_spec implementation n :=
by sorry
null
null
null
def count_nums(arr: List[int]) -> int
Write a function count_nums which takes an array of integers and returns the number of elements which has a sum of digits > 0. If a number is negative, then its first signed digit will be negative: e.g. -123 has signed digits -1, 2, and 3.
[{"input": [], "expected_output": 0}, {"input": [-1, 11, -11], "expected_output": 1}, {"input": [1, 1, 2], "expected_output": 3}]
def count_nums(arr: List[int]) -> int """Write a function count_nums which takes an array of integers and returns the number of elements which has a sum of digits > 0. If a number is negative, then its first signed digit will be negative: e.g. -123 has signed digits -1, 2, and 3. """
def problem_spec -- function signature (implementation: List Int β†’ Int) -- inputs (arr: List Int) := -- spec let spec (result: Int) := let dig_sum (x: Int): Int := let digs := x.natAbs.digits 10; if x >= 0 then (List.map (fun t => (t: Int)) digs).sum else (List.map (fun t => (t: Int)) (digs.drop 1)).sum - (digs[0]! : Int); (arr = [] β†’ result = 0) ∧ (arr β‰  [] β†’ 0 < (dig_sum arr[0]!) β†’ result = 1 + implementation (arr.drop 1)) ∧ (arr β‰  [] β†’ (dig_sum arr[0]!) ≀ 0 β†’ result = implementation (arr.drop 1)); -- program termination βˆƒ result, implementation arr = result ∧ spec result
def generated_spec -- function signature (impl: List Int β†’ Int) -- inputs (arr: List Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ arr, problem_spec impl arr) ↔ (βˆ€ arr, generated_spec impl arr) :=
sorry
def implementation (arr: List Int) : Int :=
let signed_digits(x: Int): List Int := let x': Nat := Int.natAbs x; let xs: List Nat := Nat.digits 10 x'; if x >= 0 then xs else (-Int.ofNat (xs[0]!)) :: xs.tail; ((List.map (fun (x: Int) => (signed_digits x).sum) arr).filter (fun (x: Int) => x > 0)).length
-- #test implementation [] = 0 -- #test implementation [-1, -2, 0] = 0 -- #test implementation [1, 1, 2, -2, 3, 4, 5] = 6 -- #test implementation [1, 6, 9, -6, 0, 1, 5] = 5 -- #test implementation [1, 100, 98, -7, 1, -1] = 4 -- #test implementation [12, 23, 34, -45, -56, 0] = 5 -- #test implementation [-0, 1^0] = 1 -- #test implementation [1] = 1
theorem correctness (arr: List Int) : problem_spec implementation arr :=
by sorry
null
null
null
def move_one_ball(arr: List[int]) -> bool
We have an array 'arr' of N integers arr[1], arr[2], ..., arr[N].The numbers in the array will be randomly ordered. Your task is to determine if it is possible to get an array sorted in non-decreasing order by performing the following operation on the given array: You are allowed to perform right shift operation any number of times. One right shift operation means shifting all elements of the array by one position in the right direction. The last element of the array will be moved to the starting position in the array i.e. 0th index. If it is possible to obtain the sorted array by performing the above operation then return True else return False. If the given array is empty then return True. Note: The given list is guaranteed to have unique elements.
[{"input": [3, 4, 5, 1, 2], "expected_output": true}, {"input": [3, 5, 4, 1, 2], "expected_output": false}]
def move_one_ball(arr: List[int]) -> bool """We have an array 'arr' of N integers arr[1], arr[2], ..., arr[N].The numbers in the array will be randomly ordered. Your task is to determine if it is possible to get an array sorted in non-decreasing order by performing the following operation on the given array: You are allowed to perform right shift operation any number of times. One right shift operation means shifting all elements of the array by one position in the right direction. The last element of the array will be moved to the starting position in the array i.e. 0th index. If it is possible to obtain the sorted array by performing the above operation then return True else return False. If the given array is empty then return True. Note: The given list is guaranteed to have unique elements. """
def problem_spec -- function signature (implementation: List Int β†’ Bool) -- inputs (arr: List Int) := let is_shifted (xs: List Int) (ys: List Int) (i: Nat) := (xs.length = ys.length) ∧ (0 <= i) ∧ (i < xs.length) ∧ (forall j, (0 <= j ∧ j < ys.length) β†’ (ys[j]! = xs[(j-i) % xs.length]!)) -- spec let spec (result: Bool) := ((arr = []) β†’ (result = True)) ∧ result ↔ (exists i, exists arr_shifted, (is_shifted arr arr_shifted i) ∧ (List.Sorted Int.le arr_shifted)) -- program termination βˆƒ result, implementation arr = result ∧ spec result
def generated_spec -- function signature (impl: List Int β†’ Bool) -- inputs (arr: List Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ arr, problem_spec impl arr) ↔ (βˆ€ arr, generated_spec impl arr) :=
sorry
def implementation (arr: List Int) : Bool :=
sorry
-- #test implementation [3, 4, 5, 1, 2] = True -- #test implementation [3, 5, 10, 1, 2] = True -- #test implementation [4, 3, 1, 2] = False -- #test implementation [3, 5, 4, 1, 2] = False -- #test implementation [] = True
theorem correctness (arr: List Int) : problem_spec implementation arr :=
by sorry
null
null
null
def exchange(lst1: list[int], lst2: list[int]) -> str
In this problem, you will implement a function that takes two lists of numbers, and determines whether it is possible to perform an exchange of elements between them to make lst1 a list of only even numbers. There is no limit on the number of exchanged elements between lst1 and lst2. If it is possible to exchange elements between the lst1 and lst2 to make all the elements of lst1 to be even, return "YES". Otherwise, return "NO". It is assumed that the input lists will be non-empty.
[{"input": "([1, 2, 3, 4], [1, 2, 3, 4])", "expected_output": "YES"}, {"input": "([1, 2, 3, 4], [1, 5, 3, 4])", "expected_output": "NO"}]
def exchange(lst1: list[int], lst2: list[int]) -> str """In this problem, you will implement a function that takes two lists of numbers, and determines whether it is possible to perform an exchange of elements between them to make lst1 a list of only even numbers. There is no limit on the number of exchanged elements between lst1 and lst2. If it is possible to exchange elements between the lst1 and lst2 to make all the elements of lst1 to be even, return "YES". Otherwise, return "NO". It is assumed that the input lists will be non-empty. """
def problem_spec -- function signature (implementation: List Int β†’ List Int β†’ String) -- inputs (lst1: List Int) (lst2: List Int) := -- spec let spec (result : String) := lst1 β‰  [] β†’ lst2 β‰  [] β†’ let bool_result := βˆƒ exchange: List (Nat Γ— Nat), let lst1_idxs := exchange.map (fun (a, b) => a) let lst2_idxs := exchange.map (fun (a, b) => b) lst1_idxs.all (fun i => i < lst1.length) ∧ lst2_idxs.all (fun i => i < lst2.length) ∧ lst1_idxs.Nodup ∧ lst2_idxs.Nodup ∧ βˆ€ i, i < lst1.length β†’ (i βˆ‰ lst1_idxs β†’ Even (lst1.get! i)) ∧ (i ∈ lst1_idxs β†’ -- find the (a, b) in exchange where a = i let i_idx := (lst1_idxs.indexesOf i).head! Even (lst2.get! (lst2_idxs.get! i_idx))) (bool_result β†’ result = "YES") ∧ (result = "NO" β†’ Β¬ bool_result) ∧ (result β‰  "YES" ∧ result β‰  "NO" β†’ False) -- program termination βˆƒ result, implementation lst1 lst2 = result ∧ spec result
def generated_spec -- function signature (impl: List Int β†’ List Int β†’ String) -- inputs (lst1: List Int) (lst2: List Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ lst1 lst2, problem_spec impl lst1 lst2) ↔ (βˆ€ lst1 lst2, generated_spec impl lst1 lst2) :=
sorry
def implementation (lst1: List Int) (lst2: List Int) : String :=
sorry
-- #test implementation ([1, 2, 3, 4], [1, 2, 3, 4]) = "YES" -- #test implementation ([1, 2, 3, 4], [1, 5, 3, 4]) = "NO"
theorem correctness (lst1: List Int) (lst2: List Int) : problem_spec implementation lst1 lst2 :=
by sorry
null
null
null
def histogram(s : str) -> Dict[str, int]
Given a string representing a space separated lowercase letters, return a dictionary of the letter with the most repetition and containing the corresponding count. If several letters have the same occurrence, return all of them. -- Note(George): I believe the equality extensionality for HashMaps makes this spec true.
[{"input": "a b c", "expected_output": {"a": 1, "b": 1, "c": 1}}, {"input": "a b b a", "expected_output": {"a": 2, "b": 2}}, {"input": "a b c a b", "expected_output": {"a": 2, "b": 2}}, {"input": "b b b b a", "expected_output": {"b": 4}}, {"input": "", "expected_output": {}}]
def histogram(s : str) -> Dict[str, int] """Given a string representing a space separated lowercase letters, return a dictionary of the letter with the most repetition and containing the corresponding count. If several letters have the same occurrence, return all of them. -- Note(George): I believe the equality extensionality for HashMaps makes this spec true. """
def problem_spec -- function signature (implementation: String β†’ Std.HashMap Char Nat) -- inputs (s: String) := -- spec let spec (result : Std.HashMap Char Nat) := let chars := s.splitOn " " chars.all (fun c => c.length = 1) ∧ s.all (fun c => c.isLower ∨ c = ' ') β†’ βˆ€ key ∈ result.keys, (key.isLower ∧ key ∈ s.data ∧ result.get! key = s.count key) ∧ (βˆ€ char ∈ s.data, char.isLower β†’ ((βˆƒ char2 ∈ s.data, char2.isLower ∧ char2 β‰  char ∧ s.count char < s.count char2) ↔ char βˆ‰ result.keys)) -- program termination βˆƒ result, implementation s = result ∧ spec result
def generated_spec -- function signature (impl: String β†’ Std.HashMap Char Nat) -- inputs (s: String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ s, problem_spec impl s) ↔ (βˆ€ s, generated_spec impl s) :=
sorry
def implementation (s: String) : Std.HashMap Char Nat :=
sorry
-- #test implementation 'a b c' = {'a': 1, 'b': 1, 'c': 1} -- #test implementation 'a b b a' = {'a': 2, 'b': 2} -- #test implementation 'a b c a b' = {'a': 2, 'b': 2} -- #test implementation 'b b b b a' = {'b': 4} -- #test implementation '' = {}
theorem correctness (s: String) : problem_spec implementation s :=
by sorry
null
null
null
def reverse_delete(s : str, c : str) -> (str, bool)
We are given two strings s and c, you have to deleted all the characters in s that are equal to any character in c then check if the result string is palindrome. A string is called palindrome if it reads the same backward as forward. You should return a tuple containing the result string and True/False for the check. -- Note: We assume the deletions preserve the order of the remaining characters.
[{"input": ["abcde", "ae"], "expected_output": "(\"bcd\", False)"}, {"input": ["abcdef", "b"], "expected_output": "(\"acdef\", False)"}, {"input": ["abcdedcba", "ab"], "expected_output": "('cdedc', True)"}]
def reverse_delete(s : str, c : str) -> (str, bool) """We are given two strings s and c, you have to deleted all the characters in s that are equal to any character in c then check if the result string is palindrome. A string is called palindrome if it reads the same backward as forward. You should return a tuple containing the result string and True/False for the check. -- Note: We assume the deletions preserve the order of the remaining characters. """
def problem_spec -- function signature (implementation: String β†’ String β†’ (String Γ— Bool)) -- inputs (s: String) (c: String) := -- spec let spec (result : String Γ— Bool) := let (result_str, result_bool) := result result_bool = (List.Palindrome result_str.data) ∧ (c.data.length = 0 β†’ result_str = s) ∧ (c.data.length > 0 β†’ result_str = (implementation (String.join ((s.data.filter (fun x => x β‰  c.data.head!)).map (fun c => String.mk [c]))) (c.drop 1)).fst) -- program termination βˆƒ result, implementation s c = result ∧ spec result
def generated_spec -- function signature (impl: String β†’ String β†’ (String Γ— Bool)) -- inputs (s: String) (c: String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ s c, problem_spec impl s c) ↔ (βˆ€ s c, generated_spec impl s c) :=
sorry
def implementation (s: String) (c: String) : String Γ— Bool :=
sorry
-- #test implementation "abcde" "ae" = ("bcd", False) -- #test implementation "abcdef" "b" = ("acdef", False) -- #test implementation "abcdedcba" "ab" = ("cdedc", True)
theorem correctness (s c: String) : problem_spec implementation s c :=
by sorry
null
null
null
def odd_count(lst : list[str]) -> list[str]
Given a list of strings, where each string consists of only digits, return a list. Each element i of the output should be "the number of odd elements in the string i of the input." where all the i's should be replaced by the number of odd digits in the i'th string of the input. Note(George): Found it hard to not leak the implementation, so I opted for a recursive statement.
[{"input": ["1234567"], "expected_output": ["the number of odd elements 4n the str4ng 4 of the 4nput."]}, {"input": ["3", "11111111"], "expected_output": ["the number of odd elements 1n the str1ng 1 of the 1nput.", "the number of odd elements 8n the str8ng 8 of the 8nput."]}]
def odd_count(lst : list[str]) -> list[str] """Given a list of strings, where each string consists of only digits, return a list. Each element i of the output should be "the number of odd elements in the string i of the input." where all the i's should be replaced by the number of odd digits in the i'th string of the input. Note(George): Found it hard to not leak the implementation, so I opted for a recursive statement. """
def problem_spec -- function signature (implementation: List String β†’ List String) -- inputs (lst: List String) := -- spec let spec (result : List String) := lst.all (fun s => s.data.all (fun c => c.isDigit)) β†’ (result.length = 0 ↔ lst.length = 0) ∧ (result.length > 0 β†’ let num_odd_digits := (lst.head!.data.filter (fun c => c.isDigit ∧ c.toNat % 2 = 1)).length result.head! = "the number of odd elements " ++ num_odd_digits.repr ++ "n the str" ++ num_odd_digits.repr ++ "ng " ++ num_odd_digits.repr ++ " of the " ++ num_odd_digits.repr ++ "nput." ∧ result.tail! = implementation lst.tail!) -- program termination βˆƒ result, implementation lst = result ∧ spec result
def generated_spec -- function signature (impl: List String β†’ List String) -- inputs (lst: List String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ lst, problem_spec impl lst) ↔ (βˆ€ lst, generated_spec impl lst) :=
sorry
def implementation (lst: List String) : List String :=
sorry
-- #test implementation ['1234567'] = ["the number of odd elements 4n the str4ng 4 of the 4nput."] -- #test implementation ['3',"11111111"] = ["the number of odd elements 1n the str1ng 1 of the 1nput.", -- "the number of odd elements 8n the str8ng 8 of the 8nput."]
theorem correctness (lst: List String) : problem_spec implementation lst :=
by sorry
null
null
null
def minSubArraySum(nums : list[int]) -> int
Given an array of integers nums, find the minimum sum of any non-empty sub-array of nums.
[{"input": [2, 3, 4, 1, 2, 4], "expected_output": 1}, {"input": [-1, -2, -3], "expected_output": -6}]
def minSubArraySum(nums : list[int]) -> int """Given an array of integers nums, find the minimum sum of any non-empty sub-array of nums. """
def problem_spec -- function signature (implementation: List Int β†’ Int) -- inputs (nums: List Int) := -- spec let spec (result : Int) := (βˆ€ subarray ∈ nums.sublists, subarray.length > 0 β†’ result ≀ subarray.sum) ∧ (βˆƒ subarray ∈ nums.sublists, subarray.length > 0 ∧ result = subarray.sum) -- program termination βˆƒ result, implementation nums = result ∧ spec result
def generated_spec -- function signature (impl: List Int β†’ Int) -- inputs (nums: List Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ nums, problem_spec impl nums) ↔ (βˆ€ nums, generated_spec impl nums) :=
sorry
def implementation (nums: List Int) : Int :=
sorry
-- #test implementation [2, 3, 4, 1, 2, 4] = 1 -- #test implementation [-1, -2, -3] = -6
theorem correctness (nums: List Int) : problem_spec implementation nums :=
by sorry
null
null
null
def max_fill_count(grid : list[list[int]], capacity : int) -> int
You are given a rectangular grid of wells. Each row represents a single well, and each 1 in a row represents a single unit of water. Each well has a corresponding bucket that can be used to extract water from it, and all buckets have the same capacity. Your task is to use the buckets to empty the wells. Output the number of times you need to lower the buckets. Constraints: * all wells have the same length * 1 <= grid.length <= 10^2 * 1 <= grid[:,1].length <= 10^2 * grid[i][j] -> 0 | 1 * 1 <= capacity <= 10
[{"input": "([[0,0,1,0], [0,1,0,0], [1,1,1,1]], 1)", "expected_output": 6}, {"input": "([[0,0,1,1], [0,0,0,0], [1,1,1,1], [0,1,1,1]], 2)", "expected_output": 5}, {"input": "([[0,0,0], [0,0,0]], 5)", "expected_output": 0}]
def max_fill_count(grid : list[list[int]], capacity : int) -> int """You are given a rectangular grid of wells. Each row represents a single well, and each 1 in a row represents a single unit of water. Each well has a corresponding bucket that can be used to extract water from it, and all buckets have the same capacity. Your task is to use the buckets to empty the wells. Output the number of times you need to lower the buckets. Constraints: * all wells have the same length * 1 <= grid.length <= 10^2 * 1 <= grid[:,1].length <= 10^2 * grid[i][j] -> 0 | 1 * 1 <= capacity <= 10 """
def problem_spec -- function signature (implementation: List (List Nat) β†’ Nat β†’ Nat) -- inputs (grid: List (List Nat)) (capacity: Nat) := -- spec let spec (result : Nat) := (grid.all (fun row => row.all (fun cell => cell = 0 ∨ cell = 1))) β†’ (βˆƒ len : Nat, grid.all (fun row => row.length = len)) β†’ (result = 0 ↔ grid.length = 0) ∧ (grid.length > 0 β†’ let well_water_count := grid.head!.sum; result - (well_water_count / capacity) - (if well_water_count % capacity > 0 then 1 else 0) = implementation grid.tail! capacity) -- program termination βˆƒ result, implementation grid capacity = result ∧ spec result
def generated_spec -- function signature (impl: List (List Nat) β†’ Nat β†’ Nat) -- inputs (grid: List (List Nat)) (capacity: Nat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ grid capacity, problem_spec impl grid capacity) ↔ (βˆ€ grid capacity, generated_spec impl grid capacity) :=
sorry
def implementation (grid: List (List Nat)) (capacity: Nat) : Nat :=
sorry
-- #test implementation [[0,0,1,0], [0,1,0,0], [1,1,1,1]] 1 = 6 -- #test implementation [[0,0,1,1], [0,0,0,0], [1,1,1,1], [0,1,1,1]] 2 = 5 -- #test implementation [[0,0,0], [0,0,0]] 5 = 0
theorem correctness (grid: List (List Nat)) (capacity: Nat) : problem_spec implementation grid capacity :=
by sorry
null
null
null
def max_fill_count(grid : list[list[int]], capacity : int) -> int
Please write a function that sorts an array of non-negative integers according to number of ones in their binary representation in ascending order. For similar number of ones, sort based on decimal value.
[{"input": [1, 5, 2, 3, 4], "expected_output": [1, 2, 3, 4, 5]}, {"input": [1, 0, 2, 3, 4], "expected_output": [0, 1, 2, 3, 4]}]
def max_fill_count(grid : list[list[int]], capacity : int) -> int """Please write a function that sorts an array of non-negative integers according to number of ones in their binary representation in ascending order. For similar number of ones, sort based on decimal value. """
def problem_spec -- function signature (implementation: List Nat β†’ List Nat) -- inputs (lst: List Nat) := -- spec let spec (result : List Nat) := βˆ€ x : Nat, lst.count x = result.count x ∧ result.length = lst.length ∧ (βˆ€ i j : Nat, i < j β†’ j < result.length β†’ Nat.digits 2 (result.get! i) < Nat.digits 2 (result.get! j) ∨ (Nat.digits 2 (result.get! i) = Nat.digits 2 (result.get! j) ∧ result.get! i < result.get! j)) -- program termination βˆƒ result, implementation lst = result ∧ spec result
def generated_spec -- function signature (impl: List Nat β†’ List Nat) -- inputs (lst: List Nat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ lst, problem_spec impl lst) ↔ (βˆ€ lst, generated_spec impl lst) :=
sorry
def implementation (lst: List Nat) : List Nat :=
sorry
-- #test implementation [1, 5, 2, 3, 4] = [1, 2, 3, 4, 5] -- #test implementation [1, 0, 2, 3, 4] = [0, 1, 2, 3, 4]
theorem correctness (lst: List Nat) : problem_spec implementation lst :=
by sorry
null
null
null
def select_words(s : str, n : int) -> list[str]
Given a string s and a natural number n, you have been tasked to implement a function that returns a list of all words from string s that contain exactly n consonants, in order these words appear in the string s. If the string s is empty then the function should return an empty list. Note: you may assume the input string contains only letters and spaces.
[{"input": "(\"Mary had a little lamb\", 4)", "expected_output": ["little"]}, {"input": "(\"Mary had a little lamb\", 3)", "expected_output": ["Mary", "lamb"]}, {"input": "(\"simple white space\", 2)", "expected_output": []}, {"input": "(\"Hello world\", 4)", "expected_output": ["world"]}, {"input": "(\"Uncle sam\", 3)", "expected_output": ["Uncle"]}]
def select_words(s : str, n : int) -> list[str] """Given a string s and a natural number n, you have been tasked to implement a function that returns a list of all words from string s that contain exactly n consonants, in order these words appear in the string s. If the string s is empty then the function should return an empty list. Note: you may assume the input string contains only letters and spaces. """
def problem_spec -- function signature (implementation: String β†’ Nat β†’ List String) -- inputs (s: String) (n: Nat) := -- spec let spec (result : List String) := let is_consonant (c: Char) := c βˆ‰ ['a', 'e', 'i', 'o', 'u'] ∧ c βˆ‰ ['A', 'E', 'I', 'O', 'U'] ∧ c.isAlpha s.all (fun c => c = ' ' ∨ c.isAlpha) β†’ let words := s.splitOn " " (result = [] ↔ (s.length = 0 ∨ words.all (fun word => (word.data.filter (fun c => is_consonant c)).length β‰  n))) ∧ (result.length > 0 β†’ let first_word := result[0]! first_word ∈ words ∧ (first_word.data.filter (fun c => is_consonant c)).length = n ∧ let first_word_idx := words.indexOf first_word (βˆ€ i, i < first_word_idx β†’ (words[i]!.data.filter (fun c => is_consonant c)).length β‰  n) ∧ result.tail! = implementation ((words.drop (first_word_idx + 1)).foldl (fun acc word => acc ++ " " ++ word) "") n ) -- program termination βˆƒ result, implementation s n = result ∧ spec result
def generated_spec -- function signature (impl: String β†’ Nat β†’ List String) -- inputs (s: String) (n: Nat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ s n, problem_spec impl s n) ↔ (βˆ€ s n, generated_spec impl s n) :=
sorry
def implementation (s: String) (n: Nat) : List String :=
sorry
-- #test implementation "Mary had a little lamb" 4 = ["little"] -- #test implementation "Mary had a little lamb" 3 = ["Mary", "lamb"] -- #test implementation "simple white space" 2 = [] -- #test implementation "Hello world" 4 = ["world"] -- #test implementation "Uncle sam" 3 = ["Uncle"]
theorem correctness (s: String) (n: Nat) : problem_spec implementation s n :=
by sorry
null
null
null
def get_closest_vowel(s : str) -> str
You are given a word. Your task is to find the closest vowel that stands between two consonants from the right side of the word (case sensitive). Vowels in the beginning and ending doesn't count. Return empty string if you didn't find any vowel met the above condition. You may assume that the given string contains English letter only. Note: The "closest" is interpreted as the closest to the end of the word, not the closest to the consonants.
[{"input": "yogurt", "expected_output": "u"}, {"input": "FULL", "expected_output": "U"}, {"input": "quick", "expected_output": "i"}, {"input": "ab", "expected_output": ""}]
def get_closest_vowel(s : str) -> str """You are given a word. Your task is to find the closest vowel that stands between two consonants from the right side of the word (case sensitive). Vowels in the beginning and ending doesn't count. Return empty string if you didn't find any vowel met the above condition. You may assume that the given string contains English letter only. Note: The "closest" is interpreted as the closest to the end of the word, not the closest to the consonants. """
def problem_spec -- function signature (implementation: String β†’ String) -- inputs (s: String) := -- spec let spec (result : String) := s.data.all (fun c => c.isAlpha) β†’ let is_consonant (c: Char) := c βˆ‰ ['a', 'e', 'i', 'o', 'u'] ∧ c βˆ‰ ['A', 'E', 'I', 'O', 'U'] ∧ c.isAlpha (result = "" β†’ Β¬ βˆƒ (i j k : Nat), i < j ∧ j < k ∧ k < s.length ∧ is_consonant s.data[i]! ∧ Β¬ is_consonant s.data[j]! ∧ is_consonant s.data[k]!) ∧ (result β‰  "" β†’ result.length = 1 ∧ result.data[0]! ∈ s.data ∧ Β¬ is_consonant result.data[0]! ∧ βˆƒ (i j k : Nat), i < j ∧ j < k ∧ k < s.length ∧ is_consonant s.data[i]! ∧ Β¬ is_consonant s.data[j]! ∧ is_consonant s.data[k]! ∧ result.data[0]! = s.data[j]! ∧ (βˆ€ (i' j' k' : Nat), i' < j' ∧ j' < k' ∧ k' < s.length ∧ is_consonant s.data[i']! ∧ Β¬ is_consonant s.data[j']! ∧ is_consonant s.data[k']! β†’ j' ≀ j) ) -- program termination βˆƒ result, implementation s = result ∧ spec result
def generated_spec -- function signature (impl: String β†’ String) -- inputs (s: String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ s, problem_spec impl s) ↔ (βˆ€ s, generated_spec impl s) :=
sorry
def implementation (s: String) : String :=
sorry
-- #test implementation "yogurt" = "u" -- #test implementation "FULL" = "U" -- #test implementation "quick" = "i" -- #test implementation "ab" = ""
theorem correctness (s: String) : problem_spec implementation s :=
by sorry
null
null
null
def match_parens(l : list[str]) -> str
You are given a list of two strings, both strings consist of open parentheses '(' or close parentheses ')' only. Your job is to check if it is possible to concatenate the two strings in some order, that the resulting string will be good. A string S is considered to be good if and only if all parentheses in S are balanced. For example: the string '(())()' is good, while the string '())' is not. Return 'Yes' if there's a way to make a good string, and return 'No' otherwise.
[{"input": ["()(", ")"], "expected_output": "Yes"}, {"input": [")", ")"], "expected_output": "No"}]
def match_parens(l : list[str]) -> str """You are given a list of two strings, both strings consist of open parentheses '(' or close parentheses ')' only. Your job is to check if it is possible to concatenate the two strings in some order, that the resulting string will be good. A string S is considered to be good if and only if all parentheses in S are balanced. For example: the string '(())()' is good, while the string '())' is not. Return 'Yes' if there's a way to make a good string, and return 'No' otherwise. """
def problem_spec -- function signature (implementation: List String β†’ String) -- inputs (l: List String) := -- spec let spec (result : String) := l.length = 2 β†’ l[0]!.all (fun c => c = '(' ∨ c = ')') β†’ l[1]!.all (fun c => c = '(' ∨ c = ')') β†’ let res := (balanced_paren_non_computable (l[0]! ++ l[1]!) '(' ')' ∨ balanced_paren_non_computable (l[1]! ++ l[0]!) '(' ')') (res β†’ result = "Yes") ∧ (Β¬ res β†’ result = "No") -- program termination βˆƒ result, implementation l = result ∧ spec result
def generated_spec -- function signature (impl: List String β†’ String) -- inputs (l: List String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ l, problem_spec impl l) ↔ (βˆ€ l, generated_spec impl l) :=
sorry
def implementation (l: List String) : String :=
sorry
-- #test implementation ['()(', ')'] = "Yes" -- #test implementation [')', ')'] = "No"
theorem correctness (l: List String) : problem_spec implementation l :=
by sorry
null
null
null
def maximum(arr: List[int], k: int) -> List[int]
Given an array arr of integers and a positive integer k, return a sorted list of length k with the maximum k numbers in arr. Note: 1. The length of the array will be in the range of [1, 1000]. 2. The elements in the array will be in the range of [-1000, 1000]. 3. 0 <= k <= len(arr)
[{"input": [[2, 4, 3, 1], 3], "expected_output": [2, 3, 4]}, {"input": [[2, 4, 3, 1], 0], "expected_output": []}]
def maximum(arr: List[int], k: int) -> List[int] """Given an array arr of integers and a positive integer k, return a sorted list of length k with the maximum k numbers in arr. Note: 1. The length of the array will be in the range of [1, 1000]. 2. The elements in the array will be in the range of [-1000, 1000]. 3. 0 <= k <= len(arr) """
def problem_spec -- function signature (impl: List Int β†’ Int β†’ List Int) -- inputs (arr: List Int) (k: Int) := -- spec let spec (result: List Int) := 1 ≀ arr.length β†’ arr.length ≀ 1000 β†’ arr.all (fun x => -1000 ≀ x ∧ x ≀ 1000) β†’ 0 ≀ k β†’ k ≀ arr.length β†’ result.length = k ∧ result.Sorted (Β· ≀ Β·) ∧ βˆ€ x ∈ result, x ∈ arr ∧ let result_reversed := result.reverse; -- reverse to get last element match result_reversed with | [] => k = 0 | max :: remaining_reversed => arr.max? = some max ∧ impl (arr.erase max) (k-1) = (remaining_reversed.reverse) -- program terminates βˆƒ result, impl arr k = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: List Int β†’ Int β†’ List Int) -- inputs (arr: List Int) (k: Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ arr k, problem_spec impl arr k) ↔ (βˆ€ arr k, generated_spec impl arr k) :=
sorry
def implementation (arr: List Int) (k: Int) : List Int :=
sorry
-- #test implementation [2, 4, 3, 1] 3 = [2, 3, 4] -- #test implementation [2, 4, 3, 1] 0 = []
theorem correctness (arr: List Int) (k: Int) : problem_spec implementation arr k :=
sorry
null
null
null
def solution(lst: List[int]) -> int
Given a non-empty list of integers, return the sum of all of the odd elements that are in even positions.
[{"input": [5, 8, 7, 1], "expected_output": 12}, {"input": [3, 3, 3, 3, 3], "expected_output": 9}, {"input": [30, 13, 24, 321], "expected_output": 0}]
def solution(lst: List[int]) -> int """Given a non-empty list of integers, return the sum of all of the odd elements that are in even positions. """
def problem_spec -- function signature (impl: List Int β†’ Int) -- inputs (lst: List Int) := -- spec let spec (result : Int) := lst β‰  [] β†’ βˆ€ i, i < lst.length ∧ i % 2 = 0 β†’ (lst.length = 1 β†’ impl lst = 0) ∧ (i + 1 < lst.length β†’ (lst[i + 1]! % 2 = 1 β†’ impl (lst.drop i) = lst[i + 1]! + (if i + 2 < lst.length then impl (lst.drop (i+2)) else 0)) ∧ (lst[i + 1]! % 2 = 0 β†’ impl (lst.drop i) = if i + 2 < lst.length then impl (lst.drop (i+2)) else 0) ) -- program terminates βˆƒ result, impl lst = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: List Int β†’ Int) -- inputs (lst: List Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ lst, problem_spec impl lst) ↔ (βˆ€ lst, generated_spec impl lst) :=
sorry
def implementation (lst: List Int) : Int :=
sorry
-- #test implementation ([5, 8, 7, 1]: List Int) = 12 -- #test implementation ([3, 3, 3, 3, 3]: List Int) = 9 -- #test implementation ([30, 13, 24, 321]: List Int) = 0
theorem correctness (lst: List Int) : problem_spec implementation lst :=
sorry
null
null
null
def add_elements(arr: List[int], k: int) -> int
Given a non-empty array of integers arr and an integer k, return the sum of the elements with at most two digits from the first k elements of arr. Constraints: 1. 1 <= len(arr) <= 100 2. 1 <= k <= len(arr)
[{"input": [[111, 21, 3, 4000, 5, 6, 7, 8, 9], 4], "expected_output": 24}]
def add_elements(arr: List[int], k: int) -> int """Given a non-empty array of integers arr and an integer k, return the sum of the elements with at most two digits from the first k elements of arr. Constraints: 1. 1 <= len(arr) <= 100 2. 1 <= k <= len(arr) """
def problem_spec -- function signature (impl: List Int β†’ Nat β†’ Int) -- inputs (arr: List Int) (k: Nat) := -- spec let spec (result: Int) := 1 ≀ arr.length β†’ arr.length ≀ 100 β†’ 1 ≀ k β†’ k ≀ arr.length β†’ ((βˆ€ i, 0 ≀ i ∧ i < k β†’ Β¬(arr[i]! ≀ 99 ∧ -99 ≀ arr[i]!)) β†’ result = 0) ∧ βˆƒ i, i < k ∧ arr[i]! ≀ 99 ∧ -99 ≀ arr[i]! ∧ result = arr[i]! + (if i = 0 then 0 else impl arr i) ∧ βˆ€ i', i < i' ∧ i' < k β†’ Β¬(arr[i']! ≀ 99 ∧ -99 ≀ arr[i']!) -- program termination βˆƒ result, impl arr k = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: List Int β†’ Nat β†’ Int) -- inputs (arr: List Int) (k: Nat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ arr k, problem_spec impl arr k) ↔ (βˆ€ arr k, generated_spec impl arr k) :=
sorry
def implementation (arr: List Int) (k: Nat) : Int :=
sorry
-- #test implementation ([111, 21, 3, 4000, 5, 6, 7, 8, 9]: List Int) 4 = 24
theorem correctness (arr: List Int) (k: Nat) : problem_spec implementation arr k :=
sorry
null
null
null
def get_odd_collatz (n: int) -> List[int]
Given a positive integer n, return a sorted list that has the odd numbers in collatz sequence. The Collatz conjecture is a conjecture in mathematics that concerns a sequence defined as follows: start with any positive integer n. Then each term is obtained from the previous term as follows: if the previous term is even, the next term is one half of the previous term. If the previous term is odd, the next term is 3 times the previous term plus 1. The conjecture is that no matter what value of n, the sequence will always reach 1. Note: 1. Collatz(1) is [1]. 2. returned list sorted in increasing order.
[{"input": 5, "expected_output": [1, 5]}]
def get_odd_collatz (n: int) -> List[int] """Given a positive integer n, return a sorted list that has the odd numbers in collatz sequence. The Collatz conjecture is a conjecture in mathematics that concerns a sequence defined as follows: start with any positive integer n. Then each term is obtained from the previous term as follows: if the previous term is even, the next term is one half of the previous term. If the previous term is odd, the next term is 3 times the previous term plus 1. The conjecture is that no matter what value of n, the sequence will always reach 1. Note: 1. Collatz(1) is [1]. 2. returned list sorted in increasing order. """
def problem_spec -- function signature (impl: Nat β†’ List Nat) -- inputs (n: Nat) := -- spec let spec (result: List Nat) := n > 0 β†’ result.Sorted (Β· < Β·) ∧ βˆ€ m, m ∈ result ↔ Odd m ∧ collatz_reachable n m -- m is reachable from starting point n -- program termination βˆƒ result, impl n = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: Nat β†’ List Nat) -- inputs (n: Nat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ n, problem_spec impl n) ↔ (βˆ€ n, generated_spec impl n) :=
sorry
def implementation (n: Nat) : List Nat :=
sorry
-- #test implementation 5 = [1, 5]
theorem correctness (n: Nat) : problem_spec implementation n :=
sorry
/-- name: collatz_reachable use: | Helper to check if a natural number m is reachable in the Collatz sequence starting at n. problems: - 123 -/ def collatz_reachable (n m : Nat) : Prop := βˆƒ k, Nat.iterate (fun x => if x % 2 = 0 then x / 2 else x * 3 + 1) k n = m
null
null
def valid_date(date: str) -> Bool
You have to write a function which validates a given date string and returns True if the date is valid otherwise False. The date is valid if all of the following rules are satisfied: 1. The date string is not empty. 2. The number of days is not less than 1 or higher than 31 days for months 1,3,5,7,8,10,12. And the number of days is not less than 1 or higher than 30 days for months 4,6,9,11. And, the number of days is not less than 1 or higher than 29 for the month 2. 3. The months should not be less than 1 or higher than 12. 4. The date should be in the format: mm-dd-yyyy
[{"input": "03-11-2000", "expected_output": true}, {"input": "15-01-2012", "expected_output": false}, {"input": "04-0-2040", "expected_output": false}, {"input": "06-04-2020", "expected_output": true}, {"input": "06/04/2020", "expected_output": false}]
def valid_date(date: str) -> Bool """You have to write a function which validates a given date string and returns True if the date is valid otherwise False. The date is valid if all of the following rules are satisfied: 1. The date string is not empty. 2. The number of days is not less than 1 or higher than 31 days for months 1,3,5,7,8,10,12. And the number of days is not less than 1 or higher than 30 days for months 4,6,9,11. And, the number of days is not less than 1 or higher than 29 for the month 2. 3. The months should not be less than 1 or higher than 12. 4. The date should be in the format: mm-dd-yyyy """
def problem_spec -- function signature (impl: String β†’ Bool) -- inputs (date: String) := -- spec let spec (result: Bool) := result = true ↔ βˆƒ m1 m2 sep1 d1 d2 sep2 y1 y2 y3 y4 : Char, date = String.mk [m1, m2, sep1, d1, d2, sep2, y1, y2, y3, y4] ∧ sep1 = '-' ∧ sep2 = '-' ∧ [m1, m2, d1, d2, y1, y2, y3, y4].all Char.isDigit ∧ let month := (String.mk [m1, m2]).toNat!; let day := (String.mk [d1, d2]).toNat!; 1 ≀ month ∧ month ≀ 12 ∧ (month ∈ ({4, 6, 9, 11}: List Nat) β†’ 1 ≀ day ∧ day ≀ 30) ∧ (month ∈ ({1, 3, 5, 7, 8, 10, 12}: List Nat) β†’ 1 ≀ day ∧ day ≀ 31) ∧ (month ∈ ({2}: List Nat) β†’ 1 ≀ day ∧ day ≀ 29) -- program terminates βˆƒ result, impl date = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: String β†’ Bool) -- inputs (date: String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ date, problem_spec impl date) ↔ (βˆ€ date, generated_spec impl date) :=
sorry
def implementation (date: String) : Bool :=
sorry
-- #test implementation "03-11-2000" = true -- #test implementation "15-01-2012" = false -- #test implementation "04-0-2040" = false -- #test implementation "06-04-2020" = true -- #test implementation "06/04/2020" = false
theorem correctness (date: String) : problem_spec implementation date :=
sorry
null
null
null
def minPath(grid, k)
Given a grid with N rows and N columns (N >= 2) and a positive integer k, each cell of the grid contains a value. Every integer in the range [1, N * N] inclusive appears exactly once on the cells of the grid. You have to find the minimum path of length k in the grid. You can start from any cell, and in each step you can move to any of the neighbor cells, in other words, you can go to cells which share an edge with you current cell. Please note that a path of length k means visiting exactly k cells (not necessarily distinct). You CANNOT go off the grid. A path A (of length k) is considered less than a path B (of length k) if after making the ordered lists of the values on the cells that A and B go through (let's call them lst_A and lst_B), lst_A is lexicographically less than lst_B, in other words, there exist an integer index i (1 <= i <= k) such that lst_A[i] < lst_B[i] and for any j (1 <= j < i) we have lst_A[j] = lst_B[j]. It is guaranteed that the answer is unique. Return an ordered list of the values on the cells that the minimum path go through.
[{"input": [[[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3], "expected_output": [1, 2, 3]}, {"input": [[5, 9, 3], [4, 1, 6], [7, 8, 2], 1], "expected_output": [1]}]
def minPath(grid, k) """Given a grid with N rows and N columns (N >= 2) and a positive integer k, each cell of the grid contains a value. Every integer in the range [1, N * N] inclusive appears exactly once on the cells of the grid. You have to find the minimum path of length k in the grid. You can start from any cell, and in each step you can move to any of the neighbor cells, in other words, you can go to cells which share an edge with you current cell. Please note that a path of length k means visiting exactly k cells (not necessarily distinct). You CANNOT go off the grid. A path A (of length k) is considered less than a path B (of length k) if after making the ordered lists of the values on the cells that A and B go through (let's call them lst_A and lst_B), lst_A is lexicographically less than lst_B, in other words, there exist an integer index i (1 <= i <= k) such that lst_A[i] < lst_B[i] and for any j (1 <= j < i) we have lst_A[j] = lst_B[j]. It is guaranteed that the answer is unique. Return an ordered list of the values on the cells that the minimum path go through. """
def problem_spec -- function signature (impl: List (List Nat) β†’ Nat β†’ List Nat) -- inputs (grid: List (List Nat)) (k: Nat) := -- spec let lexographically_less (a b: List Nat) : Prop := a.length = b.length ∧ a.length = k ∧ (βˆƒ i, i < k ∧ a.get! i < b.get! i ∧ (βˆ€ j, j < i β†’ a.get! j = b.get! j)); let rec is_valid_path (k': Nat) (path: List Nat) (grid: List (List Nat)) : Prop := let n := grid.length; path.length = k' β†’ (βˆƒ i j, (i < n ∧ j < n ∧ path.get! 0 = (grid.get! i).get! j) ∧ (1 < path.length β†’ ( βˆƒ i' j', i' < n ∧ j' < n ∧ (path.get! 1 = (grid.get! i').get! j') ∧ ((abs ((i: Int) - (i': Int)) = 1 ∧ j = j') ∨ (abs ((j: Int) - (j': Int)) = 1 ∧ i = i'))) ∧ (is_valid_path (k' - 1) (path.drop 1) grid)) ); let spec (result: List Nat) := let n := grid.length; (βˆ€ i, i < n β†’ (grid.get! i).length = n) β†’ (βˆ€ i j, i < n β†’ j < n ↔ ((grid.get! i).get! j) ∈ [1, n^2]) β†’ is_valid_path k result grid ∧ (βˆ€ path, is_valid_path k path grid β†’ lexographically_less result path); -- program terminates βˆƒ result, impl grid k = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: List (List Nat) β†’ Nat β†’ List Nat) -- inputs (grid: List (List Nat)) (k: Nat): Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ grid k, problem_spec impl grid k) ↔ (βˆ€ grid k, generated_spec impl grid k) :=
sorry
def implementation (grid: List (List Nat)) (k: Nat) : List Nat :=
sorry
-- #test implementation [[1,2,3], [4,5,6], [7,8,9]] 3 = [1,2,3] -- #test implementation [[5,9,3], [4,1,6], [7,8,2]] 1 = [1]
theorem correctness (grid: List (List Nat)) (k: Nat) : problem_spec implementation grid k :=
sorry
null
null
null
def is_sorted(lst: List[int]) -> Bool
Given a list of numbers, return whether or not they are sorted in ascending order. If list has more than 1 duplicate of the same number, return False. Assume no negative numbers and only integers.
[{"input": [5], "expected_output": true}, {"input": [1, 2, 3, 4, 5], "expected_output": true}, {"input": [1, 3, 2, 4, 5], "expected_output": false}, {"input": [1, 2, 3, 4, 5, 6], "expected_outupt": true}, {"input": [1, 2, 3, 4, 5, 6, 7], "expected_output": true}, {"input": [1, 3, 2, 4, 5, 6, 7], "expected_output": false}, {"input": [1, 2, 2, 3, 3, 4], "expected_output": true}, {"input": [1, 2, 2, 2, 3, 4], "expected_output": false}]
def is_sorted(lst: List[int]) -> Bool """Given a list of numbers, return whether or not they are sorted in ascending order. If list has more than 1 duplicate of the same number, return False. Assume no negative numbers and only integers. """
def problem_spec -- function signature (impl: List Int β†’ Bool) -- inputs (lst: List Int) := -- spec let sorted_ascending := lst.Sorted (Β· ≀ Β·); let ms := Multiset.ofList lst; let multiple_duplicates := βˆƒ i, i ∈ lst ∧ 2 < ms.count i; let spec (res: Bool) := res β†’ sorted_ascending ∧ res β†’ Β¬multiple_duplicates ∧ multiple_duplicates β†’ Β¬res ∧ Β¬sorted_ascending β†’ Β¬res; -- program terminates βˆƒ result, impl lst = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: List Int β†’ Bool) -- inputs (lst: List Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ lst, problem_spec impl lst) ↔ (βˆ€ lst, generated_spec impl lst) :=
sorry
def implementation (lst: List Int) : Bool :=
sorry
-- #test implementation [5] = true -- #test implementation [1, 2, 3, 4, 5] = true -- #test implementation [1, 3, 2, 4, 5] = false -- #test implementation [1, 2, 3, 4, 5, 6] = true -- #test implementation [1, 2, 3, 4, 5, 6, 7] = true -- #test implementation [1, 3, 2, 4, 5, 6, 7] = false -- #test implementation [1, 2, 2, 3, 3, 4] = true -- #test implementation [1, 2, 2, 2, 3, 4] = false
theorem correctness (lst: List Int) : problem_spec implementation lst :=
sorry
null
null
null
def intersection(interval1: Tuple[Int, Int], interval2: Tuple[Int, Int]) -> str
You are given two intervals, where each interval is a pair of integers. For example, interval = (start, end) = (1, 2). The given intervals are closed which means that the interval (start, end) includes both start and end. For each given interval, it is assumed that its start is less or equal its end. Your task is to determine whether the length of intersection of these two intervals is a prime number. Example, the intersection of the intervals (1, 3), (2, 4) is (2, 3) which its length is 1, which not a prime number. If the length of the intersection is a prime number, return "YES", otherwise, return "NO". If the two intervals don't intersect, return "NO".
[{"input": ["(1", "2)", "(2", "3)"], "expected_output": "NO"}, {"input": ["(-1", "1)", "(0", "4)"], "expected_output": "NO"}, {"input": ["(-3", "-1)", "(-5", "5)"], "expected_output": "YES"}]
def intersection(interval1: Tuple[Int, Int], interval2: Tuple[Int, Int]) -> str """You are given two intervals, where each interval is a pair of integers. For example, interval = (start, end) = (1, 2). The given intervals are closed which means that the interval (start, end) includes both start and end. For each given interval, it is assumed that its start is less or equal its end. Your task is to determine whether the length of intersection of these two intervals is a prime number. Example, the intersection of the intervals (1, 3), (2, 4) is (2, 3) which its length is 1, which not a prime number. If the length of the intersection is a prime number, return "YES", otherwise, return "NO". If the two intervals don't intersect, return "NO". """
def problem_spec -- function signature (impl: Int Γ— Int β†’ Int Γ— Int β†’ String) -- inputs (interval1: Int Γ— Int) (interval2: Int Γ— Int) := -- spec let spec (result: String) := let (s1, e1) := interval1; let (s2, e2) := interval2; s1 ≀ e1 β†’ s2 ≀ e2 β†’ let intersectionStart := max s1 s2; let intersectionEnd := min e1 e2; let hasIntersection := intersectionStart ≀ intersectionEnd; let isPrime := Nat.Prime (intersectionEnd - intersectionStart).toNat; (result = "YES" ↔ hasIntersection ∧ isPrime) ∧ (result = "NO" ↔ Β¬hasIntersection ∨ Β¬isPrime) ∧ (result = "YES" ∨ result = "NO") -- program terminates βˆƒ result, impl interval1 interval2 = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: Int Γ— Int β†’ Int Γ— Int β†’ String) -- inputs (interval1: Int Γ— Int) (interval2: Int Γ— Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ interval1 interval2, problem_spec impl interval1 interval2) ↔ (βˆ€ interval1 interval2, generated_spec impl interval1 interval2) :=
sorry
def implementation (interval1: Int Γ— Int) (interval2: Int Γ— Int) : String :=
sorry
-- #test implementation (1, 2) (2, 3) = "NO" -- #test implementation (-1, 1) (0, 4) = "NO" -- #test implementation (-3, -1) (-5, 5) = "YES"
theorem correctness (interval1: Int Γ— Int) (interval2: Int Γ— Int) : problem_spec implementation interval1 interval2 :=
sorry
null
null
null
def prod_signs(arr: List[int]) -> Optional[int]
You are given an array arr of integers and you need to return sum of magnitudes of integers multiplied by product of all signs of each number in the array, represented by 1, -1 or 0. Note: return None for empty arr.
[{"input": [1, 2, 2, -4], "expected_output": -9}, {"input": [0, 1], "expected_output": 0}, {"input": [], "expected_output": "None"}]
def prod_signs(arr: List[int]) -> Optional[int] """You are given an array arr of integers and you need to return sum of magnitudes of integers multiplied by product of all signs of each number in the array, represented by 1, -1 or 0. Note: return None for empty arr. """
def problem_spec -- function signature (impl: List Int β†’ Option Int) -- inputs (arr: List Int) := -- spec let spec (result: Option Int) := match result with | none => arr = [] | some result => let magnitude_sum := (arr.map (fun x => Int.ofNat x.natAbs)).sum; let neg_count_odd := (arr.filter (fun x => x < 0)).length % 2 = 1; let has_zero := 0 ∈ arr; (result < 0 ↔ (neg_count_odd ∧ Β¬has_zero) ∧ result = magnitude_sum * -1) ∧ (0 < result ↔ (Β¬neg_count_odd ∧ Β¬has_zero) ∧ result = magnitude_sum) ∧ (result = 0 ↔ has_zero) -- program terminates βˆƒ result, impl arr = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: List Int β†’ Option Int) -- inputs (arr: List Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ arr, problem_spec impl arr) ↔ (βˆ€ arr, generated_spec impl arr) :=
sorry
def implementation (arr: List Int) : Option Int :=
sorry
-- #test implementation ([1, 2, 2, -4]: List Int) = (-9: Int) -- #test implementation ([0, 1]: List Int) = (0: Int) -- #test implementation ([]: List Int) = none
theorem correctness (arr: List Int) : problem_spec implementation arr :=
sorry
null
null
null
def split_words(txt)
Given a string of words, return a list of words split on whitespace, if no whitespaces exists in the text you should split on commas ',' if no commas exists you should return the number of lower-case letters with odd order in the alphabet, ord('a') = 0, ord('b') = 1, ... ord('z') = 25
[{"input": "Hello world!", "expected_output": ["Hello", "world!"]}, {"input": "Hello,world!", "expected_output": ["Hello", "world!"]}, {"input": "abcdef", "expected_output": 3}]
def split_words(txt) """Given a string of words, return a list of words split on whitespace, if no whitespaces exists in the text you should split on commas ',' if no commas exists you should return the number of lower-case letters with odd order in the alphabet, ord('a') = 0, ord('b') = 1, ... ord('z') = 25 """
def problem_spec -- function signature -- return a tuple of Option (List String) and Option Nat (impl: String β†’ Option (List String) Γ— Option Nat) -- inputs (text: String) := -- spec let spec (result: Option (List String) Γ— Option Nat) := -- both cannot be None let words := result.fst; let ord := result.snd; 0 < text.length β†’ Β¬ (words = none ∧ ord = none) ∧ (words = none ↔ βˆ€ ch, ch ∈ text.toList β†’ (ch = ',' ∨ ch = ' ')) ∧ (βˆ€ num, ord = some num β†’ (text.get! 0).toNat = num) ∧ (βˆ€ lst, words = some lst β†’ βˆ€ i, i < lst.length β†’ let str := lst.get! i; text.containsSubstr str) ∧ (βˆ€ lst, words = some lst β†’ let first := text.takeWhile (fun c => c β‰  ',' ∧ c β‰  ' '); let nextImpl := impl (text.drop (first.length + 1)); let nextWords := nextImpl.fst; (βˆƒ nextLst, nextWords = some nextLst ∧ lst = [first] ++ nextLst)) -- program terminates βˆƒ result, impl text = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: String β†’ Option (List String) Γ— Option Nat) -- inputs (text: String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ text, problem_spec impl text) ↔ (βˆ€ text, generated_spec impl text) :=
sorry
def implementation (text: String) : Option (List String) Γ— Option Nat :=
sorry
-- #test implementation "Hello world!" = (some ["Hello", "world!"], none) -- #test implementation "Hello,world!" = (some ["Hello", "world!"], none) -- #test implementation "abcdef" = (none, some 3)
theorem correctness (text: String) : problem_spec implementation text :=
sorry
null
null
null
def tri(n: int) -> List[int]
Everyone knows Fibonacci sequence, it was studied deeply by mathematicians in the last couple centuries. However, what people don't know is Tribonacci sequence. Tribonacci sequence is defined by the recurrence: tri(1) = 3 tri(n) = 1 + n / 2, if n is even. tri(n) = tri(n - 1) + tri(n - 2) + tri(n + 1), if n is odd. For example: tri(2) = 1 + (2 / 2) = 2 tri(4) = 3 tri(3) = tri(2) + tri(1) + tri(4) = 2 + 3 + 3 = 8 You are given a non-negative integer number n, you have to a return a list of the first n + 1 numbers of the Tribonacci sequence.
[{"input": 3, "expected_output": [1, 3, 2, 8]}]
def tri(n: int) -> List[int] """Everyone knows Fibonacci sequence, it was studied deeply by mathematicians in the last couple centuries. However, what people don't know is Tribonacci sequence. Tribonacci sequence is defined by the recurrence: tri(1) = 3 tri(n) = 1 + n / 2, if n is even. tri(n) = tri(n - 1) + tri(n - 2) + tri(n + 1), if n is odd. For example: tri(2) = 1 + (2 / 2) = 2 tri(4) = 3 tri(3) = tri(2) + tri(1) + tri(4) = 2 + 3 + 3 = 8 You are given a non-negative integer number n, you have to a return a list of the first n + 1 numbers of the Tribonacci sequence. """
def problem_spec -- function signature (impl: Nat β†’ List Int) -- inputs (n: Nat) := -- spec let spec (result: List Int) := 0 < result.length ∧ result.length = n ∧ let i := result.length-1; (i = 0 β†’ result[0]! = 1) ∧ -- base case (i = 1 β†’ result[1]! = 3) ∧ (2 ≀ i ∧ i % 2 = 0 β†’ result[i]! = 1 + i / 2) ∧ (2 ≀ i ∧ i % 2 = 1 β†’ result[i]! = result[i-2]! + result[i-1]! + (1 + (i+1) / 2)) ∧ if i = 0 then true else result.take i = impl (i-1) -- program termination βˆƒ result, impl n = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: Nat β†’ List Int) -- inputs (n: Nat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ n, problem_spec impl n) ↔ (βˆ€ n, generated_spec impl n) :=
sorry
def implementation (n: Nat) : List Int:=
sorry
-- #test implementation 3 = [1, 3, 2, 8]
theorem correctness (n: Nat) : problem_spec implementation n :=
sorry
null
null
null
def digits(n: int) -> int
Given a positive integer n, return the product of the odd digits. Return 0 if all digits are even.
[{"input": 1, "expected_output": 1}, {"input": 4, "expected_output": 0}, {"input": 235, "expected_output": 15}]
def digits(n: int) -> int """Given a positive integer n, return the product of the odd digits. Return 0 if all digits are even. """
def problem_spec -- function signature (impl: Nat β†’ Nat) -- inputs (n: Nat) := -- spec let spec (result: Nat) := 0 < n β†’ (n < 10 β†’ (n % 2 = 1 β†’ result = n) ∧ (n % 2 = 0 β†’ result = 0)) ∧ (10 ≀ n β†’ let digit := n % 10; let rest := n / 10; (digit % 2 = 1 β†’ if (Nat.toDigits 10 rest).all (fun x => Even (x.toNat - '0'.toNat)) then impl rest = 0 ∧ result = digit else result = impl rest * digit) ∧ (digit % 2 = 0 β†’ result = impl rest)) -- program termination βˆƒ result, impl n = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: Nat β†’ Nat) -- inputs (n: Nat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ n, problem_spec impl n) ↔ (βˆ€ n, generated_spec impl n) :=
sorry
def implementation (n: Nat) : Nat :=
sorry
-- #test implementation 1 = 1 -- #test implementation 4 = 0 -- #test implementation 235 = 15
theorem correctness (n: Nat) : problem_spec implementation n :=
sorry
null
null
null
def is_nested(string: str) -> Bool
Create a function that takes a string as input which contains only parentheses. The function should return True if and only if there is a valid subsequence of parentheses where at least one parenthesis in the subsequence is nested.
[{"input": "(())", "expected_output": true}, {"input": "()))))))((((()", "expected_output": false}, {"input": "()()", "expected_output": false}, {"input": "()", "expected_output": false}, {"input": "(()())", "expected_output": true}, {"input": "(())((", "expected_output": true}]
def is_nested(string: str) -> Bool """Create a function that takes a string as input which contains only parentheses. The function should return True if and only if there is a valid subsequence of parentheses where at least one parenthesis in the subsequence is nested. """
def problem_spec -- function signature (impl: String β†’ Bool) -- inputs (string: String) := -- spec let spec (result: Bool) := string.toList.all (fun x => x = '(' ∨ x = ')') β†’ result = true ↔ βˆƒ x : String, is_subsequence x.toList string.toList ∧ balanced_paren_non_computable x '(' ')' ∧ 2 ≀ count_max_paren_depth x -- program termination βˆƒ result, impl string = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: String β†’ Bool) -- inputs (lst: String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ string, problem_spec impl string) ↔ (βˆ€ string, generated_spec impl string) :=
sorry
def implementation (lst: String) : Bool :=
sorry
-- #test implementation "(())" = true -- #test implementation "()))))))((((()" = false -- #test implementation "()()" = false -- #test implementation "()" = false -- #test implementation "(()())" = true -- #test implementation "(())((" = true
theorem correctness (string: String) : problem_spec implementation string :=
sorry
/-- name: string_is_paren_balanced_helper use: | Helper function to check if a string is balanced with respect to parentheses. problems: - 1 - 6 - 132 sample_problems: - 0 -/ def string_is_paren_balanced_helper (paren_string: String) (num_open: Int): Bool := -- Recursively check if the string is balanced if paren_string.isEmpty then num_open = 0 else let c := paren_string.get! 0 if c == '(' then string_is_paren_balanced_helper (paren_string.drop 1) (num_open + 1) else if c == ')' then string_is_paren_balanced_helper (paren_string.drop 1) (num_open - 1) else string_is_paren_balanced_helper (paren_string.drop 1) num_open termination_by paren_string.length decreasing_by all_goals { rename_i h_non_empty_string rw [String.drop_eq, String.length] simp rw [String.isEmpty_iff] at h_non_empty_string by_cases h_paren_nil : paren_string.length ≀ 0 rw [Nat.le_zero_eq] at h_paren_nil rw [←string_eq_iff_data_eq] at h_non_empty_string have h_temp : "".data = [] := by simp rw [h_temp] at h_non_empty_string rw [String.length] at h_paren_nil rw [List.length_eq_zero] at h_paren_nil contradiction have h_temp : paren_string.length > 0 := by linarith assumption } /-- name: string_is_paren_balanced use: | Function to check if a string is balanced with respect to parentheses. problems: - 1 - 6 - 132 sample_problems: - 0 -/ def string_is_paren_balanced (paren_string: String): Bool := string_is_paren_balanced_helper paren_string 0 /-- name: count_max_paren_depth_helper use: | Helper to count the maximum depth of parentheses in a string. problems: - 6 - 132 -/ def count_max_paren_depth_helper (paren_string: String) (num_open: Int) (max_depth: Nat): Nat := -- Recursively count the maximum depth of parentheses if paren_string.isEmpty then max_depth else let c := paren_string.get! 0 if c == '(' then let new_num_open := num_open + 1 count_max_paren_depth_helper (paren_string.drop 1) (new_num_open) (max_depth.max new_num_open.toNat) else if c == ')' then count_max_paren_depth_helper (paren_string.drop 1) (num_open - 1) max_depth else count_max_paren_depth_helper (paren_string.drop 1) num_open max_depth termination_by paren_string.length decreasing_by all_goals { rename_i h_non_empty_string rw [String.drop_eq, String.length] simp rw [String.isEmpty_iff] at h_non_empty_string by_cases h_paren_nil : paren_string.length ≀ 0 rw [Nat.le_zero_eq] at h_paren_nil rw [←string_eq_iff_data_eq] at h_non_empty_string have h_temp : "".data = [] := by simp rw [h_temp] at h_non_empty_string rw [String.length] at h_paren_nil rw [List.length_eq_zero] at h_paren_nil contradiction have h_temp : paren_string.length > 0 := by linarith assumption } /-- name: count_max_paren_depth use: | Function to count the maximum depth of parentheses in a string. problems: - 6 - 132 -/ def count_max_paren_depth (paren_string: String): Nat := count_max_paren_depth_helper paren_string 0 0 /-- name: is_subsequence use: | Helper to check if List Char xs is a subsequence of List Char ys. problems: - 132 -/ def is_subsequence (xs ys : List Char) : Bool := match xs, ys with | [], _ => true | _, [] => false | x::xs', y::ys' => if x = y then is_subsequence xs' ys' else is_subsequence xs ys'
null
null
def sum_squares(lst: List[float]) -> int
You are given a list of numbers. You need to return the sum of squared numbers in the given list, round each element in the list to the upper int(Ceiling) first.
[{"input": [1, 2, 3], "expected_output": 14}, {"input": [1, 4, 9], "expected_output": 98}, {"input": [1, 3, 5, 7], "expected_output": 84}, {"input": [1.4, 4.2, 0], "expected_output": 29}, {"input": [-2.4, 1, 1], "expected_output": 6}]
def sum_squares(lst: List[float]) -> int """You are given a list of numbers. You need to return the sum of squared numbers in the given list, round each element in the list to the upper int(Ceiling) first. """
def problem_spec -- function signature (impl: List Rat β†’ Int) -- inputs (lst: List Rat) := -- spec let spec (result: Int) := (lst = [] β†’ result = 0) ∧ (lst != [] β†’ 0 ≀ result - lst[0]!.ceil^2 ∧ (impl (lst.drop 1) = (result - lst[0]!.ceil^2))) -- program termination βˆƒ result, impl lst = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: List Rat β†’ Int) -- inputs (lst: List Rat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ lst, problem_spec impl lst) ↔ (βˆ€ lst, generated_spec impl lst) :=
sorry
def implementation (lst: List Rat) : Int :=
sorry
-- #test implementation [1, 2, 3] = 14 -- #test implementation [1, 4, 9] = 98 -- #test implementation [1, 3, 5, 7] = 84 -- #test implementation [1.4, 4.2, 0] = 29 -- #test implementation [-2.4, 1, 1] = 6
theorem correctness (lst: List Rat) : problem_spec implementation lst :=
sorry
null
null
null
def check_if_last_char_is_a_letter(txt: str) -> Bool
Create a function that returns True if the last character of a given string is an alphabetical character and is not a part of a word, and False otherwise. Note: "word" is a group of characters separated by space.
[{"input": "apple pie", "expected_output": false}, {"input": "apple pi e", "expected_output": true}, {"input": "apple pi e ", "expected_output": false}, {"input": "", "expected_output": false}]
def check_if_last_char_is_a_letter(txt: str) -> Bool """Create a function that returns True if the last character of a given string is an alphabetical character and is not a part of a word, and False otherwise. Note: "word" is a group of characters separated by space. """
def problem_spec -- function signature (impl: String β†’ Bool) -- inputs (txt: String) := -- spec let spec (result: Bool) := let words := txt.splitOn " "; match words with | [] => result = False | [last_word] => (result ↔ last_word.length = 1 ∧ (let diff := (last_word.get 0).toLower.toNat - 'a'.toNat; 0 ≀ diff ∧ diff ≀ 25)) | head::tail => result ↔ (let tail_txt := String.join tail; impl tail_txt); -- program terminates βˆƒ result, impl txt = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: String β†’ Bool) -- inputs (txt: String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ txt, problem_spec impl txt) ↔ (βˆ€ txt, generated_spec impl txt) :=
sorry
def implementation (txt: String) : Bool :=
sorry
-- #test implementation "apple pie" = false -- #test implementation "apple pi e" = true -- #test implementation "apple pi e " = false -- #test implementation "" = false
theorem correctness (txt: String) : problem_spec implementation txt :=
sorry
null
null
null
def can_arrange(arr: List[int]) -> int
Create a function which returns the largest index of an element which is not greater than or equal to the element immediately preceding it. If no such element exists then return -1. The given array will not contain duplicate values.
[{"input": [1, 2, 4, 3, 5], "expected_output": 3}, {"input": [1, 2, 3], "expected_output": -1}]
def can_arrange(arr: List[int]) -> int """Create a function which returns the largest index of an element which is not greater than or equal to the element immediately preceding it. If no such element exists then return -1. The given array will not contain duplicate values. """
def problem_spec -- function signature (impl: List Int β†’ Int) -- inputs (arr: List Int) := -- spec let spec (result: Int) := Β¬arr.any (fun x => 1 < arr.count x) β†’ (arr.length = 0 ∨ arr.length = 1 β†’ result = -1) ∧ (1 < arr.length β†’ let last := arr.length-1; let i := if arr[last]! < arr[last-1]! then Int.ofNat last else -1; result = max (impl (arr.take last)) i); -- program termination βˆƒ result, impl arr = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: List Int β†’ Int) -- inputs (arr: List Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ arr, problem_spec impl arr) ↔ (βˆ€ arr, generated_spec impl arr) :=
sorry
def implementation (arr: List Int) : Int :=
sorry
-- #test implementation [1, 2, 4, 3, 5] = 3 -- #test implementation [1, 2, 3] = -1
theorem correctness (arr: List Int) : problem_spec implementation arr :=
sorry
null
null
null
def largest_smallest_integers(lst: List[int]) -> Tuple[ Optional[Int], Optional[Int] ]
Create a function that returns a tuple (a, b), where 'a' is the largest of negative integers, and 'b' is the smallest of positive integers in a list. If there is no negative or positive integers, return them as None.
[{"input": [2, 4, 1, 3, 5, 7], "expected_output": "(None, 1)"}, {"input": [], "expected_output": "(None, None)"}, {"input": [0], "expected_output": "(None, None)"}]
def largest_smallest_integers(lst: List[int]) -> Tuple[ Optional[Int], Optional[Int] ] """Create a function that returns a tuple (a, b), where 'a' is the largest of negative integers, and 'b' is the smallest of positive integers in a list. If there is no negative or positive integers, return them as None. """
def problem_spec -- function signature (impl: List Int β†’ Option Int Γ— Option Int) -- inputs (lst: List Int) := -- spec let spec (result: Option Int Γ— Option Int) := let (a, b) := result; (match a with | none => Β¬(βˆƒ i, i ∈ lst ∧ i < 0) | some a => a < 0 ∧ a ∈ lst ∧ βˆ€ i, i ∈ lst ∧ i < 0 β†’ i ≀ a) ∧ (match b with | none => Β¬(βˆƒ i, i ∈ lst ∧ 0 < i) | some b => 0 < b ∧ b ∈ lst ∧ βˆ€ i, i ∈ lst ∧ 0 < i β†’ b ≀ i) -- program termination βˆƒ result, impl lst = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: List Int β†’ Option Int Γ— Option Int) -- inputs (lst: List Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ lst, problem_spec impl lst) ↔ (βˆ€ lst, generated_spec impl lst) :=
sorry
def implementation (lst: List Int) : (Option Int Γ— Option Int) :=
sorry
-- #test implementation [2, 4, 1, 3, 5, 7] = (none, some 1) -- #test implementation [] = (none, none) -- #test implementation [0] = (none, none)
theorem correctness (lst: List Int) : problem_spec implementation lst :=
sorry
null
null
null
def is_equal_to_sum_even(n: int) -> Bool
Evaluate whether the given number n can be written as the sum of exactly 4 positive even numbers
[{"input": 4, "expected_output": false}, {"input": 6, "expected_output": false}, {"input": 8, "expected_output": true}]
def is_equal_to_sum_even(n: int) -> Bool """Evaluate whether the given number n can be written as the sum of exactly 4 positive even numbers """
def problem_spec -- function signature (impl: Int β†’ Bool) -- inputs (n: Int) := -- spec let spec (result: Bool) := let sum_exists := βˆƒ a b c d : Nat, Even a ∧ Even b ∧ Even c ∧ Even d ∧ (a + b + c + d = n); result = true ↔ sum_exists -- program termination βˆƒ result, impl n = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: Int β†’ Bool) -- inputs (n: Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ n, problem_spec impl n) ↔ (βˆ€ n, generated_spec impl n) :=
sorry
def implementation (n: Int) : Bool :=
sorry
-- #test implementation 4 = false -- #test implementation 6 = false -- #test implementation 8 = true
theorem correctness (n: Int) : problem_spec implementation n :=
sorry
null
null
null
def special_factorial(n: int) -> int
The Brazilian factorial is defined as: brazilian_factorial(n) = n! * (n-1)! * (n-2)! * ... * 1! where n > 0. Please write a function that computes the Brazilian factorial.
[{"input": 4, "expected_output": 288}]
def special_factorial(n: int) -> int """The Brazilian factorial is defined as: brazilian_factorial(n) = n! * (n-1)! * (n-2)! * ... * 1! where n > 0. Please write a function that computes the Brazilian factorial. """
def problem_spec -- function signature (impl: Nat β†’ Nat) -- inputs (n: Nat) := -- spec let spec (result: Nat) := let factorial := Nat.factorial n; (0 < n β†’ result / factorial = impl (n - 1)) ∧ (n = 0 β†’ result = 1); -- program termination βˆƒ result, impl n = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: Nat β†’ Nat) -- inputs (n: Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ n, problem_spec impl n) ↔ (βˆ€ n, generated_spec impl n) :=
sorry
def implementation (n: Nat) : Nat :=
sorry
-- #test implementation 4 = 288
theorem correctness (n: Nat) : problem_spec implementation n :=
sorry
null
null
null
def fix_spaces(text: str) -> str
Given a string text, replace all spaces in it with underscores, and if a string has more than 2 consecutive spaces, then replace all consecutive spaces with -
[{"input": "Example", "expected_output": "Example"}, {"input": "Example 1", "expected_output": "Example_1"}, {"input": " Example 2", "expected_output": "_Example_2"}, {"input": " Example 3", "expected_output": "_Example-3"}]
def fix_spaces(text: str) -> str """Given a string text, replace all spaces in it with underscores, and if a string has more than 2 consecutive spaces, then replace all consecutive spaces with - """
def problem_spec -- function signature (impl: String β†’ String) -- inputs (text: String) := -- spec let spec (result: String) := (result = "" β†’ text = "") ∧ (result β‰  "" β†’ ( (βˆƒ pref s, text = pref ++ s ∧ pref.length = 1 ∧ pref β‰  " " ∧ result = pref ++ impl s) ∨ (βˆƒ pref s : String, text = pref ++ s ∧ pref β‰  "" ∧ (βˆ€ ch, ch ∈ pref.toList β†’ ch = ' ') ∧ let k := pref.length; (k ≀ 2 β†’ result = (String.replicate k '_') ++ (impl (text.drop k))) ∧ (2 < k β†’ result = "-" ++ (impl (text.drop k)))) ) ) -- program termination βˆƒ result, impl text = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: String β†’ String) -- inputs (text: String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ text, problem_spec impl text) ↔ (βˆ€ text, generated_spec impl text) :=
sorry
def implementation (text: String) : String :=
sorry
-- #test implementation "Example" = "Example" -- #test implementation "Example 1" = "Example_1" -- #test implementation " Example 2" = "_Example_2" -- #test implementation " Example 3" = "_Example-3"
theorem correctness (text: String) : problem_spec implementation text :=
sorry
null
null
null
def file_name_check(file_name: str) -> str
Create a function which takes a string representing a file's name, and returns 'Yes' if the the file's name is valid, and returns 'No' otherwise. A file's name is considered to be valid if and only if all the following conditions are met: - There should not be more than three digits ('0'-'9') in the file's name. - The file's name contains exactly one dot '.' - The substring before the dot should not be empty, and it starts with a letter from the latin alphapet ('a'-'z' and 'A'-'Z'). - The substring after the dot should be one of these: ['txt', 'exe', 'dll']
[{"input": "example.txt", "expected_output": "Yes"}, {"input": "1example.dll", "expected_output": "No"}]
def file_name_check(file_name: str) -> str """Create a function which takes a string representing a file's name, and returns 'Yes' if the the file's name is valid, and returns 'No' otherwise. A file's name is considered to be valid if and only if all the following conditions are met: - There should not be more than three digits ('0'-'9') in the file's name. - The file's name contains exactly one dot '.' - The substring before the dot should not be empty, and it starts with a letter from the latin alphapet ('a'-'z' and 'A'-'Z'). - The substring after the dot should be one of these: ['txt', 'exe', 'dll'] """
def problem_spec -- function signature (impl: String β†’ String) -- inputs (file_name : String) := -- spec let spec (result: String) := let valid := (file_name.toList.filter Char.isDigit).length ≀ 3 ∧ (file_name.toList.filter (Β· = '.')).length = 1 ∧ βˆƒ before after : String, file_name = before ++ "." ++ after ∧ before != "" ∧ Char.isAlpha (before.get! 0) ∧ (after = "txt" ∨ after = "exe" ∨ after = "dll") (result = "Yes" ↔ valid) ∧ (result = "No" ↔ Β¬valid) -- program termination βˆƒ result, impl file_name = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: String β†’ String) -- inputs (file_name: String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ file_name, problem_spec impl file_name) ↔ (βˆ€ file_name, generated_spec impl file_name) :=
sorry
def implementation (file_name : String) : String :=
sorry
-- #test implementation "example.txt" = "Yes" -- #test implementation "1example.dll" = "No"
theorem correctness (file_name : String) : problem_spec implementation file_name :=
sorry
null
null
null
def sum_squares(lst: List[int]) -> int
This function will take a list of integers. For all entries in the list, the function shall square the integer entry if its index is a multiple of 3 and will cube the integer entry if its index is a multiple of 4 and not a multiple of 3. The function will not change the entries in the list whose indexes are not a multiple of 3 or 4. The function shall then return the sum of all entries.
[{"input": [1, 2, 3], "expected_output": 6}, {"input": [], "expected_output": 0}, {"input": [-1, -5, 2, -1, -5], "expected_output": -126}]
def sum_squares(lst: List[int]) -> int """This function will take a list of integers. For all entries in the list, the function shall square the integer entry if its index is a multiple of 3 and will cube the integer entry if its index is a multiple of 4 and not a multiple of 3. The function will not change the entries in the list whose indexes are not a multiple of 3 or 4. The function shall then return the sum of all entries. """
def problem_spec -- function signature (impl: List Int β†’ Int) -- inputs (lst : List Int) := -- spec let spec (result : Int) := let last := lst.length-1; (lst = [] β†’ result = 0) ∧ (lst β‰  [] ∧ last % 3 = 0 β†’ result = lst[last]! ^ 2 + impl (lst.take last)) ∧ (lst β‰  [] ∧ last % 4 = 0 ∧ last % 3 != 0 β†’ result = lst[last]! ^ 3 + impl (lst.take last)) ∧ (lst β‰  [] ∧ last % 3 != 0 ∧ last % 4 != 0 β†’ result = lst[last]! + impl (lst.take last)) -- program termination βˆƒ result, impl lst = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: List Int β†’ Int) -- inputs (lst : List Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ lst, problem_spec impl lst) ↔ (βˆ€ lst, generated_spec impl lst) :=
sorry
def implementation (lst : List Int) : Int :=
sorry
-- #test implementation [1, 2, 3] = 6 -- #test implementation [] = 0 -- #test implementation [-1, -5, 2, -1, -5] = -126
theorem correctness (lst : List Int) : problem_spec implementation lst :=
sorry
null
null
null
def words_in_sentence(sentence: str) -> str
You are given a string representing a sentence, the sentence contains some words separated by a space, and you have to return a string that contains the words from the original sentence, whose lengths are prime numbers, the order of the words in the new string should be the same as the original one. Constraints: * 1 <= len(sentence) <= 100 * sentence contains only letters
[{"input": "This is a test", "expected_output": "is"}, {"input": "lets go for swimming", "expected_output": "go for"}]
def words_in_sentence(sentence: str) -> str """You are given a string representing a sentence, the sentence contains some words separated by a space, and you have to return a string that contains the words from the original sentence, whose lengths are prime numbers, the order of the words in the new string should be the same as the original one. Constraints: * 1 <= len(sentence) <= 100 * sentence contains only letters """
def problem_spec -- function signature (impl: String β†’ String) -- inputs (sentence: String) := -- spec let spec (result: String) := let words := sentence.splitOn; let result_words := result.splitOn; 1 ≀ sentence.length β†’ sentence.length ≀ 100 β†’ sentence.all (fun x => Char.isAlpha x) β†’ result_words.length ≀ words.length ∧ (βˆ€ word ∈ result_words, word ∈ words ∧ Nat.Prime word.length) ∧ match result_words with | [] => βˆ€ word ∈ words, Β¬(Nat.Prime word.length) | head :: tail => if Nat.Prime head.length ∧ head = words[0]! then String.join tail = impl (String.join (words.drop 1)) else result = impl (String.join (words.drop 1)) -- program termination βˆƒ result, impl sentence = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: String β†’ String) -- inputs (sentence: String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ sentence, problem_spec impl sentence) ↔ (βˆ€ sentence, generated_spec impl sentence) :=
sorry
def implementation (sentence : String) : String :=
sorry
-- #test implementation "This is a test" = "is" -- #test implementation "lets go for swimming" = "go for"
theorem correctness (sentence : String) : problem_spec implementation sentence :=
sorry
null
null
null
def simplify(x: str, n: str) -> Bool
Your task is to implement a function that will simplify the expression x * n. The function returns True if x * n evaluates to a whole number and False otherwise. Both x and n, are string representation of a fraction, and have the following format, <numerator>/<denominator> where both numerator and denominator are positive whole numbers. You can assume that x, and n are valid fractions, and do not have zero as denominator.
[{"input": ["1/5", "5/1"], "expected_output": true}, {"input": ["1/6", "2/1"], "expected_output": false}, {"input": ["7/10", "10/2"], "expected_output": false}]
def simplify(x: str, n: str) -> Bool """Your task is to implement a function that will simplify the expression x * n. The function returns True if x * n evaluates to a whole number and False otherwise. Both x and n, are string representation of a fraction, and have the following format, <numerator>/<denominator> where both numerator and denominator are positive whole numbers. You can assume that x, and n are valid fractions, and do not have zero as denominator. """
def problem_spec -- function signature (impl: String β†’ String β†’ Bool) -- inputs (x: String) (n: String) := -- spec let spec (result: Bool) := let fx := x.splitOn "/"; let fn := n.splitOn "/"; fx.length = 2 β†’ fn.length = 2 β†’ fx.all String.isNat β†’ fn.all String.isNat β†’ let p1 := fx[0]!.toNat!; let q1 := fx[1]!.toNat!; let p2 := fn[0]!.toNat!; let q2 := fn[1]!.toNat!; q1 β‰  0 β†’ q2 β‰  0 β†’ (result ↔ (βˆƒ k, k * p1 * p2 = q1 * q2)) -- program termination βˆƒ result, impl x n = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: String β†’ String β†’ Bool) -- inputs (x: String) (n: String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ x n, problem_spec impl x n) ↔ (βˆ€ x n, generated_spec impl x n) :=
sorry
def implementation (x: String) (n: String) : Bool :=
sorry
-- #test implementation "1/5" "5/1" = True -- #test implementation "1/6" "2/1" = False -- #test implementation "7/10" "10/2" = False
theorem correctness (x: String) (n: String) : problem_spec implementation x n :=
sorry
null
null
null
def order_by_points(nums: List[int]) -> List[int]
Write a function which sorts the given list of integers in ascending order according to the sum of their digits. Note: if there are several items with similar sum of their digits, order them based on their index in original list.
[{"input": [1, 11, -1, -11, -12], "expected_output": [-1, -11, 1, -12, 11]}, {"input": [], "expected_output": []}]
def order_by_points(nums: List[int]) -> List[int] """Write a function which sorts the given list of integers in ascending order according to the sum of their digits. Note: if there are several items with similar sum of their digits, order them based on their index in original list. """
def problem_spec -- function signature (impl: List Int β†’ List Int) -- inputs (nums: List Int) := -- spec let spec (result: List Int) := List.Perm nums result ∧ match result with | [] => nums = [] | head::tail => let head_sum := digit_sum head; (βˆ€ num ∈ nums, let sum := digit_sum num; sum > head_sum ∨ (sum = head_sum ∧ nums.indexOf num β‰₯ nums.indexOf head)) ∧ impl (nums.erase head) = tail -- program termination βˆƒ result, impl nums = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: List Int β†’ List Int) -- inputs (nums: List Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ nums, problem_spec impl nums) ↔ (βˆ€ nums, generated_spec impl nums) :=
sorry
def implementation (nums: List Int) : List Int :=
sorry
-- #test implementation [1, 11, -1, -11, -12] = [-1, -11, 1, -12, 11] -- #test implementation [] = []
theorem correctness (nums: List Int) : problem_spec implementation nums :=
sorry
/-- name: digit_sum use: | Helper to sum the digits of a number. If the number is negative, the negative sign is treated as part of the first digit. problems: - 145 -/ def digit_sum (n : Int) : Int := let ds := (toString n.natAbs).toList.map fun c => c.toNat - Char.toNat '0' match ds with | [] => 0 | d :: ds' => let tail := ds'.foldl (Β· + Β·) 0 if n < 0 then Int.ofNat tail - Int.ofNat d else Int.ofNat (d + tail)
null
null
def specialFilter(nums: List[int]) -> int
Write a function that takes an array of numbers as input and returns the number of elements in the array that are greater than 10 and both first and last digits of a number are odd (1, 3, 5, 7, 9).
[{"input": [15, -73, 14, -15], "expected_output": 1}, {"input": [33, -2, -3, 45, 21, 109], "expected_output": 2}]
def specialFilter(nums: List[int]) -> int """Write a function that takes an array of numbers as input and returns the number of elements in the array that are greater than 10 and both first and last digits of a number are odd (1, 3, 5, 7, 9). """
def problem_spec -- function signature (impl: List Int β†’ Int) -- inputs (nums: List Int) := -- spec let spec (result: Int) := match nums with | [] => result = 0 | head::tail => let first_digit := (toString head.natAbs).toList[0]!.toNat - Char.toNat '0'; let last_digit := head % 10; let valid := head > 10 ∧ Odd first_digit ∧ Odd last_digit if valid then result = 1 + impl tail else result = impl tail -- program termination βˆƒ result, impl nums = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: List Int β†’ Int) -- inputs (nums: List Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ nums, problem_spec impl nums) ↔ (βˆ€ nums, generated_spec impl nums) :=
sorry
def implementation (nums: List Int) : Int :=
sorry
-- #test implementation [15, -73, 14, -15] = 1 -- #test implementation [33, -2, -3, 45, 21, 109] = 2
theorem correctness (nums: List Int) : problem_spec implementation nums :=
sorry
null
null
null
def get_max_triples(n: int) -> int
You are given a positive integer n. You have to create an integer array a of length n. For each i (1 ≀ i ≀ n), the value of a[i] = i * i - i + 1. Return the number of triples (a[i], a[j], a[k]) of a where i < j < k, and a[i] + a[j] + a[k] is a multiple of 3.
[{"input": 5, "expected_output": 1}]
def get_max_triples(n: int) -> int """You are given a positive integer n. You have to create an integer array a of length n. For each i (1 ≀ i ≀ n), the value of a[i] = i * i - i + 1. Return the number of triples (a[i], a[j], a[k]) of a where i < j < k, and a[i] + a[j] + a[k] is a multiple of 3. """
def problem_spec -- function signature (impl: Nat β†’ Nat) -- inputs (n: Nat) := -- spec let spec (result: Nat) := βˆƒ (S : Finset (Nat Γ— Nat Γ— Nat)), S.card = result ∧ βˆ€ (triple: Nat Γ— Nat Γ— Nat), let (i, j, k) := triple; let a_i := i * i - i + 1; let a_j := j * j - j + 1; let a_k := k * k - k + 1; (1 ≀ i ∧ i < j ∧ j < k ∧ k ≀ n ∧ (a_i + a_j + a_k) % 3 = 0) ↔ triple ∈ S -- program termination βˆƒ result, impl n = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: Nat β†’ Nat) -- inputs (nums: Nat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ n, problem_spec impl n) ↔ (βˆ€ n, generated_spec impl n) :=
sorry
def implementation (n: Nat) : Nat :=
sorry
-- #test implementation 5 = 1
theorem correctness (n: Nat) : problem_spec implementation n :=
sorry
null
null
null
def bf(planet1: str, planet2: str) -> List[str]
There are eight planets in our solar system: the closest to the Sun is Mercury, the next one is Venus, then Earth, Mars, Jupiter, Saturn, Uranus, Neptune. Write a function that takes two planet names as strings planet1 and planet2. The function should return a tuple containing all planets whose orbits are located between the orbit of planet1 and the orbit of planet2, sorted by the proximity to the sun. The function should return an empty tuple if planet1 or planet2 are not correct planet names.
[{"input": "(\"Jupiter\", \"Neptune\")", "expected_output": "(\"Saturn\", \"Uranus\")"}, {"input": "(\"Earth\", \"Mercury\")", "expected_output": "(\"Venus\")"}, {"input": "(\"Mercury\", \"Uranus\")", "expected_output": "(\"Venus\", \"Earth\", \"Mars\", \"Jupiter\", \"Saturn\")"}]
def bf(planet1: str, planet2: str) -> List[str] """There are eight planets in our solar system: the closest to the Sun is Mercury, the next one is Venus, then Earth, Mars, Jupiter, Saturn, Uranus, Neptune. Write a function that takes two planet names as strings planet1 and planet2. The function should return a tuple containing all planets whose orbits are located between the orbit of planet1 and the orbit of planet2, sorted by the proximity to the sun. The function should return an empty tuple if planet1 or planet2 are not correct planet names. """
def problem_spec -- function signature (impl: String β†’ String β†’ List String) -- inputs (planet1: String) (planet2: String) := -- spec let spec (result: List String) := let planets := ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"]; if planet1 βˆ‰ planets ∨ planet2 βˆ‰ planets then result = [] else let index1 := planets.indexOf planet1; let index2 := planets.indexOf planet2; let minIdx := if index1 < index2 then index1 else index2; let maxIdx := if index1 < index2 then index2 else index1; (βˆ€ str ∈ result, str ∈ planets) ∧ (βˆ€ planet ∈ planets, planet ∈ result ↔ planets.indexOf planet < maxIdx ∧ minIdx < planets.indexOf planet) ∧ result.Sorted (fun a b => planets.indexOf a < planets.indexOf b) -- program termination βˆƒ result, impl planet1 planet2 = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: String β†’ String β†’ List String) -- inputs (planet1: String) (planet2: String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ planet1 planet2, problem_spec impl planet1 planet2) ↔ (βˆ€ planet1 planet2, generated_spec impl planet1 planet2) :=
sorry
def implementation (planet1: String) (planet2: String) : List String :=
sorry
-- #test implementation "Jupiter" "Neptune" = ["Saturn", "Uranus"] -- #test implementation "Earth" "Mercury" = ["Venus"] -- #test implementation "Mercury" "Uranus" = ["Venus", "Earth", "Mars", "Jupiter", "Saturn"]
theorem correctness (planet1: String) (planet2: String) : problem_spec implementation planet1 planet2 :=
sorry
null
null
null
def sorted_list_sum(lst: List[str]) -> List[str]
Write a function that accepts a list of strings as a parameter, deletes the strings that have odd lengths from it, and returns the resulted list with a sorted order, The list is always a list of strings and never an array of numbers, and it may contain duplicates. The order of the list should be ascending by length of each word, and you should return the list sorted by that rule. If two words have the same length, sort the list alphabetically. The function should return a list of strings in sorted order. You may assume that all words will have the same length.
[{"input": ["aa", "a", "aaa"], "output": ["aa"]}, {"input": ["ab", "a", "aaa", "cd"], "output": ["ab", "cd"]}]
def sorted_list_sum(lst: List[str]) -> List[str] """Write a function that accepts a list of strings as a parameter, deletes the strings that have odd lengths from it, and returns the resulted list with a sorted order, The list is always a list of strings and never an array of numbers, and it may contain duplicates. The order of the list should be ascending by length of each word, and you should return the list sorted by that rule. If two words have the same length, sort the list alphabetically. The function should return a list of strings in sorted order. You may assume that all words will have the same length. """
def problem_spec -- function signature (impl: List String β†’ List String) -- inputs (lst: List String) := -- spec let spec (result: List String) := match result with | [] => βˆ€ str ∈ lst, Odd str.length | head::tail => Even head.length ∧ (βˆ€ str ∈ lst, Odd str.length ∨ str.length > head.length ∨ str.length = head.length ∧ str β‰₯ head) ∧ impl (lst.erase head) = tail -- program termination βˆƒ result, impl lst = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: List String β†’ List String) -- inputs (lst: List String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ lst, problem_spec impl lst) ↔ (βˆ€ lst, generated_spec impl lst) :=
sorry
def implementation (lst: List String) : List String :=
sorry
-- #test implementation ["aa", "a", "aaa"] = ["aa"] -- #test implementation ["ab", "a", "aaa", "cd"] = ["ab", "cd"]
theorem correctness (lst: List String) : problem_spec implementation lst :=
sorry
null
null
null
def x_or_y(int n, int x, int y) -> int
A simple program which should return the value of x if n is a prime number and should return the value of y otherwise.
[{"input": [7, 34, 12], "expected_output": 34}, {"input": [15, 8, 5], "expected_output": 5}]
def x_or_y(int n, int x, int y) -> int """A simple program which should return the value of x if n is a prime number and should return the value of y otherwise. """
def problem_spec -- function signature (impl: Int β†’ Int β†’ Int β†’ Int) -- inputs (n x y: Int) := -- spec let spec (result: Int) := (result = x ↔ Nat.Prime n.toNat) ∧ (result = y ↔ (Β¬ Nat.Prime n.toNat ∨ n ≀ 1)) -- program terminates βˆƒ result, impl n x y = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: Int β†’ Int β†’ Int β†’ Int) -- inputs (n x y: Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ n x y, problem_spec impl n x y) ↔ (βˆ€ n x y, generated_spec impl n x y) :=
sorry
def implementation (n x y: Int) : Int :=
sorry
-- #test implementation 7 34 12 = 34 -- #test implementation 15 8 5 = 5
theorem correctness (n x y: Int) : problem_spec implementation n x y :=
sorry
null
null
null
def double_the_difference(numbers: List[float]) -> Int
Given a list of numbers, return the sum of squares of the numbers in the list that are odd. Ignore numbers that are negative or not integers.
[{"input": [1, 3, 2, 0], "expected_output": 10}, {"input": ["-1. -2", 0], "expected_output": 0}, {"input": [9, -2], "expected_output": 81}, {"input": [0], "expected_output": 0}]
def double_the_difference(numbers: List[float]) -> Int """Given a list of numbers, return the sum of squares of the numbers in the list that are odd. Ignore numbers that are negative or not integers. """
def problem_spec -- function signature (impl: List Rat β†’ Int) -- inputs (numbers: List Rat) := let isEven (n : Rat) := n % 2 = 0; let isNegative (n : Rat) := n < 0; let isNotInteger (n : Rat) := Β¬ n.isInt; -- spec let spec (result: Int) := 0 < numbers.length β†’ 0 ≀ result ∧ if numbers.length = 1 then result = if (isEven numbers[0]! ∨ isNegative numbers[0]! ∨ isNotInteger numbers[0]!) then (0 : Int) else numbers[0]!.floor ^ 2 else result = if (isEven numbers[0]! ∨ isNegative numbers[0]! ∨ isNotInteger numbers[0]!) then (0 : Int) else numbers[0]!.floor ^ 2 + impl numbers.tail -- program terminates βˆƒ result, impl numbers = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: List Rat β†’ Int) -- inputs (numbers: List Rat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ numbers, problem_spec impl numbers) ↔ (βˆ€ numbers, generated_spec impl numbers) :=
sorry
def implementation (numbers: List Rat) : Int :=
sorry
-- #test implementation ([1, 3, 2, 0]: List Rat) = (10: Int) -- #test implementation ([-1, -2, 0]: List Int) = (0: Int) -- #test implementation ([9, -2]: List Int) = 81 -- #test implementation ([0]: List Int) = 0
theorem correctness (numbers: List Rat) : problem_spec implementation numbers :=
sorry
null
null
null
def compare(scores: List float, guesses: List float) -> List [float]
I think we all remember that feeling when the result of some long-awaited event is finally known. The feelings and thoughts you have at that moment are definitely worth noting down and comparing. Your task is to determine if a person correctly guessed the results of a number of matches. You are given two arrays of scores and guesses of equal length, where each index shows a match. Return an array of the same length denoting how far off each guess was. If they have guessed correctly, the value is 0, and if not, the value is the absolute difference between the guess and the score. Note: to reviewer, the reason for not using |.| to get the absolute value is to avoid leaking the implementation.
[{"input": [[1, 2, 3, 4, 5, 1], [1, 2, 3, 4, 2, -2]], "expected_output": [0, 0, 0, 0, 3, 3]}, {"input": [[0, 5, 0, 0, 0, 4], [4, 1, 1, 0, 0, -2]], "expected_output": [4, 4, 1, 0, 0, 6]}]
def compare(scores: List float, guesses: List float) -> List [float] """I think we all remember that feeling when the result of some long-awaited event is finally known. The feelings and thoughts you have at that moment are definitely worth noting down and comparing. Your task is to determine if a person correctly guessed the results of a number of matches. You are given two arrays of scores and guesses of equal length, where each index shows a match. Return an array of the same length denoting how far off each guess was. If they have guessed correctly, the value is 0, and if not, the value is the absolute difference between the guess and the score. Note: to reviewer, the reason for not using |.| to get the absolute value is to avoid leaking the implementation. """
def problem_spec -- function signature (impl: List Rat β†’ List Rat β†’ List Rat) -- inputs (scores guesses: List Rat) := -- spec let spec (result: List Rat) := result.length = scores.length ∧ scores.length = guesses.length ∧ βˆ€ i, i < scores.length β†’ if scores[i]! > guesses[i]! then result[i]! + guesses[i]! = scores[i]! else result[i]! + scores[i]! = guesses[i]! -- program terminates βˆƒ result, impl scores guesses = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: List Rat β†’ List Rat β†’ List Rat) -- inputs (scores guesses: List Rat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ scores guesses, problem_spec impl scores guesses) ↔ (βˆ€ scores guesses, generated_spec impl scores guesses) :=
sorry
def implementation (scores guesses: List Rat) : List Rat :=
sorry
-- #test implementation [1,2,3,4,5,1] [1,2,3,4,2,-2] = [0,0,0,0,3,3] -- #test implementation [0,5,0,0,0,4] [4,1,1,0,0,-2] = [4,4,1,0,0,6]
theorem correctness (scores guesses: List Rat) : problem_spec implementation scores guesses :=
sorry
null
null
null
def Strongest_Extension(class_name: String, extensions: List[String]) -> String
You will be given the name of a class (a string) and a list of extensions. The extensions are to be used to load additional classes to the class. The strength of the extension is as follows: Let CAP be the number of the uppercase letters in the extension's name, and let SM be the number of lowercase letters in the extension's name, the strength is given by the fraction CAP - SM. You should find the strongest extension and return a string in this format: ClassName.StrongestExtensionName. If there are two or more extensions with the same strength, you should choose the one that comes first in the list. For example, if you are given "Slices" as the class and a list of the extensions: ['SErviNGSliCes', 'Cheese', 'StuFfed'] then you should return 'Slices.SErviNGSliCes' since 'SErviNGSliCes' is the strongest extension (its strength is -1).
[{"input": ["my_class", ["AA", "Be", "CC"]], "expected_output": "my_class.AA"}]
def Strongest_Extension(class_name: String, extensions: List[String]) -> String """You will be given the name of a class (a string) and a list of extensions. The extensions are to be used to load additional classes to the class. The strength of the extension is as follows: Let CAP be the number of the uppercase letters in the extension's name, and let SM be the number of lowercase letters in the extension's name, the strength is given by the fraction CAP - SM. You should find the strongest extension and return a string in this format: ClassName.StrongestExtensionName. If there are two or more extensions with the same strength, you should choose the one that comes first in the list. For example, if you are given "Slices" as the class and a list of the extensions: ['SErviNGSliCes', 'Cheese', 'StuFfed'] then you should return 'Slices.SErviNGSliCes' since 'SErviNGSliCes' is the strongest extension (its strength is -1). """
def problem_spec -- function signature (impl: String β†’ List String β†’ String) -- inputs (class_name: String) (extensions: List String) := let strength (extension: String) := let cap := (extension.toList.filter (fun c => c.isUpper)).length; let sm := (extension.toList.filter (fun c => c.isLower)).length; cap - sm; -- spec let spec (result: String) := let last_pos := result.revPosOf '.'; 0 < extensions.length ∧ extensions.all (fun x => 0 < x.length) ∧ 0 < class_name.length β†’ 0 < result.length ∧ last_pos.isSome ∧ let class_name' := result.take (last_pos.get!).byteIdx; let extension_name := result.drop ((last_pos.get!).byteIdx + 1); class_name' = class_name ∧ extension_name ∈ extensions ∧ let strength_of_extensions := extensions.map (fun ext => strength ext); βˆƒ j : Nat, j < extensions.length ∧ extensions[j]! = extension_name ∧ (βˆ€ i : Nat, i < j β†’ strength_of_extensions[i]! < strength_of_extensions[j]!) ∧ strength_of_extensions[j]! = strength_of_extensions.max?.get!; -- program terminates βˆƒ result, impl class_name extensions = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: String β†’ List String β†’ String) -- inputs (class_name: String) (extensions: List String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ class_name extensions, problem_spec impl class_name extensions) ↔ (βˆ€ class_name extensions, generated_spec impl class_name extensions) :=
sorry
def implementation (class_name: String) (extensions: List String) : String :=
sorry
-- #test implementation 'my_class', ['AA', 'Be', 'CC'] = 'my_class.AA'
theorem correctness (class_name: String) (extensions: List String) : problem_spec implementation class_name extensions :=
sorry
null
null
null
def cycpattern_check(String a, String b) -> Bool
You are given 2 words. You need to return True if the second word or any of its rotations is a substring in the first word, else False
[{"input": ["abcd", "abd"], "expected_output": false}, {"input": ["hello", "ell"], "expected_output": true}, {"input": ["whassup", "psus"], "expected_output": false}, {"input": ["abab", "baa"], "expected_output": true}, {"input": ["efef", "eeff"], "expected_output": false}, {"input": ["himenss", "simen"], "expected_output": true}]
def cycpattern_check(String a, String b) -> Bool """You are given 2 words. You need to return True if the second word or any of its rotations is a substring in the first word, else False """
def problem_spec -- function signature (impl: String β†’ String β†’ Bool) -- inputs (a b: String) := -- spec let spec (result: Bool) := (b.length = 0 β†’ result) ∧ (0 < b.length β†’ result ↔ ((b.length ≀ a.length) ∧ (βˆƒ i : Nat, i < b.length ∧ let b_rotation := b.drop i ++ b.take i; a.containsSubstr b_rotation))); -- program terminates βˆƒ result, impl a b = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: String β†’ String β†’ Bool) -- inputs (a b: String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ a b, problem_spec impl a b) ↔ (βˆ€ a b, generated_spec impl a b) :=
sorry
def implementation (a b: String) : Bool :=
sorry
-- #test implementation "abcd" "abd" = False -- #test implementation "hello" "ell" = True -- #test implementation "whassup" "psus" = False -- #test implementation "abab" "baa" = True -- #test implementation "efef" "eeff" = False -- #test implementation "himenss" "simen" = True
theorem correctness (a b: String) : problem_spec implementation a b :=
sorry
null
null
null
def even_odd_count(num: int) -> Tuple[int, int]
Given an integer. return a tuple that has the number of even and odd digits respectively.
[{"input": -12, "expected_output": [1, 1]}, {"input": 123, "expected_output": [1, 2]}]
def even_odd_count(num: int) -> Tuple[int, int] """Given an integer. return a tuple that has the number of even and odd digits respectively. """
def problem_spec -- function signature (impl: Int β†’ Int Γ— Int) -- inputs (num: Int) := -- spec let spec (result: Int Γ— Int) := let (even_count, odd_count) := result; let numAbs := |num|.toNat; let numBy10 := numAbs/10; let (even_count', odd_count') := impl numBy10; (result = impl numAbs) ∧ (0 ≀ num β†’ (Even num ↔ 1 + even_count' = even_count) ∧ (Odd num ↔ even_count' = even_count)) ∧ (0 ≀ num β†’ (Odd num ↔ 1 + odd_count' = odd_count) ∧ (Even num ↔ odd_count' = odd_count)); -- program terminates βˆƒ result, impl num = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: Int β†’ Int Γ— Int) -- inputs (num: Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ num, problem_spec impl num) ↔ (βˆ€ num, generated_spec impl num) :=
sorry
def implementation (num: Int) : Int Γ— Int :=
sorry
-- #test implementation -12 = (1, 1) -- #test implementation 123 = (1, 2)
theorem correctness (num: Int) : problem_spec implementation num :=
sorry
null
null
null
def int_to_mini_roman(num: Nat) -> String
Given a positive integer, obtain its roman numeral equivalent as a string, and return it in lowercase. Restrictions: 1 <= num <= 1000
[{"input": 19, "expected_output": "xix"}, {"input": 152, "expected_output": "clii"}, {"input": 426, "expected_output": "cdxxvi"}]
def int_to_mini_roman(num: Nat) -> String """Given a positive integer, obtain its roman numeral equivalent as a string, and return it in lowercase. Restrictions: 1 <= num <= 1000 """
def problem_spec -- function signature (impl: Nat β†’ String) -- inputs (num: Nat) := -- spec let spec (result: String) := 1 ≀ num ∧ num ≀ 1000 ∧ (result.data.all (fun c => c.isLower)) β†’ isValidRoman result ∧ romanToDecimal result = num -- program terminates βˆƒ result, impl num = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: Nat β†’ String) -- inputs (num: Nat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ num, problem_spec impl num) ↔ (βˆ€ num, generated_spec impl num) :=
sorry
def implementation (num: Nat) : String :=
sorry
-- #test implementation 19 = "xix" -- #test implementation 152 = "clii" -- #test implementation 426 = "cdxxvi"
theorem correctness (num: Nat) : problem_spec implementation num :=
sorry
/-- name: romanCharToValue use: | Map from valid characters and their values problems: - 156 sample_problems: [] -/ def romanCharToValue : Char β†’ Nat | 'i' => 1 | 'v' => 5 | 'x' => 10 | 'l' => 50 | 'c' => 100 | 'd' => 500 | 'm' => 1000 | _ => 0 /-- name: validSubtractivePairs use: | Legal subtractive pairs: first char can precede second for subtraction (in roman numerals) problems: - 156 sample_problems: [] -/ def validSubtractivePairs : List (Char Γ— Char) := [('i', 'v'), ('i', 'x'), ('x', 'l'), ('x', 'c'), ('c', 'd'), ('c', 'm')] /-- name: maxRepetitions use: | Max allowed repetitions for each character (in roman numerals) problems: - 156 sample_problems: [] -/ def maxRepetitions : Char β†’ Nat | 'i' | 'x' | 'c' | 'm' => 3 | 'v' | 'l' | 'd' => 1 | _ => 0 /-- name: countRepetitions use: | Helper to count consecutive repetitions (in roman numerals) problems: - 156 sample_problems: [] -/ def countRepetitions : List Char β†’ Char β†’ Nat β†’ Nat | [], _, n => n | (h :: t), c, n => if h = c then countRepetitions t c (n + 1) else n /-- name: validRepetition use: | Helper to validate proper use of repetitions (in roman numerals) problems: - 156 sample_problems: [] -/ partial def validRepetition : List Char β†’ Bool | [] => true | c :: rest => let max := maxRepetitions c let count := countRepetitions rest c 1 count ≀ max ∧ validRepetition (rest.drop (count - 1)) /-- name: validSubtractiveOrder use: | Helper to validate legal subtractive combinations (in roman numerals) problems: - 156 sample_problems: [] -/ def validSubtractiveOrder : List Char β†’ Bool | [] | [_] => true | c1 :: c2 :: rest => match romanCharToValue c1, romanCharToValue c2 with | v1, v2 => if v1 < v2 then -- check if c1 and c2 form a legal subtractive pair (c1, c2) ∈ validSubtractivePairs ∧ validSubtractiveOrder rest else if v1 = 0 ∨ v2 = 0 then false else validSubtractiveOrder (c2 :: rest) /-- name: isValidRoman use: | Function to check if a string is a roman numeral problems: - 156 sample_problems: [] -/ def isValidRoman (s : String) : Bool := s.data.all (Ξ» c => romanCharToValue c β‰  0) ∧ validRepetition s.data ∧ validSubtractiveOrder s.data /-- name: romanToDecimalAux use: | Helper to convert list of roman characters to decimal problems: - 156 sample_problems: [] -/ def romanToDecimalAux : List Char β†’ Nat | [] => 0 | c1 :: c2 :: rest => let val1 := romanCharToValue c1 let val2 := romanCharToValue c2 if val1 < val2 then -- subtractive notation (val2 - val1) + romanToDecimalAux rest else val1 + romanToDecimalAux (c2 :: rest) | [c] => romanCharToValue c /-- name: romanToDecimalAux use: | Function to convert a valid lowercase Roman numeral string to Nat problems: - 156 sample_problems: [] -/ def romanToDecimal (s : String) : Nat := romanToDecimalAux s.data
null
null
def right_angle_triangle(a: Nat, b: Nat, c: Nat) -> Bool
Given the lengths of the three sides of a triangle. Return True if the three sides form a right-angled triangle, False otherwise. A right-angled triangle is a triangle in which one angle is right angle or 90 degree.
[{"input": [3, 4, 5], "expected_output": true}, {"input": [1, 2, 3], "expected_output": false}]
def right_angle_triangle(a: Nat, b: Nat, c: Nat) -> Bool """Given the lengths of the three sides of a triangle. Return True if the three sides form a right-angled triangle, False otherwise. A right-angled triangle is a triangle in which one angle is right angle or 90 degree. """
def problem_spec -- function signature (impl: Nat β†’ Nat β†’ Nat β†’ Bool) -- inputs (a b c: Nat) := -- spec let spec (result: Bool) := result ↔ 0 < a ∧ 0 < b ∧ 0 < c ∧ ((a * a + b * b = c * c) ∨ (a * a + c * c = b * b) ∨ (b * b + c * c = a * a)) -- program terminates βˆƒ result, impl a b c = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: Nat β†’ Nat β†’ Nat β†’ Bool) -- inputs (a b c: Nat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ a b c, problem_spec impl a b c) ↔ (βˆ€ a b c, generated_spec impl a b c) :=
sorry
def implementation (a b c: Nat) : Bool :=
sorry
-- #test implementation ([1, 2, 2, -4]: List Int) = (-9: Int) -- #test implementation ([0, 1]: List Int) = (0: Int) -- #test implementation ([]: List Int) = none
theorem correctness (a b c: Nat) : problem_spec implementation a b c :=
sorry
null
null
null
def find_max(words: List String) -> String
Write a function that accepts a list of strings. The list contains different words. Return the word with maximum number of unique characters. If multiple strings have maximum number of unique characters, return the one which comes first in lexicographical order.
[{"input": ["name", "of", "string"], "expected_output": "string"}, {"input": ["name", "enam", "game"], "expected_output": "enam"}, {"input": ["aaaaaaa", "bb", "cc"], "expected_output": "aaaaaaa"}]
def find_max(words: List String) -> String """Write a function that accepts a list of strings. The list contains different words. Return the word with maximum number of unique characters. If multiple strings have maximum number of unique characters, return the one which comes first in lexicographical order. """
def problem_spec -- function signature (impl: List String β†’ String) -- inputs (words: List String) := let unique_chars (string: String) := let string_idx := {i: Nat | i < string.length}.toFinset; let characters := string_idx.image (fun i => string.toList.get! i); characters.card; -- spec let spec (result: String) := (result = "" ↔ words.length = 0) ∧ (words.length != 0 β†’ result ∈ words ∧ let unique_chars_list := words.map unique_chars; let max_unique_chars := unique_chars_list.max?.get!; unique_chars result = max_unique_chars ∧ βˆ€ i : Nat, i < words.length β†’ unique_chars_list[i]! = max_unique_chars β†’ result ≀ words[i]!); -- program terminates βˆƒ result, impl words = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: List String β†’ String) -- inputs (words: List String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ words, problem_spec impl words) ↔ (βˆ€ words, generated_spec impl words) :=
sorry
def implementation (words: List String) : String :=
sorry
-- #test implementation ["name", "of", "string"]= "string" -- #test implementation ["name", "enam", "game"] = "enam" -- #test implementation ["aaaaaaa", "bb" ,"cc"] = "aaaaaaa"
theorem correctness (words: List String) : problem_spec implementation words :=
sorry
null
null
null
def eat(number: Nat, need: Nat, remaining: Nat) -> List Nat
You're a hungry rabbit, and you already have eaten a certain number of carrots, but now you need to eat more carrots to complete the day's meals. you should return an array of [ total number of eaten carrots after your meals, the number of carrots left after your meals ] if there are not enough remaining carrots, you will eat all remaining carrots, but will still be hungry. Variables: @number : integer the number of carrots that you have eaten. @need : integer the number of carrots that you need to eat. @remaining : integer the number of remaining carrots thet exist in stock Constrain: * 0 <= number <= 1000 * 0 <= need <= 1000 * 0 <= remaining <= 1000
[{"input": [5, 6, 10], "expected_output": [11, 4]}, {"input": [4, 8, 9], "expected_output": [12, 1]}, {"input": [1, 10, 10], "expected_output": [11, 0]}, {"input": [2, 11, 5], "expected_output": [7, 0]}]
def eat(number: Nat, need: Nat, remaining: Nat) -> List Nat """You're a hungry rabbit, and you already have eaten a certain number of carrots, but now you need to eat more carrots to complete the day's meals. you should return an array of [ total number of eaten carrots after your meals, the number of carrots left after your meals ] if there are not enough remaining carrots, you will eat all remaining carrots, but will still be hungry. Variables: @number : integer the number of carrots that you have eaten. @need : integer the number of carrots that you need to eat. @remaining : integer the number of remaining carrots thet exist in stock Constrain: * 0 <= number <= 1000 * 0 <= need <= 1000 * 0 <= remaining <= 1000 """
def problem_spec -- function signature (impl: Nat β†’ Nat β†’ Nat β†’ List Nat) -- inputs (number need remaining: Nat) := -- spec let spec (result: List Nat) := number ≀ 1000 β†’ need ≀ 1000 β†’ remaining ≀ 1000 β†’ result.length = 2 ∧ (need ≀ remaining β†’ result[0]! - need = number ∧ need = remaining - result[1]!) ∧ (remaining < need β†’ result[0]! - remaining = number ∧ result[1]! = 0); -- program terminates βˆƒ result, impl number need remaining = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: Nat β†’ Nat β†’ Nat β†’ List Nat) -- inputs (a b c : Nat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ a b c, problem_spec impl a b c) ↔ (βˆ€ a b c, generated_spec impl a b c) :=
sorry
def implementation (a b c: Nat) : List Nat :=
sorry
-- #test implementation 5 6 10 = [11, 4] -- #test implementation 4 8 9 = [12, 1] -- #test implementation 1 10 10 = [11, 0] -- #test implementation 2 11 5 = [7, 0]
theorem correctness (a b c: Nat) : problem_spec implementation a b c :=
sorry
null
null
null
def do_algebra(operator: List String, operand: List Nat) -> Int
Given two lists operator, and operand. The first list has basic algebra operations, and the second list is a list of integers. Use the two given lists to build the algebric expression and return the evaluation of this expression. The basic algebra operations: Addition ( + ) Subtraction ( - ) Multiplication ( * ) Floor division ( // ) Exponentiation ( ** ) Note: The length of operator list is equal to the length of operand list minus one. Operand is a list of of non-negative integers. Operator list has at least one operator, and operand list has at least two operands.
[{"input": [["+", "*", "-"], [2, 3, 4, 5]], "expected_output": 9}]
def do_algebra(operator: List String, operand: List Nat) -> Int """Given two lists operator, and operand. The first list has basic algebra operations, and the second list is a list of integers. Use the two given lists to build the algebric expression and return the evaluation of this expression. The basic algebra operations: Addition ( + ) Subtraction ( - ) Multiplication ( * ) Floor division ( // ) Exponentiation ( ** ) Note: The length of operator list is equal to the length of operand list minus one. Operand is a list of of non-negative integers. Operator list has at least one operator, and operand list has at least two operands. """
def problem_spec -- function signature (impl: List String β†’ List Nat β†’ Int) -- inputs (operator : List String) (operand : List Nat) := -- spec let spec (result: Int) := operator.length = operand.length - 1 ∧ 0 < operator.length ∧ operand.all (fun n => 0 ≀ n) β†’ let inline_tokens : List String := mergeAlternately operand operator evalArith_precedence inline_tokens result -- program terminates βˆƒ result, impl operator operand = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: List String β†’ List Nat β†’ Int) -- inputs (operator : List String) (operand : List Nat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ operator operand, problem_spec impl operator operand) ↔ (βˆ€ operator operand, generated_spec impl operator operand) :=
sorry
def implementation (operator: List String) (operand : List Nat) : Int :=
sorry
-- #test implementation ['+', '*', '-'] [2,3,4,5] = 9
theorem correctness (operator : List String) (operand : List Nat) : problem_spec implementation operator operand :=
sorry
/-- name: mergeAlternately use: | Helper Methods to mergeAlternately a list of strings problems: - 160 sample_problems: [] -/ def mergeAlternately : List Nat β†’ List String β†’ List String | [], [] => [] | [], y :: ys => y :: mergeAlternately [] ys | x :: xs, [] => x.repr :: mergeAlternately xs [] | x :: xs, y :: ys => x.repr :: y :: mergeAlternately xs ys /-- name: applyOp use: | Helper method to apply operations on two integers problems: - 160 sample_problems: [] -/ def applyOp (x y : Int) : String β†’ Option Int | "+" => some (x + y) | "-" => some (x - y) | "*" => some (x * y) | "//" => if y == 0 then none else some (x / y) | "**" => if x < 0 then none else some (Int.ofNat ((Int.toNat x) ^ (Int.toNat y))) | _ => none /-- name: evalArith_pass use: | Noncomputable relational spec for a single step evaluation (any op, ignoring precedence). problems: - 160 sample_problems: [] -/ inductive evalArith_pass : List String β†’ Int β†’ Prop | num {s : String} {n : Nat} (h : s.toNat! = n) : evalArith_pass [s] (Int.ofNat n) | binOp {ts1 ts2 : List String} {op : String} {r1 r2 r : Int} (h1 : evalArith_pass ts1 r1) (h2 : evalArith_pass ts2 r2) (hop : applyOp r1 r2 op = some r) : evalArith_pass (ts1 ++ op :: ts2) r /-- name: evalArith_exp use: | Relational spec for exponentiation (highest precedence). problems: - 160 sample_problems: [] -/ inductive evalArith_exp : List String β†’ Int β†’ Prop | of_pass {ts r} (h : evalArith_pass ts r) : evalArith_exp ts r | step {ts1 ts2 r1 r2 r} (h1 : evalArith_exp ts1 r1) (h2 : evalArith_exp ts2 r2) (hop : applyOp r1 r2 "**" = some r) : evalArith_exp (ts1 ++ "**" :: ts2) r /-- name: evalArith_mul use: | Relational spec for multiplication/division (middle precedence). problems: - 160 sample_problems: [] -/ inductive evalArith_mul : List String β†’ Int β†’ Prop | of_exp {ts r} (h : evalArith_exp ts r) : evalArith_mul ts r | step {ts1 ts2 r1 r2 r} (h1 : evalArith_mul ts1 r1) (h2 : evalArith_mul ts2 r2) (hop : applyOp r1 r2 "*" = some r ∨ applyOp r1 r2 "//" = some r) : evalArith_mul (ts1 ++ "*" :: ts2) r /-- name: evalArith_mul use: | Relational spec for addition/subtraction (lowest precedence). problems: - 160 sample_problems: [] -/ inductive evalArith_add : List String β†’ Int β†’ Prop | of_mul {ts r} (h : evalArith_mul ts r) : evalArith_add ts r | step {ts1 ts2 r1 r2 r} (h1 : evalArith_add ts1 r1) (h2 : evalArith_add ts2 r2) (hop : applyOp r1 r2 "+" = some r ∨ applyOp r1 r2 "-" = some r) : evalArith_add (ts1 ++ "+" :: ts2) r /-- name: evalArith_mul use: | Main function to evaluate an expression problems: - 160 sample_problems: [] -/ def evalArith_precedence (ts : List String) (r : Int) : Prop := evalArith_add ts r
null
null
def solve(string : String) -> String
You are given a string s. if s[i] is a letter, reverse its case from lower to upper or vise versa, otherwise keep it as it is. If the string contains no letters, reverse the string. The function should return the resulted string.
[{"input": "1234", "expected_output": "4321"}, {"input": "ab", "expected_output": "AB"}, {"input": "#a@C", "expected_output": "#A@c"}]
def solve(string : String) -> String """You are given a string s. if s[i] is a letter, reverse its case from lower to upper or vise versa, otherwise keep it as it is. If the string contains no letters, reverse the string. The function should return the resulted string. """
def problem_spec -- function signature (impl: String β†’ String) -- inputs (string : String) := -- spec let spec (result: String) := result.length = string.length ∧ let hasNoAlphabet := string.all (Ξ» c => not (c.isAlpha)); (hasNoAlphabet β†’ result.toList = string.toList.reverse) ∧ (hasNoAlphabet = false β†’ βˆ€ i, i < string.length β†’ let c := string.get! ⟨i⟩; (c.isAlpha β†’ ((c.isLower β†’ c.toUpper = result.get! ⟨i⟩) ∨ (c.isUpper β†’ c.toLower = result.get! ⟨i⟩))) ∧ (Β¬ c.isAlpha β†’ c = result.get! ⟨i⟩)) -- program terminates βˆƒ result, impl string = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: String β†’ String) -- inputs (s : String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ s, problem_spec impl s) ↔ (βˆ€ s, generated_spec impl s) :=
sorry
def implementation (s: String) : String :=
sorry
-- #test implementation "1234" = "4321" -- #test implementation "ab" = "AB" -- #test implementation "#a@C" = "#A@c"
theorem correctness (s: String) : problem_spec implementation s :=
sorry
null
null
null
def generate_integers(a : Nat, b : Nat) -> List Nat
Given two positive integers a and b, return the even digits between a and b, in ascending order.
[{"input": [2, 8], "expected_output": [2, 4, 6, 8]}, {"input": [8, 2], "expected_output": [2, 4, 6, 8]}, {"input": [10, 14], "expected_output": [10, 12, 14]}]
def generate_integers(a : Nat, b : Nat) -> List Nat """Given two positive integers a and b, return the even digits between a and b, in ascending order. """
def problem_spec -- function signature (impl: Nat β†’ Nat β†’ List Nat) -- inputs (a b : Nat) := let isAscendingBy2 (r : List Nat) := βˆ€ i, i < r.length - 1 β†’ r[i+1]! - r[i]! = 2 -- spec let spec (result: List Nat) := result.all (fun n => n % 2 = 0) ∧ isAscendingBy2 result ∧ 1 < result.length ∧ let min_a_b := min a b; let max_a_b := max a b; if min_a_b = max_a_b ∧ (min_a_b % 2 = 1) then result = [] else ((result[0]! = if 2 ∣ min_a_b then min_a_b else (min_a_b + 1)) ∧ (result[result.length-1]! = if 2 ∣ max_a_b then max_a_b else max_a_b - 1)) -- program terminates βˆƒ result, impl a b = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: Nat β†’ Nat β†’ List Nat) -- inputs (a b : Nat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ a b, problem_spec impl a b) ↔ (βˆ€ a b, generated_spec impl a b) :=
sorry
def implementation (a b: Nat) : List Nat :=
sorry
-- #test implementation 2 8 = [2, 4, 6, 8] -- #test implementation 8 2 = [2, 4, 6, 8] -- #test implementation 10 14 = [10, 12, 14]
theorem correctness (a b: Nat) : problem_spec implementation a b :=
sorry
null
null
null