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 has_close_elements(numbers: List[float], threshold: float) -> bool
Check if in given list of numbers, are any two numbers closer to each other than given threshold.
[{"input": [[1.0, 2.0, 3.0], 0.5], "expected_output": false}, {"input": [[1.0, 2.8, 3.0, 4.0, 5.0, 2.0], 0.3], "expected_output": true}]
def has_close_elements(numbers: List[float], threshold: float) -> bool """Check if in given list of numbers, are any two numbers closer to each other than given threshold."""
def problem_spec -- function signature (impl: List Rat β†’ Rat β†’ Bool) -- inputs (numbers: List Rat) (threshold: Rat) := -- spec let numbers_within_threshold := (βˆƒ i j, i < numbers.length ∧ j < numbers.length ∧ i β‰  j ∧ |numbers.get! i - numbers.get! j| < threshold); let spec (res: Bool) := numbers.length > 1 β†’ if res then numbers_within_threshold else Β¬numbers_within_threshold; -- program terminates βˆƒ result, impl numbers threshold = result ∧ -- return value satisfies spec spec result -- if result then spec else Β¬spec
def generated_spec -- function signature (impl: List Rat β†’ Rat β†’ Bool) -- inputs (numbers: List Rat) (threshold: Rat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ numbers threshold, problem_spec impl numbers threshold) ↔ (βˆ€ numbers threshold, generated_spec impl numbers threshold) :=
sorry
def implementation (numbers: List Rat) (threshold: Rat) : Bool :=
match numbers with | [] => false | (x::xs) => (xs.any (fun y => (|x - y| < threshold))) || implementation xs threshold
#test implementation ([1, 2, 3]: List Rat) 0.5 = false #test implementation ([1, 2.8, 3, 4, 5, 2]: List Rat) 0.3 = true
theorem correctness (numbers: List Rat) (threshold: Rat) : problem_spec implementation numbers threshold :=
by unfold problem_spec let result := implementation numbers threshold use result simp [result] induction numbers simp [implementation] rename_i head tail ih simp at * by_cases result = false rename_i h_result_false simp [result] at h_result_false simp [h_result_false] simp [implementation] at h_result_false have h_tail_impl_false := And.right h_result_false have h_head_in_threshold := And.left h_result_false simp [h_tail_impl_false] at ih by_cases h_1_lt_tail: 1 < tail.length have h_0_lt_tail_len : 0 < tail.length := by linarith simp [h_0_lt_tail_len, h_1_lt_tail] simp [h_1_lt_tail] at ih intros i h_i j h_j h_i_neq_j by_cases i = 0 rename_i h_i_eq_0 simp [h_i_eq_0] simp [h_i_eq_0] at h_i_neq_j have h_0_lt_j : 0 < j := by contrapose! h_i_neq_j linarith let j' := j - 1 have h_j'_tail : (head::tail)[j' + 1]! = tail[j']! := by rw [List.getElem!_cons_succ] have h_j'_j : j' + 1 = j := by simp [j'] rw [Nat.sub_add_cancel h_0_lt_j] rw [←h_j'_j] simp [h_j'_tail] have h_j'_lt_tail_len : j' < tail.length := by linarith simp [h_j'_lt_tail_len] apply h_head_in_threshold simp rename_i h_i_neq_0 have h_0_lt_i : 0 < i := by contrapose! h_i_neq_0 linarith let i' := i - 1 have h_i'_tail : (head::tail)[i' + 1]! = tail[i']! := by rw [List.getElem!_cons_succ] have h_i'_i : i' + 1 = i := by simp [i'] rw [Nat.sub_add_cancel h_0_lt_i] rw [←h_i'_i] simp [h_i'_tail] have h_i'_lt_tail_len : i' < tail.length := by linarith simp [h_i'_lt_tail_len] by_cases 0 < j rename_i h_0_lt_j let j' := j - 1 have h_j'_tail : (head::tail)[j' + 1]! = tail[j']! := by rw [List.getElem!_cons_succ] have h_j'_j : j' + 1 = j := by simp [j'] rw [Nat.sub_add_cancel h_0_lt_j] rw [←h_j'_j] simp [h_j'_tail] have h_j'_lt_tail_len : j' < tail.length := by linarith simp [h_j'_lt_tail_len] have h_i'_neq_j' : i' β‰  j' := by contrapose! h_i_neq_j linarith have h_final := ih i' simp [h_i'_lt_tail_len] at h_final have h_final' := h_final j' h_j'_lt_tail_len simp [h_i'_neq_j'] at h_final' simp [h_j'_lt_tail_len] at h_final' assumption rename_i h_j_le_0 have h_j_eq_0 : j = 0 := by linarith simp [h_j_eq_0] have h_head_minus_tail : tail[i'] - head = -(head - tail[i']) := by simp rw [h_head_minus_tail] have h_head_minus_tail_lt_threshold : |-(head - tail[i'])| = |head - tail[i']| := by rw [abs_neg] rw [h_head_minus_tail_lt_threshold] apply h_head_in_threshold simp have h_tl_eq_0 : tail.length ≀ 1 := by linarith by_cases tail.length = 1 rename_i h_tl_eq_1 simp [h_tl_eq_1] intros i h_i j h_j h_i_neq_j by_cases i = 0 rename_i h_i_eq_0 simp [h_i_eq_0] by_cases j = 0 rename_i h_j_eq_0 rw [h_j_eq_0] at h_i_neq_j contradiction by_cases j = 1 rename_i h_j_eq_1 simp [h_j_eq_1] simp [h_tl_eq_1] apply h_head_in_threshold simp rename_i h_j_neq_1 h_j_neq_0 have h_0_lt_j : 0 < j := by contrapose! h_j_neq_0 simp at h_j_neq_0 contradiction have h_1_lt_j : 1 < j := by contrapose! h_j_neq_1 have h_j_eq_1' : j = 1 := by linarith contradiction linarith rename_i h_i_neq_0 by_cases i = 1 rename_i h_i_eq_1 simp [h_i_eq_1] simp [h_tl_eq_1] by_cases j = 0 rename_i h_j_eq_0 simp [h_j_eq_0] have h_head_minus_tail : tail[0] - head = -(head - tail[0]) := by simp rw [h_head_minus_tail] rw [abs_neg] apply h_head_in_threshold simp by_cases j = 1 rename_i h_j_neq_0 h_j_eq_1 rw [h_j_eq_1] at h_i_neq_j contradiction rename_i h_j_neq_1 h_j_neq_0 have h_0_lt_j : 0 < j := by contrapose! h_j_neq_0 simp at h_j_neq_0 contradiction have h_1_lt_j : 1 < j := by contrapose! h_j_neq_1 have h_j_eq_1' : j = 1 := by linarith contradiction linarith rename_i h_i_neq_1 have h_0_lt_i : 0 < i := by contrapose! h_i_neq_1 simp at h_i_neq_1 contradiction have h_1_lt_i : 1 < i := by contrapose! h_i_neq_0 have h_i_eq_1' : i = 1 := by linarith contradiction linarith rename_i h_tl_neq_1 rw [Nat.le_one_iff_eq_zero_or_eq_one] at h_tl_eq_0 by_cases tail.length = 0 rename_i h_tl_eq_0 simp [h_tl_eq_0] rename_i h_tl_neq_0 cases h_tl_eq_0 contradiction contradiction -- case when tail has two elements that are close to each other rename_i h_result_true simp at h_result_true simp [result] at h_result_true simp [h_result_true] simp [implementation] at h_result_true by_cases h_0_lt_tail_len : 0 < tail.length rw [List.exists_mem_iff_getElem] at h_result_true simp [h_0_lt_tail_len] cases h_result_true use 0 simp rename_i h_head_in_threshold obtain ⟨ j, h_j_lt_len, h_j_in_threshold ⟩ := h_head_in_threshold use j + 1 simp [h_j_lt_len] assumption rename_i h_tail_has_close_elements simp [h_tail_has_close_elements] at ih by_cases h_1_lt_tail : 1 < tail.length simp [h_1_lt_tail] at ih obtain ⟨ i, h_i_lt_tail_len, h_i_in_threshold ⟩ := ih use i + 1 have h_i_lt_tail_len' : i + 1 < tail.length + 1 := by linarith simp [h_i_lt_tail_len'] obtain ⟨ j, h_j_lt_tail_len, h_i_neq_j, h_j_in_threshold ⟩ := h_i_in_threshold use j + 1 simp [h_j_lt_tail_len] simp [h_i_neq_j] simp [h_i_lt_tail_len, h_j_lt_tail_len] at h_j_in_threshold assumption use 0 simp use 1 simp unfold implementation at h_tail_has_close_elements by_cases h_1_tail: tail.length = 0 have h_tail_empty := List.eq_nil_of_length_eq_zero h_1_tail simp [h_tail_empty] at h_tail_has_close_elements simp at h_1_lt_tail have h_0_lt_tail_len : tail.length = 1 := by linarith have h_tail_singleton: βˆƒ x, tail = [x] := by rw [List.length_eq_one] at h_0_lt_tail_len assumption obtain ⟨ x, h_tail_singleton ⟩ := h_tail_singleton simp [h_0_lt_tail_len] simp [h_tail_singleton] try (simp [h_tail_singleton] at h_tail_has_close_elements) unfold implementation at h_tail_has_close_elements simp at h_tail_has_close_elements simp at h_0_lt_tail_len have h_tail_ln_eq_0 : tail.length = 0 := by rw [h_0_lt_tail_len] simp simp [h_tail_ln_eq_0]
null
null
null
def separate_paren_groups(paren_string: str) -> List[str]
Input to this function is a string containing multiple groups of nested parentheses. Your goal is to separate those group into separate strings and return the list of those. Separate groups are balanced (each open brace is properly closed) and not nested within each other Ignore any spaces in the input string.
[{"input": "( ) (( )) (( )( ))", "expected_output": ["()", "(())", "(()())"]}]
def separate_paren_groups(paren_string: str) -> List[str] """Input to this function is a string containing multiple groups of nested parentheses. Your goal is to separate those group into separate strings and return the list of those. Separate groups are balanced (each open brace is properly closed) and not nested within each other Ignore any spaces in the input string. """
def problem_spec -- function signature (impl: String β†’ List String) -- inputs (paren_string: String) := -- spec let paren_string_filtered := (paren_string.toList.filter (fun c => c == '(' ∨ c == ')')).asString; let forward_spec (result_list: List String) := -- every substring of the input string -- that is a balanced paren group is in -- the result list βˆ€ i j, j < paren_string_filtered.length β†’ i ≀ j β†’ let substr_ij := (paren_string_filtered.take (j + 1)).drop i; balanced_paren_non_computable substr_ij '(' ')' β†’ count_paren_groups substr_ij = 1 β†’ substr_ij ∈ result_list; let backward_spec (result_list: List String) := -- every string in the result list is a -- balanced paren group and is a substring -- of the input string i.e. there is no -- extra substring in the result list -- that is not from the input string βˆ€ str, str ∈ result_list β†’ paren_string_filtered.containsSubstr str = true ∧ balanced_paren_non_computable str '(' ')' ∧ count_paren_groups str = 1; let spec (result_list: List String) := forward_spec result_list ∧ backward_spec result_list; -- program terminates βˆƒ result, impl paren_string = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: String β†’ List String) -- inputs (paren_string: String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ paren_string, problem_spec impl paren_string) ↔ (βˆ€ paren_string, generated_spec impl paren_string) :=
sorry
def implementation (paren_string: String) : List String :=
-- remove space or any other characters that are not '(' or ')' let filtered := paren_string.toList.filter (fun c => c == '(' ∨ c == ')'); -- auxiliary recursive function: let rec aux (cs : List Char) (cur : List Char) (balance : Int) (acc : List String) : List String := match cs with | [] => acc.reverse -- when finished, return accumulated groups in original order | c::rest => let new_cur := cur ++ [c] let new_balance:= if c = '(' then balance + 1 else balance - 1 if new_balance = 0 then let group := new_cur.asString aux rest [] 0 (group :: acc) else aux rest new_cur new_balance acc; aux filtered [] 0 []
#test implementation "( ) (( )) (( )( ))" = ["()", "(())", "(()())"]
theorem correctness (paren_string: String) : problem_spec implementation paren_string :=
by unfold problem_spec let result := implementation paren_string use result simp [result] 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_paren_groups_helper use: | Helper to count the number of groups of parentheses in a string. problems: - 1 -/ def count_paren_groups_helper (paren_string: String) (num_open: Int) (num_groups: Nat): Nat := -- Recursively count the number of paren groups if paren_string.isEmpty then num_groups else let c := paren_string.get! 0 if c == '(' then count_paren_groups_helper (paren_string.drop 1) (num_open + 1) num_groups else if c == ')' then let new_num_groups := if num_open == 1 then num_groups + 1 else num_groups count_paren_groups_helper (paren_string.drop 1) (num_open - 1) new_num_groups else count_paren_groups_helper (paren_string.drop 1) num_open num_groups 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_paren_groups use: | Function to count the number of groups of parentheses in a string. problems: - 1 -/ def count_paren_groups (paren_string: String): Nat := count_paren_groups_helper paren_string 0 0
null
null
def truncate_number(number: float) -> float
Given a positive floating point number, it can be decomposed into and integer part (largest integer smaller than given number) and decimals (leftover part always smaller than 1).
[{"input": 3.5, "expected_output": 0.5}]
def truncate_number(number: float) -> float """Given a positive floating point number, it can be decomposed into and integer part (largest integer smaller than given number) and decimals (leftover part always smaller than 1). """
def problem_spec -- function signature (impl: Rat β†’ Rat) -- inputs (number: Rat) := -- spec let spec (res) := 0 ≀ res ∧ res < 1 ∧ number.floor + res = number; number > 0 β†’ -- program terminates (βˆƒ result, impl number = result ∧ -- return value satisfies spec spec result)
def generated_spec -- function signature (impl: Rat β†’ Rat) -- inputs (number: Rat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ number, problem_spec impl number) ↔ (βˆ€ number, generated_spec impl number) :=
sorry
def implementation (number: Rat) : Rat :=
number - number.floor
#test implementation 3.5 = 0.5
theorem correctness (number: Rat) : problem_spec implementation number :=
by unfold problem_spec let result := implementation number simp [result] simp [implementation] have h1: βˆ€ x: Rat, x.floor ≀ x := by intro x rw [←Rat.le_floor] intro npos apply And.intro apply h1 have h2 := h1 number sorry
null
null
null
def below_zero(operations: List[int]) -> bool
You're given a list of deposit and withdrawal operations on a bank account that starts with zero balance. Your task is to detect if at any point the balance of account fallls below zero, and at that point function should return True. Otherwise it should return False.
[{"input": [1, 2, 3], "expected_output": false}, {"input": [1, 2, -4, 5], "expected_output": true}]
def below_zero(operations: List[int]) -> bool """You're given a list of deposit and withdrawal operations on a bank account that starts with zero balance. Your task is to detect if at any point the balance of account fallls below zero, and at that point function should return True. Otherwise it should return False. """
def problem_spec -- function signature (impl: List Int β†’ Bool) -- inputs (operations: List Int) := -- spec let below_zero_condition := βˆƒ i, i ≀ operations.length ∧ (operations.take i).sum < 0; let spec (result: Bool) := if result then below_zero_condition else Β¬below_zero_condition; -- program terminates βˆƒ result, impl operations = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: List Int β†’ Bool) -- inputs (operations: List Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ operations, problem_spec impl operations) ↔ (βˆ€ operations, generated_spec impl operations) :=
sorry
def implementation (operations: List Int) : Bool :=
let rec check (ops : List Int) (acc : Int) : Bool := match ops with | [] => false | op :: ops' => let new_acc := acc + op if new_acc < 0 then true else check ops' new_acc check operations 0
#test implementation [1, 2, 3] = false #test implementation [1, 2, -4, 5] = true
theorem correctness (operations: List Int) : problem_spec implementation operations :=
by -- sometimes we have to create a temporary variable to use in the proof unfold problem_spec let result := implementation operations use result simp [result] simp [implementation] -- induction at this point will not work -- because the induction hypothesis is not strong enough -- we need to prove a stronger inductive hypothesis have h_harder_inductive_hypothesis_true: βˆ€ h, βˆ€ ops, implementation.check ops h = true β†’ βˆƒ i, i ≀ ops.length ∧ h + (ops.take i).sum < 0 := by intros h ops h_check induction' ops generalizing h simp [implementation.check] at h_check rename_i head tail ih simp [implementation.check] at h_check by_cases h_head_lt_0 : h + head < 0 use 1 simp assumption simp at h_head_lt_0 cases h_check linarith rename_i h_tail_sum_gt_0 specialize ih (h + head) simp [h_tail_sum_gt_0] at ih obtain ⟨i, h_i_lt_tail_len, h_tail_sum_lt_0⟩ := ih use i + 1 simp [h_i_lt_tail_len] linarith by_cases h_if_pos: (implementation.check operations 0 = true) simp [h_if_pos] obtain ⟨i, h_i_lt_tail_len, h_tail_sum_lt_0⟩ := h_harder_inductive_hypothesis_true 0 operations h_if_pos simp at h_tail_sum_lt_0 use i have h_harder_inductive_hypothesis_false: βˆ€ h, βˆ€ ops, h β‰₯ 0 β†’ implementation.check ops h = false β†’ βˆ€ i, i ≀ ops.length β†’ h + (ops.take i).sum β‰₯ 0 := by intros h ops h_ge_0 h_check induction' ops generalizing h intros i h_i_lt_tail_len simp [implementation.check] at h_check simp linarith rename_i head tail ih simp [implementation.check] at h_check by_cases h_head_lt_0 : h + head < 0 simp at h_head_lt_0 cases h_check linarith simp at h_head_lt_0 apply And.right at h_check specialize ih (h + head) simp at ih simp [h_head_lt_0, h_check] at ih intros i h_i_lt_tail_len simp at h_i_lt_tail_len simp specialize ih (i - 1) by_cases h_i_1_lt_i: 1 ≀ i let i' := i - 1 have h_i'_lt_tail_len: i = i' + 1 := by simp [i'] rw [Nat.sub_add_cancel] linarith simp [h_i'_lt_tail_len] rw [←Int.add_assoc] have h_i_minus_1_lt_tail_len: i - 1 ≀ tail.length := by linarith simp [h_i_minus_1_lt_tail_len] at ih simp [i'] assumption simp at h_i_1_lt_i simp [h_i_1_lt_i] at ih simp [h_i_1_lt_i] linarith simp at h_if_pos simp [h_if_pos] have h_harder' := h_harder_inductive_hypothesis_false 0 operations simp at h_harder' simp [h_if_pos] at h_harder' intros i h_i_lt_tail_len apply h_harder' assumption
null
null
null
def mean_absolute_deviation(numbers: List[float]) -> float
For a given list of input numbers, calculate Mean Absolute Deviation around the mean of this dataset. Mean Absolute Deviation is the average absolute difference between each element and a centerpoint (mean in this case): MAD = average | x - x_mean |
[{"input": [1.0, 2.0, 3.0, 4.0], "expected_output": 1.0}]
def mean_absolute_deviation(numbers: List[float]) -> float """For a given list of input numbers, calculate Mean Absolute Deviation around the mean of this dataset. Mean Absolute Deviation is the average absolute difference between each element and a centerpoint (mean in this case): MAD = average | x - x_mean | """
def problem_spec -- function signature (implementation: List Rat β†’ Rat) -- inputs (numbers: List Rat) := -- spec let spec (result: Rat):= 0 < numbers.length β†’ 0 ≀ result ∧ result * numbers.length * numbers.length = (numbers.map (fun x => |x * numbers.length - numbers.sum|)).sum; -- program terminates βˆƒ result, implementation numbers = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (implementation: List Rat β†’ Rat) -- inputs (numbers: List Rat) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ numbers, problem_spec implementation numbers) ↔ (βˆ€ numbers, generated_spec implementation numbers) :=
sorry
def implementation (numbers: List Rat) : Rat :=
if numbers.length = 0 then 1.0 else let mean := numbers.sum / numbers.length; let abs_diffs := numbers.map (fun x => |x - mean|); abs_diffs.sum / abs_diffs.length
#test implementation [1.0, 2.0, 3.0, 4.0] = 1.0
theorem correctness (numbers: List Rat) : problem_spec implementation numbers :=
by -- sometimes we have to create a temporary variable to use in the proof unfold problem_spec let result := implementation numbers use result simp [result] simp [implementation] by_cases (numbers.length = 0) rename_i h simp [h] rename_i h simp at h have h1: 0 < numbers.length := by by_contra rename_i h2 simp at h2 contradiction simp [h1] simp [h] sorry
null
null
null
def intersperse(numbers: List[int], delimeter: int) -> List[int]
Insert a number 'delimeter' between every two consecutive elements of input list `numbers'
[{"input": [[], 4], "expected_output": []}, {"input": [[1, 2, 3], 4], "expected_output": [1, 4, 2, 4, 3]}]
def intersperse(numbers: List[int], delimeter: int) -> List[int] """Insert a number 'delimeter' between every two consecutive elements of input list `numbers' """
def problem_spec -- function signature (implementation: List Int β†’ Int β†’ List Int) -- inputs (numbers: List Int) (delimeter: Int) := -- spec let spec (result: List Int) := (result.length = 0 ∧ result = numbers) ∨ (result.length = 2 ∧ numbers.length = 1 ∧ result[0]! = numbers[0]! ∧ result[1]! = delimeter) ∨ (result.length = 2 * numbers.length - 1 ∧ βˆ€ i, i < numbers.length β†’ result[2 * i]! = numbers[i]! ∧ (0 < 2*i - 1 β†’ result[2 * i - 1]! = delimeter)); -- program termination βˆƒ result, implementation numbers delimeter = result ∧ spec result
def generated_spec -- function signature (implementation: List Int β†’ Int β†’ List Int) -- inputs (numbers: List Int) (delimeter: Int) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ numbers delimeter, problem_spec implementation numbers delimeter) ↔ (βˆ€ numbers delimeter, generated_spec implementation numbers delimeter) :=
sorry
def implementation (numbers: List Int) (delimeter: Int) : List Int :=
numbers.intersperse delimeter
#test implementation [1, 2, 3] 4 = [1, 4, 2, 4, 3] #test implementation [] 4 = [] #test implementation [1] 4 = [1]
theorem correctness (numbers: List Int) (delimeter: Int) : problem_spec implementation numbers delimeter :=
by unfold problem_spec let result := implementation numbers delimeter use result simp [result] sorry
null
null
null
def parse_nested_parens(paren_string: str) -> List[int]
Input to this function is a string represented multiple groups for nested parentheses separated by spaces. For each of the group, output the deepest level of nesting of parentheses. E.g. (()()) has maximum two levels of nesting while ((())) has three.
[{"input": "(()()) ((())) () ((())()())", "expected_output": [2, 3, 1, 3]}]
def parse_nested_parens(paren_string: str) -> List[int] """Input to this function is a string represented multiple groups for nested parentheses separated by spaces. For each of the group, output the deepest level of nesting of parentheses. E.g. (()()) has maximum two levels of nesting while ((())) has three. """
def problem_spec -- function signature (implementation: String β†’ List Int) -- inputs (paren_string: String) := -- spec let spec (result: List Int) := let paren_space_split := paren_string.split (fun x => x = ' '); result.length = paren_space_split.length ∧ βˆ€ i, i < result.length β†’ let group := paren_space_split[i]!; balanced_paren_non_computable group '(' ')' β†’ 0 < result[i]! ∧ count_max_paren_depth group = result[i]!.toNat; -- program termination βˆƒ result, implementation paren_string = result ∧ spec result
def generated_spec -- function signature (implementation: String β†’ List Int) -- inputs (paren_string: String) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ paren_string, problem_spec implementation paren_string) ↔ (βˆ€ paren_string, generated_spec implementation paren_string) :=
sorry
def implementation (paren_string: String) : List Int :=
(paren_string.split (fun x => x = ' ')).map (fun x => count_max_paren_depth x)
#test implementation "(()()) ((())) () ((())()())" = [2, 3, 1, 3]
theorem correctness (paren_string: String) : problem_spec implementation paren_string :=
by unfold problem_spec let result := implementation paren_string use result simp [result] 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
null
null
def filter_by_substring(strings: List[str], substring: str) -> List[str]
Filter an input list of strings only for ones that contain given substring
[{"input": [[], "a"], "expected_output": []}, {"input": [["abc", "bacd", "cde", "array"], "a"], "expected_output": ["abc", "bacd", "array"]}]
def filter_by_substring(strings: List[str], substring: str) -> List[str] """Filter an input list of strings only for ones that contain given substring """
def problem_spec -- function signature (implementation: List String β†’ String β†’ List String) -- inputs (strings: List String) (substring: String) := -- spec let spec (result: List String) := (βˆ€ i, i < result.length β†’ result[i]!.containsSubstr substring β†’ βˆ€ j, j < strings.length ∧ strings[j]!.containsSubstr substring β†’ strings[j]! ∈ result β†’ βˆ€ j, j < result.length β†’ result.count result[j]! = strings.count result[j]!); -- program termination βˆƒ result, implementation strings substring = result ∧ spec result
def generated_spec -- function signature (implementation: List String β†’ String β†’ List String) -- inputs (strings: List String) (substring: String) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ strings substring, problem_spec implementation strings substring) ↔ (βˆ€ strings substring, generated_spec implementation strings substring) :=
sorry
def implementation (strings: List String) (substring: String): List String :=
strings.filter (fun x => x.containsSubstr substring)
#test implementation [] "a" = [] #test implementation ["abc", "bacd", "cde", "array"] "a" = ["abc", "bacd", "array"]
theorem correctness (strings: List String) (substring: String) : problem_spec implementation strings substring :=
by unfold problem_spec let result := implementation strings substring use result simp [result] sorry
null
null
null
def sum_product(numbers: List[int]) -> Tuple[int, int]
For a given list of integers, return a tuple consisting of a sum and a product of all the integers in a list. Empty sum should be equal to 0 and empty product should be equal to 1.
[{"input": [], "expected_output": "(0, 1)"}, {"input": [1, 2, 3, 4], "expected_output": "(10, 24)"}]
def sum_product(numbers: List[int]) -> Tuple[int, int] """For a given list of integers, return a tuple consisting of a sum and a product of all the integers in a list. Empty sum should be equal to 0 and empty product should be equal to 1. """
def problem_spec -- function signature (implementation: List Int β†’ (Int Γ— Int)) -- inputs (numbers: List Int) := -- spec let spec (result: (Int Γ— Int)) := let (sum, prod) := result; (numbers = [] β†’ sum = 0 ∧ prod = 1) ∧ (numbers β‰  [] β†’ (let (sum_tail, prod_tail) := implementation numbers.tail; sum - sum_tail = numbers[0]! ∧ sum_tail * prod_tail + prod = sum * prod_tail)); -- program termination βˆƒ result, implementation numbers = result ∧ spec result
def generated_spec -- function signature (implementation: List Int β†’ (Int Γ— Int)) -- inputs (numbers: List Int) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ numbers, problem_spec implementation numbers) ↔ (βˆ€ numbers, generated_spec implementation numbers) :=
sorry
def implementation (numbers: List Int) : (Int Γ— Int) :=
(numbers.sum, numbers.prod)
#test implementation [] = (0, 1) #test implementation [1, 2, 3, 4] = (10, 24)
theorem correctness (numbers: List Int) : problem_spec implementation numbers :=
by unfold problem_spec let result := implementation numbers use result simp [result] simp [implementation] sorry
null
null
null
def sum_product(numbers: List[int]) -> Tuple[int, int]
From a given list of integers, generate a list of rolling maximum element found until given moment in the sequence.
[{"input": [1, 2, 3, 2, 3, 4, 2], "expected_output": [1, 2, 3, 3, 3, 4, 4]}]
def sum_product(numbers: List[int]) -> Tuple[int, int] """From a given list of integers, generate a list of rolling maximum element found until given moment in the sequence. """
def problem_spec -- function signature (implementation: List Int β†’ List Int) -- inputs (numbers: List Int) := -- spec let spec (result: List Int) := result.length = numbers.length ∧ βˆ€ i, i < numbers.length β†’ (result[i]! ∈ numbers.take (i + 1) ∧ βˆ€ j, j ≀ i β†’ numbers[j]! ≀ result[i]!); -- program termination βˆƒ result, implementation numbers = result ∧ spec result
def generated_spec -- function signature (implementation: List Int β†’ List Int) -- inputs (numbers: List Int) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ numbers, problem_spec implementation numbers) ↔ (βˆ€ numbers, generated_spec implementation numbers) :=
sorry
def implementation (numbers: List Int) : List Int :=
let rec rolling_max (numbers: List Int) (results: List Int) (acc: Int) : List Int := match numbers with | [] => results | n :: ns => let new_acc := max acc n let new_results := results ++ [new_acc] rolling_max ns new_results new_acc rolling_max numbers [] 0
#test implementation [1, 2, 3, 2, 3, 4, 2] = [1, 2, 3, 3, 3, 4, 4]
theorem correctness (numbers: List Int) : problem_spec implementation numbers :=
by unfold problem_spec let result := implementation numbers use result simp [result] simp [implementation] apply And.intro induction numbers simp simp [implementation.rolling_max] rename_i head tail ih unfold implementation.rolling_max simp -- At this point we will have to -- strengthen the induction hypothesis -- to prove the correctness of the implementation sorry sorry
null
null
null
def make_palindrome(string: str) -> str
Find the shortest palindrome that begins with a supplied string. Algorithm idea is simple: - Find the longest postfix of supplied string that is a palindrome. - Append to the end of the string reverse of a string prefix that comes before the palindromic suffix.
[{"input": "", "expected_output": ""}, {"input": "cat", "expected_output": "catac"}, {"input": "cata", "expected_output": "catac"}]
def make_palindrome(string: str) -> str """Find the shortest palindrome that begins with a supplied string. Algorithm idea is simple: - Find the longest postfix of supplied string that is a palindrome. - Append to the end of the string reverse of a string prefix that comes before the palindromic suffix. """
def problem_spec -- function signature (implementation: String β†’ String) -- inputs (string: String) := -- spec let spec (result: String) := is_palindrome result ∧ result.length β‰₯ string.length ∧ string.isPrefixOf result ∧ -- comprehensive check that the result is the shortest palindrome (βˆ€ (possible_palindrome: String), string.isPrefixOf possible_palindrome β†’ is_palindrome possible_palindrome β†’ result.length ≀ possible_palindrome.length); -- program termination βˆƒ result, implementation string = result ∧ spec result
def generated_spec -- function signature (implementation: String β†’ String) -- inputs (string: String) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ string, problem_spec implementation string) ↔ (βˆ€ string, generated_spec implementation string) :=
sorry
def implementation (string: String) : String :=
sorry
-- #test implementation "" = "" -- #test implementation "cat" = "catac" -- #test implementation "cata" = "catac"
theorem correctness (s: String) : problem_spec implementation s :=
by unfold problem_spec let result := implementation s use result simp [result] sorry
/-- name: is_palindrome use: | Helper to check if a string is a palindrome. problems: - 10 - 48 -/ def is_palindrome (s: String): Bool := s = s.toList.reverse.asString
null
null
def string_xor(a: str, b: str) -> str
Input are two strings a and b consisting only of 1s and 0s. Perform binary XOR on these inputs and return result also as a string.
[{"input": ["010", "110"], "expected_output": "100"}]
def string_xor(a: str, b: str) -> str """Input are two strings a and b consisting only of 1s and 0s. Perform binary XOR on these inputs and return result also as a string. """
def problem_spec -- function signature (implementation: String β†’ String β†’ String) -- inputs (a b: String) := -- spec let spec (result: String) := a.all (fun c => c = '0' ∨ c = '1') β†’ b.all (fun c => c = '0' ∨ c = '1') β†’ a.length = b.length β†’ result.length = a.length ∧ result.all (fun c => c = '0' ∨ c = '1') ∧ (βˆ€ i, i < a.length β†’ let i_pos := String.Pos.mk i; (a.get i_pos = b.get i_pos β†’ result.get i_pos = '0') ∧ (a.get i_pos β‰  b.get i_pos β†’ result.get i_pos = '1')); -- program termination βˆƒ result, implementation a b = result ∧ spec result
def generated_spec -- function signature (implementation: String β†’ String β†’ String) -- inputs (a b: String) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ a b, problem_spec implementation a b) ↔ (βˆ€ a b, generated_spec implementation a b) :=
sorry
def implementation (a b: String) : String :=
sorry
-- #test implementation "010" "110" = "100"
theorem correctness (a b: String) : problem_spec implementation a b :=
by unfold problem_spec let result := implementation a b use result simp [result] sorry
null
null
null
def longest(strings: List[str]) -> Optional[str]
Out of list of strings, return the longest one. Return the first one in case of multiple strings of the same length. Return None in case the input list is empty.
[{"input": [], "expected_output": "None"}, {"input": ["a", "b", "c"], "expected_output": "a"}, {"input": ["a", "bb", "ccc"], "expected_output": "ccc"}]
def longest(strings: List[str]) -> Optional[str] """Out of list of strings, return the longest one. Return the first one in case of multiple strings of the same length. Return None in case the input list is empty. """
def problem_spec -- function signature (implementation: List String β†’ Option String) -- inputs (strings: List String) := -- spec let spec (result: Option String) := (result = none ↔ strings.length = 0) ∨ (βˆƒ longest, result = some longest ∧ longest ∈ strings ∧ βˆ€ s, s ∈ strings β†’ s.length ≀ longest.length β†’ (βˆƒ i, i < strings.length ∧ strings[i]! = longest ∧ βˆ€ j < i, strings[j]!.length < longest.length)); -- program termination βˆƒ result, implementation strings = result ∧ spec result
def generated_spec -- function signature (implementation: List String β†’ Option String) -- inputs (strings: List String) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ strings, problem_spec implementation strings) ↔ (βˆ€ strings, generated_spec implementation strings) :=
sorry
def implementation (strings: List String) : Option String :=
if strings = [] then none else let longest := (strings.map (Ξ» s => s.length)).maximum.get! (strings.find? (Ξ» s => s.length = longest)).get!
#test implementation ["a", "b", "c"] = some "a" #test implementation ["a", "bb", "ccc"] = some "ccc" #test implementation [] = none
theorem correctness (strings: List String) : problem_spec implementation strings :=
by unfold problem_spec let result := implementation strings use result simp [result] sorry
null
null
null
def greatest_common_divisor(a: int, b: int) -> int
Return a greatest common divisor of two integers a and b
[{"input": [25, 15], "expected_output": 5}, {"input": [3, 5], "expected_output": 1}]
def greatest_common_divisor(a: int, b: int) -> int """Return a greatest common divisor of two integers a and b """
def problem_spec -- function signature (implementation: Int β†’ Int β†’ Int) -- inputs (a b: Int) := -- spec let spec (result: Int) := (result ∣ a) ∧ (result ∣ b) ∧ (βˆ€ (d': Int), (d' > 0) β†’ (d' ∣ a) β†’ (d' ∣ b) β†’ d' ≀ result); -- program termination βˆƒ result, implementation a b = result ∧ spec result
def generated_spec -- function signature (implementation: Int β†’ Int β†’ Int) -- inputs (a b: Int) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ a b, problem_spec implementation a b) ↔ (βˆ€ a b, generated_spec implementation a b) :=
sorry
def implementation (a b: Int) : Int :=
(Int.gcd a b)
#test implementation 25 15 = 5 #test implementation 3 5 = 1
theorem correctness (a b: Int) : problem_spec implementation a b :=
by unfold problem_spec let result := implementation a b use result simp [result] sorry
null
null
null
def all_prefixes(string: str) -> List[str]
Return list of all prefixes from shortest to longest of the input string
[{"input": "abc", "expected_output": ["a", "ab", "abc"]}]
def all_prefixes(string: str) -> List[str] """Return list of all prefixes from shortest to longest of the input string """
def problem_spec -- function signature (implementation: String β†’ List String) -- inputs (string: String) := -- spec let spec (result: List String) := result.length = string.length ∧ βˆ€ i, i < result.length β†’ result[i]! = string.take (i + 1); -- program termination βˆƒ result, implementation string = result ∧ spec result
def generated_spec -- function signature (implementation: String β†’ List String) -- inputs (string: String) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ string, problem_spec implementation string) ↔ (βˆ€ string, generated_spec implementation string) :=
sorry
def implementation (string: String) : List String :=
let rec aux (chars: List Char) (result: List String) (i: Nat) := if i < chars.length then aux chars (result ++ [(chars.take (i + 1)).asString]) (i + 1) else result; aux string.toList [] 0
#test implementation "abc" = ["a", "ab", "abc"]
theorem correctness (string: String) : problem_spec implementation string :=
by unfold problem_spec let result := implementation string use result simp [result] sorry
null
null
null
def string_sequence(n: int) -> str
Return a string containing space-delimited numbers starting from 0 upto n inclusive.
[{"input": 0, "expected_output": "0"}, {"input": 5, "expected_output": "0 1 2 3 4 5"}]
def string_sequence(n: int) -> str """Return a string containing space-delimited numbers starting from 0 upto n inclusive. """
def problem_spec -- function signature (implementation: Nat β†’ String) -- inputs (n: Nat) := -- spec let spec (result: String) := let result_nums := result.splitOn " "; result_nums.length = n + 1 ∧ βˆ€ i, i < n + 1 β†’ result_nums[i]! = i.repr; -- program termination βˆƒ result, implementation n = result ∧ spec result
def generated_spec -- function signature (implementation: Nat β†’ String) -- inputs (n: Nat) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ n, problem_spec implementation n) ↔ (βˆ€ n, generated_spec implementation n) :=
sorry
def implementation (n: Nat) : String :=
(String.join (List.map (fun i => String.append i.repr " ") (List.range n))).append (n.repr)
#test implementation 0 = "0" #test implementation 5 = "0 1 2 3 4 5"
theorem correctness (n: Nat) : problem_spec implementation n :=
by unfold problem_spec let result := implementation n use result simp [result] sorry
null
null
null
def count_distinct_characters(string: str) -> int
Given a string, find out how many distinct characters (regardless of case) does it consist of
[{"input": "xyzXYZ", "expected_output": 3}, {"input": "Jerry", "expected_output": 4}]
def count_distinct_characters(string: str) -> int """Given a string, find out how many distinct characters (regardless of case) does it consist of """
def problem_spec -- function signature (implementation: String β†’ Nat) -- inputs (string: String) := -- spec let spec (result: Nat) := let string_idx := {i: Nat | i < string.length}.toFinset let characters := string_idx.image (fun i => string.toList.get! i) let lowercase_characters := characters.image (fun c => c.toLower) result = lowercase_characters.card; -- program termination βˆƒ result, implementation string = result ∧ spec result
def generated_spec -- function signature (implementation: String β†’ Nat) -- inputs (string: String) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ string, problem_spec implementation string) ↔ (βˆ€ string, generated_spec implementation string) :=
sorry
def implementation (string: String) : Nat :=
let char_set := string.toLower.toList.toFinset char_set.card
#test implementation "xyzXYZ" = 3 #test implementation "Jerry" = 4
theorem correctness (string: String) : problem_spec implementation string :=
by unfold problem_spec let result := implementation string use result simp [result] simp [implementation] rcases string with ⟨data⟩ induction data simp have h1: "".toLower = "" := by unfold String.toLower unfold Char.toLower unfold String.map unfold String.mapAux simp intros contradiction simp [h1] rename_i head tail ih unfold String.toLower rw [String.map_eq] simp sorry
null
null
null
def parse_music(music_string: str) -> List[int]
Input to this function is a string representing musical notes in a special ASCII format. Your task is to parse this string and return list of integers corresponding to how many beats does each not last. Here is a legend: 'o' - whole note, lasts four beats 'o|' - half note, lasts two beats '.|' - quater note, lasts one beat
[{"input": "o o| .| o| o| .| .| .| .| o o", "expected_output": [4, 2, 1, 2, 2, 1, 1, 1, 1, 4, 4]}]
def parse_music(music_string: str) -> List[int] """Input to this function is a string representing musical notes in a special ASCII format. Your task is to parse this string and return list of integers corresponding to how many beats does each not last. Here is a legend: 'o' - whole note, lasts four beats 'o|' - half note, lasts two beats '.|' - quater note, lasts one beat """
def problem_spec -- function signature (implementation: String β†’ List Nat) -- inputs (string: String) := -- spec let not_map := fun | "o" => 4 | "o|" => 2 | ".|" => 1 | _ => 0; let spec (result: List Nat) := let space_split := string.splitOn " "; space_split.length = result.length ∧ βˆ€ i < result.length, not_map (space_split.get! i) = result.get! i; -- program termination βˆƒ result, implementation string = result ∧ spec result
def generated_spec -- function signature (implementation: String β†’ List Nat) -- inputs (string: String) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ string, problem_spec implementation string) ↔ (βˆ€ string, generated_spec implementation string) :=
sorry
def implementation (string: String) : List Nat :=
(string.splitOn " ").map (fun | "o" => 4 | "o|" => 2 | ".|" => 1 | _ => 0)
#test implementation "o o| .| o| o| .| .| .| .| o o" = [4, 2, 1, 2, 2, 1, 1, 1, 1, 4, 4]
theorem correctness (string: String) : problem_spec implementation string :=
by unfold problem_spec let result := implementation string use result simp [result] simp [implementation] intro i by_cases h_i_lt_len: (i < (string.splitOn " ").length) simp [h_i_lt_len] simp [h_i_lt_len]
null
null
null
def how_many_times(string: str, substring: str) -> int
Find how many times a given substring can be found in the original string. Count overlaping cases.
[{"input": ["", "a"], "expected_output": 0}, {"input": ["aaa", "a"], "expected_output": 3}, {"input": ["aaaa", "aa"], "expected_output": 3}]
def how_many_times(string: str, substring: str) -> int """Find how many times a given substring can be found in the original string. Count overlaping cases. """
def problem_spec -- function signature (implementation: String β†’ String β†’ Nat) -- inputs (string substring: String) := -- spec let spec (result: Nat) := (string.length < substring.length β†’ result = 0) ∧ (string.length = substring.length β†’ ((string = substring ↔ result = 1) ∧ (substring β‰  string ↔ result = 0))) ∧ (substring.length < string.length β†’ let subtring_start_idx := {i: Nat | i ≀ string.length - substring.length}; let substring_occurrences := {i ∈ subtring_start_idx | (string.take (i + substring.length)).drop i = substring }; result = substring_occurrences.toFinset.card); -- program termination βˆƒ result, implementation string substring = result ∧ spec result
def generated_spec -- function signature (implementation: String β†’ String β†’ Nat) -- inputs (string substring: String) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ string substring, problem_spec implementation string substring) ↔ (βˆ€ string substring, generated_spec implementation string substring) :=
sorry
def implementation (string: String) (substring: String) : Nat :=
(string.findAllSubstr substring).size
#test implementation "aaa" "a" = 3 #test implementation "aaaa" "aa" = 3 #test implementation "" "a" = 0 #test implementation "a" "" = 1 #test implementation "a" "a" = 1
theorem correctness (string: String) (substring: String) : problem_spec implementation string substring :=
by unfold problem_spec let result := implementation string substring use result simp [result] sorry
null
null
null
def sort_numbers(numbers: str) -> str
Input is a space-delimited string of numberals from 'zero' to 'nine'. Valid choices are 'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight' and 'nine'. Return the string with numbers sorted from smallest to largest
[{"input": "three one five", "expected_output": "one three five"}]
def sort_numbers(numbers: str) -> str """Input is a space-delimited string of numberals from 'zero' to 'nine'. Valid choices are 'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight' and 'nine'. Return the string with numbers sorted from smallest to largest """
def problem_spec -- function signature (implementation: String β†’ String) -- inputs (numbers: String) := -- spec let word_to_number_map : String β†’ Int := fun word => match word with | "zero" => 0 | "one" => 1 | "two" => 2 | "three" => 3 | "four" => 4 | "five" => 5 | "six" => 6 | "seven" => 7 | "eight" => 8 | "nine" => 9 | _ => -1; let is_sorted_asc : List Int β†’ Bool := fun numbers => let rec is_sorted_asc_helper : List Int β†’ Bool β†’ Bool := fun numbers is_sorted => match numbers with | [] => is_sorted | [x] => is_sorted | x::y::rest => if x <= y then is_sorted_asc_helper (y::rest) true else false; is_sorted_asc_helper numbers false; let spec (result: String) := let result_split := result.splitOn " "; let numbers_split := numbers.splitOn " "; let result_mapped_to_numbers := result_split.map word_to_number_map; let numbers_as_str_mapped_to_numbers := numbers_split.map word_to_number_map; Β¬ -1 ∈ numbers_as_str_mapped_to_numbers β†’ result_split.length = numbers_split.length ∧ (βˆ€ n, n ∈ numbers_as_str_mapped_to_numbers β†’ βˆƒ m, m ∈ result_mapped_to_numbers) ∧ (βˆ€ n, n ∈ result_mapped_to_numbers β†’ βˆƒ m, m ∈ numbers_as_str_mapped_to_numbers) ∧ (βˆ€ n, numbers_as_str_mapped_to_numbers.count n = result_mapped_to_numbers.count n) ∧ is_sorted_asc result_mapped_to_numbers = true; -- program termination βˆƒ result, implementation numbers = result ∧ spec result
def generated_spec -- function signature (implementation: String β†’ String) -- inputs (numbers: String) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ numbers, problem_spec implementation numbers) ↔ (βˆ€ numbers, generated_spec implementation numbers) :=
sorry
def implementation (numbers: String) : String :=
let word_to_number_map : String β†’ Int := fun word => match word with | "zero" => 0 | "one" => 1 | "two" => 2 | "three" => 3 | "four" => 4 | "five" => 5 | "six" => 6 | "seven" => 7 | "eight" => 8 | "nine" => 9 | _ => -1; let number_to_word_map : Int β†’ String := fun number => match number with | 0 => "zero" | 1 => "one" | 2 => "two" | 3 => "three" | 4 => "four" | 5 => "five" | 6 => "six" | 7 => "seven" | 8 => "eight" | 9 => "nine" | _ => "invalid"; let numbers_split := numbers.splitOn " "; let numbers_mapped_to_numbers := numbers_split.map word_to_number_map; let sorted_numbers := numbers_mapped_to_numbers.mergeSort; let sorted_numbers_mapped_to_words := sorted_numbers.map number_to_word_map; let join: List String β†’ String := fun words => let head := words.get! 0; let tail := words.drop 1; tail.foldl (fun acc word => acc ++ " " ++ word) head; join sorted_numbers_mapped_to_words
#test implementation "three one five" = "one three five"
theorem correctness (numbers: String) : problem_spec implementation numbers :=
by unfold problem_spec let result := implementation numbers use result simp [result] sorry
null
null
null
def find_closest_elements(numbers: List[float]) -> Tuple[float, float]
From a supplied list of numbers (of length at least two) select and return two that are the closest to each other and return them in order (smaller number, larger number).
[{"input": [1.0, 2.0, 3.0, 4.0, 5.0, 2.2], "expected_output": "(2.0, 2.2)"}, {"input": [1.0, 2.0, 3.0, 4.0, 5.0, 2.0], "expected_output": "(2.0, 2.0)"}]
def find_closest_elements(numbers: List[float]) -> Tuple[float, float] """From a supplied list of numbers (of length at least two) select and return two that are the closest to each other and return them in order (smaller number, larger number). """
def problem_spec -- function signature (implementation: List Rat β†’ (Rat Γ— Rat)) -- inputs (numbers: List Rat) := -- spec let spec (result: (Rat Γ— Rat)) := 2 ≀ numbers.length β†’ (let (smaller, larger) := result; let abs_diff := |larger - smaller|; smaller ≀ larger ∧ smaller ∈ numbers ∧ larger ∈ numbers ∧ (βˆ€ x y, x ∈ numbers β†’ y ∈ numbers β†’ abs_diff ≀ |x - y|) ∧ (smaller = larger β†’ 1 ≀ (numbers.filter (fun z => z = smaller)).length)); -- program termination βˆƒ result, implementation numbers = result ∧ spec result
def generated_spec -- function signature (implementation: List Rat β†’ (Rat Γ— Rat)) -- inputs (numbers: List Rat) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ numbers, problem_spec implementation numbers) ↔ (βˆ€ numbers, generated_spec implementation numbers) :=
sorry
def implementation (numbers: List Rat): (Rat Γ— Rat) :=
let n := numbers.length; let sorted_numbers := numbers.mergeSort; let min_diff := sorted_numbers.get! 1 - sorted_numbers.get! 0; let min_pair := (sorted_numbers.get! 0, sorted_numbers.get! 1); let rec loop (i: Nat) (min_diff: Rat) (min_pair: (Rat Γ— Rat)): (Rat Γ— Rat) := if i < n - 1 then let diff := sorted_numbers.get! (i + 1) - sorted_numbers.get! i; if diff < min_diff then loop (i + 1) diff (sorted_numbers.get! i, sorted_numbers.get! (i + 1)) else loop (i + 1) min_diff min_pair else min_pair; loop 1 min_diff min_pair
#test implementation [1.0, 2.0, 3.0, 4.0, 5.0, 2.2] = (2.0, 2.2) #test implementation [1.0, 2.0, 3.0, 4.0, 5.0, 2.0] = (2.0, 2.0)
theorem correctness (numbers: List Rat) : problem_spec implementation numbers :=
by unfold problem_spec let result := implementation numbers use result simp [result] sorry
null
null
null
def rescale_to_unit(numbers: List[float]) -> List[float]
Given list of numbers (of at least two elements), apply a linear transform to that list, such that the smallest number will become 0 and the largest will become 1
[{"input": [1.0, 2.0, 3.0, 4.0, 5.0], "expected_output": [0.0, 0.25, 0.5, 0.75, 1.0]}]
def rescale_to_unit(numbers: List[float]) -> List[float] """Given list of numbers (of at least two elements), apply a linear transform to that list, such that the smallest number will become 0 and the largest will become 1 """
def problem_spec -- function signature (implementation: List Rat β†’ List Rat) -- inputs (numbers: List Rat) := -- spec let spec (result: List Rat) := 2 ≀ numbers.length β†’ let min_elem := numbers.min?.get!; let max_elem := numbers.max?.get!; let range := max_elem - min_elem; result.length = numbers.length ∧ βˆ€ i, i < numbers.length β†’ (min_elem β‰  max_elem β†’ result[i]! = (numbers[i]! - min_elem) / range) ∧ (min_elem = max_elem β†’ result[i]! = 0); -- program termination βˆƒ result, implementation numbers = result ∧ spec result
def generated_spec -- function signature (implementation: List Rat β†’ List Rat) -- inputs (numbers: List Rat) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ numbers, problem_spec implementation numbers) ↔ (βˆ€ numbers, generated_spec implementation numbers) :=
sorry
def implementation (numbers: List Rat): List Rat :=
sorry
-- #test implementation [1.0, 2.0, 3.0, 4.0, 5.0] = [0.0, 0.25, 0.5, 0.75, 1.0]
theorem correctness (numbers: List Rat) : problem_spec implementation numbers :=
by unfold problem_spec let result := implementation numbers use result simp [result] sorry
null
null
null
def strlen(string: str) -> int
Return length of given string
[{"input": "", "expected_output": 0}, {"input": "abc", "expected_output": 3}]
def strlen(string: str) -> int """Return length of given string """
def problem_spec -- function signature (implementation: String β†’ Nat) -- inputs (string: String) := -- spec let spec (result: Nat) := -- every character in the string is counted once result = 0 ↔ string.isEmpty ∧ (0 < result β†’ result - 1 = implementation (string.drop 1)) -- program termination βˆƒ result, implementation string = result ∧ spec result
def generated_spec -- function signature (implementation: String β†’ Nat) -- inputs (string: String) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ string, problem_spec implementation string) ↔ (βˆ€ string, generated_spec implementation string) :=
sorry
def implementation (string: String): Nat :=
string.length
#test implementation "" = 0 #test implementation "abc" = 3
theorem correctness (string: String) : problem_spec implementation string :=
by unfold problem_spec let result := implementation string use result simp [result] sorry
null
null
null
def largest_divisor(n: int) -> int
For a given number n, find the largest number that divides n evenly, smaller than n
[{"input": 15, "expected_output": 5}]
def largest_divisor(n: int) -> int """For a given number n, find the largest number that divides n evenly, smaller than n """
def problem_spec -- function signature (implementation: Nat β†’ Nat) -- inputs (n: Nat) := -- spec let spec (result: Nat) := 0 < n β†’ 0 < result β†’ result ∣ n β†’ βˆ€ x, x ∣ n β†’ x β‰  n β†’ x ≀ result; -- program termination βˆƒ result, implementation n = result ∧ spec result
def generated_spec -- function signature (implementation: Nat β†’ Nat) -- inputs (n: Nat) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ n, problem_spec implementation n) ↔ (βˆ€ n, generated_spec implementation n) :=
sorry
def implementation (n: Nat) : Nat :=
let possible_divisors := (List.range (n / 2 + 1)).drop 1 let reversed_possible_divisors := List.reverse possible_divisors; Id.run do for i in reversed_possible_divisors do if n % i = 0 then return i return 1
#test implementation 15 = 5
theorem correctness (n: Nat) : problem_spec implementation n :=
by unfold problem_spec let result := implementation n use result simp [result] sorry
null
null
null
def factorize(n: int) -> List[int]
Return list of prime factors of given integer in the order from smallest to largest. Each of the factors should be listed number of times corresponding to how many times it appeares in factorization. Input number should be equal to the product of all factors
[{"input": 8, "expected_output": [2, 2, 2]}, {"input": 25, "expected_output": [5, 5]}, {"input": 70, "expected_output": [2, 5, 7]}]
def factorize(n: int) -> List[int] """Return list of prime factors of given integer in the order from smallest to largest. Each of the factors should be listed number of times corresponding to how many times it appeares in factorization. Input number should be equal to the product of all factors """
def problem_spec -- function signature (implementation: Nat β†’ List Nat) -- inputs (n: Nat) := -- spec let spec (result: List Nat) := 2 ≀ n β†’ (result.prod = n ∧ List.Sorted Nat.le result ∧ result.all (Ξ» i => n % i = 0 ∧ Nat.Prime i)); -- program termination βˆƒ result, implementation n = result ∧ spec result
def generated_spec -- function signature (implementation: Nat β†’ List Nat) -- inputs (n: Nat) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ n, problem_spec implementation n) ↔ (βˆ€ n, generated_spec implementation n) :=
sorry
def implementation (n: Nat) : List Nat :=
Id.run do let mut result := [] for num in List.range' 2 n.sqrt do let mut n' := n while n' % num = 0 do n' := n' / num result := result ++ [num] result
#test implementation 8 = [2, 2, 2] #test implementation 25 = [5, 5] #test implementation 70 = [2, 5, 7]
theorem correctness (n: Nat) : problem_spec implementation n :=
by unfold problem_spec let result := implementation n use result simp [result] simp [implementation, Id.run] unfold List.foldl intro h_2_lt_n sorry
null
null
null
def remove_duplicates(numbers: List[int]) -> List[int]
From a list of integers, remove all elements that occur more than once. Keep order of elements left the same as in the input.
[{"input": [1, 2, 3, 2, 4], "expected_output": [1, 3, 4]}]
def remove_duplicates(numbers: List[int]) -> List[int] """From a list of integers, remove all elements that occur more than once. Keep order of elements left the same as in the input. """
def problem_spec -- function signature (implementation: List Int β†’ List Int) -- inputs (numbers: List Int) := -- spec let spec (result: List Int) := (βˆ€ i: Int, i ∈ result β†’ numbers.count i = 1) ∧ (βˆ€ i: Int, i ∈ numbers β†’ numbers.count i = 1 β†’ i ∈ result) ∧ (βˆ€ i j : Nat, i < result.length β†’ j < result.length β†’ i < j β†’ βˆƒ ip jp : Nat, ip < jp ∧ result[i]! = numbers[ip]! ∧ result[j]! = numbers[jp]!) -- program termination βˆƒ result, implementation numbers = result ∧ spec result
def generated_spec -- function signature (implementation: List Int β†’ List Int) -- inputs (numbers: List Int) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ numbers, problem_spec implementation numbers) ↔ (βˆ€ numbers, generated_spec implementation numbers) :=
sorry
def implementation (numbers: List Int) : List Int :=
Id.run do let mut result := [] for number in numbers do if numbers.count number = 1 then result := result ++ [number] return result
#test implementation [1, 2, 3, 2, 4] = [1, 3, 4]
theorem correctness (numbers: List Int) : problem_spec implementation numbers :=
by unfold problem_spec let result := implementation numbers use result simp [result] sorry
null
null
null
def flip_case(string: str) -> str
For a given string, flip lowercase characters to uppercase and uppercase to lowercase.
[{"input": "Hello", "expected_output": "hELLO"}]
def flip_case(string: str) -> str """For a given string, flip lowercase characters to uppercase and uppercase to lowercase. """
def problem_spec -- function signature (implementation: String β†’ String) -- inputs (string: String) := -- spec let spec (result: String) := let chars_in_result := result.toList; let chars_in_string := string.toList; chars_in_result.length = string.length ∧ (βˆ€ i, i < chars_in_result.length β†’ let c := chars_in_result.get! i; let c' := chars_in_string.get! i; (c.isUpper β†’ c'.isLower) ∧ (c.isLower β†’ c'.isUpper) ∧ ((Β¬ c.isUpper ∧ Β¬ c.isLower) β†’ c = c') ); -- program termination βˆƒ result, implementation string = result ∧ spec result
def generated_spec -- function signature (implementation: String β†’ String) -- inputs (string: String) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ string, problem_spec implementation string) ↔ (βˆ€ string, generated_spec implementation string) :=
sorry
def implementation (string: String) : String :=
string.map (Ξ» c => if c.isUpper then c.toLower else c.toUpper)
#test implementation "Hello" = "hELLO"
theorem correctness (string: String) : problem_spec implementation string :=
by unfold problem_spec let result := implementation string use result simp [result] sorry
null
null
null
def concatenate(strings: List[str]) -> str
Concatenate list of strings into a single string
[{"input": [], "expected_output": ""}, {"input": ["a", "b", "c"], "expected_output": "abc"}]
def concatenate(strings: List[str]) -> str """Concatenate list of strings into a single string """
def problem_spec -- function signature (implementation: List String β†’ String) -- inputs (strings: List String) := -- spec let spec (result: String) := let result_chars := result.toList; result_chars.length = (strings.map (Ξ» s => s.length)).sum ∧ βˆ€ i, i < strings.length β†’ (let string_in_result := strings.get! i; let end_idx := ((strings.take (i + 1)).map (Ξ» s => s.length)).sum; let start_idx := end_idx - string_in_result.length; let corresponding_string_in_result := ((result_chars.take end_idx).drop start_idx).asString; corresponding_string_in_result = string_in_result); -- program termination βˆƒ result, implementation strings = result ∧ spec result
def generated_spec -- function signature (implementation: List String β†’ String) -- inputs (strings: List String) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ strings, problem_spec implementation strings) ↔ (βˆ€ strings, generated_spec implementation strings) :=
sorry
def implementation (strings: List String) : String :=
String.join strings
#test implementation [] = "" #test implementation ["a", "b", "c"] = "abc"
theorem correctness (strings: List String) : problem_spec implementation strings :=
by unfold problem_spec let result := implementation strings use result simp [result] apply And.intro simp [implementation] repeat sorry
null
null
null
def filter_by_prefix(strings: List[str], prefix: str) -> List[str]
Filter an input list of strings only for ones that start with a given prefix.
[{"input": [[], "a"], "expected_output": []}, {"input": [["abc", "bcd", "cde", "array"], "a"], "expected_output": ["abc", "array"]}]
def filter_by_prefix(strings: List[str], prefix: str) -> List[str] """Filter an input list of strings only for ones that start with a given prefix. """
def problem_spec -- function signature (implementation: List String β†’ String β†’ List String) -- inputs (strings: List String) (pref: String) := -- spec let spec (result: List String) := result.all (Ξ» s => s.startsWith pref) ∧ result.all (Ξ» s => s ∈ strings) ∧ strings.all (Ξ» s => s.startsWith pref β†’ s ∈ result) ∧ βˆ€ s : String, s ∈ result β†’ result.count s = strings.count s; -- program termination βˆƒ result, implementation strings pref = result ∧ spec result
def generated_spec -- function signature (implementation: List String β†’ String β†’ List String) -- inputs (strings: List String) (pref: String) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ strings pref, problem_spec implementation strings pref) ↔ (βˆ€ strings pref, generated_spec implementation strings pref) :=
sorry
def implementation (strings: List String) (pref: String) : List String :=
strings.filter (Ξ» s => s.startsWith pref)
#test implementation [] "a" = [] #test implementation ["abc", "bcd", "cde", "array"] "a" = ["abc", "array"]
theorem correctness (strings: List String) (pref: String) : problem_spec implementation strings pref :=
by unfold problem_spec let result := implementation strings pref use result simp [result] repeat sorry
null
null
null
def get_positive(l: list)
Return only positive numbers in the list.
[{"input": [-1, 2, -4, 5, 6], "expected_output": [2, 5, 6]}, {"input": [5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10], "expected_output": [5, 3, 2, 3, 9, 123, 1]}]
def get_positive(l: list) """Return only positive numbers in the list. """
def problem_spec -- function signature (implementation: List Int β†’ List Int) -- inputs (numbers: List Int) := -- spec let spec (result: List Int) := result.all (Ξ» x => x > 0 ∧ x ∈ numbers) ∧ numbers.all (Ξ» x => x > 0 β†’ x ∈ result) ∧ result.all (Ξ» x => result.count x = numbers.count x); -- program termination βˆƒ result, implementation numbers = result ∧ spec result
def generated_spec -- function signature (implementation: List Int β†’ List Int) -- inputs (numbers: List Int) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ numbers, problem_spec implementation numbers) ↔ (βˆ€ numbers, generated_spec implementation numbers) :=
sorry
def implementation (numbers: List Int): List Int :=
numbers.filter (Ξ» x => x > 0)
#test implementation [(-1), 2, (-4), 5, 6] = [2, 5, 6] #test implementation [5, 3, (-5), 2, (-3), 3, 9, 0, 123, 1, (-10)] = [5, 3, 2, 3, 9, 123, 1]
theorem correctness (numbers: List Int) : problem_spec implementation numbers :=
by unfold problem_spec let result := implementation numbers use result simp [result] repeat sorry
null
null
null
def is_prime(n)
Return true if a given number is prime, and false otherwise.
[{"input": 6, "output": false}, {"input": 101, "output": true}, {"input": 11, "output": true}, {"input": 13441, "output": true}, {"input": 61, "output": true}, {"input": 4, "output": false}, {"input": 1, "output": false}]
def is_prime(n) """Return true if a given number is prime, and false otherwise. """
def problem_spec -- function signature (implementation: Nat β†’ Bool) -- inputs (n: Nat) := -- spec let spec (result: Bool) := result ↔ Β¬ (βˆƒ k, 2 ≀ k ∧ k < n ∧ n % k = 0); -- program termination βˆƒ result, implementation n = result ∧ spec result
def generated_spec -- function signature (implementation: Nat β†’ Bool) -- inputs (n: Nat) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ n, problem_spec implementation n) ↔ (βˆ€ n, generated_spec implementation n) :=
sorry
def implementation (n: Nat): Bool :=
Nat.Prime n
#test implementation 6 = false #test implementation 101 = true #test implementation 11 = true #test implementation 13441 = true #test implementation 61 = true #test implementation 4 = false #test implementation 1 = false
theorem correctness (n: Nat) : problem_spec implementation n :=
by unfold problem_spec let result := implementation n use result simp [result] simp [implementation] sorry
null
null
null
def find_zero(xs: list)
xs are coefficients of a polynomial. find_zero find x such that poly(x) = 0. find_zero returns only only zero point, even if there are many. Moreover, find_zero only takes list xs having even number of coefficients and largest non zero coefficient as it guarantees a solution. Note(George): This problem has been modified from the original HumanEval spec because of Real is not a computable type, but a zero does not necessarily exist over the rationals.
[{"input": [1, 2], "output": -0.5}, {"input": [-6, 11, -6, 1], "output": 1.0}]
def find_zero(xs: list) """xs are coefficients of a polynomial. find_zero find x such that poly(x) = 0. find_zero returns only only zero point, even if there are many. Moreover, find_zero only takes list xs having even number of coefficients and largest non zero coefficient as it guarantees a solution. Note(George): This problem has been modified from the original HumanEval spec because of Real is not a computable type, but a zero does not necessarily exist over the rationals. """
def problem_spec -- function signature (implementation: List Rat β†’ Rat) -- inputs (xs: List Rat) := -- spec let spec (result: Rat) := let eps := (1: Rat) / 1000000; xs.length β‰₯ 1 β†’ xs.length % 2 = 0 β†’ βˆ€ poly : Polynomial Rat, poly.degree = some (xs.length - 1) β†’ (βˆ€ i, i ≀ xs.length - 1 β†’ poly.coeff i = xs.get! i) β†’ |poly.eval result| ≀ eps; -- program termination βˆƒ result, implementation xs = result ∧ spec result
def generated_spec -- function signature (implementation: List Rat β†’ Rat) -- inputs (xs: List Rat) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ xs, problem_spec implementation xs) ↔ (βˆ€ xs, generated_spec implementation xs) :=
sorry
def implementation (xs: List Rat) : Rat :=
let rec poly (xs: List Rat) (x: Rat) := xs.reverse.foldl (Ξ» acc a => acc * x + a) 0; let rec poly' (xs: List Rat) (x: Rat) := (xs.drop 1).reverse.foldl (Ξ» acc a => acc * x + a) 0; let rec eps := (1: Rat) / 1000000; let rec find_zero (xs: List Rat) (guess: Rat) (fuel: Nat) := let eval := poly xs guess; let eval' := poly' xs guess; if eval ≀ eps ∨ fuel = 0 then (guess, fuel) else let guess' := (eval' * guess - eval) / eval'; find_zero xs guess' (fuel - 1); (find_zero xs 1.0 1000000).1 -- Note: The above implementation can fail to converge in some cases. For example, -- on the test case [-6, 11, -6, 1] as the derivative of the polynomial -- can be zero at if the guess is 0. In such cases, the implementation will not -- converge. The implementation can be improved by using a better guess.
#test implementation [1, 2] = -0.5 #test implementation [-6, 11, -6, 1] = 1.0
theorem correctness (xs: List Rat) : problem_spec implementation xs :=
by unfold problem_spec let result := implementation xs use result simp [result] intros h1 h2 poly h3 h4 repeat sorry
null
null
null
def sort_third(l: list)
This function takes a list l and returns a list l' such that l' is identical to l in the indicies that are not divisible by three, while its values at the indicies that are divisible by three are equal to the values of the corresponding indicies of l, but sorted.
[{"input": [1, 2, 3], "output": [1, 2, 3]}, {"input": [5, 6, 3, 4, 8, 9, 2], "output": [2, 6, 3, 4, 8, 9, 5]}]
def sort_third(l: list) """This function takes a list l and returns a list l' such that l' is identical to l in the indicies that are not divisible by three, while its values at the indicies that are divisible by three are equal to the values of the corresponding indicies of l, but sorted. """
def problem_spec -- function signature (implementation: List Int β†’ List Int) -- inputs (l: List Int) := -- spec let spec (result: List Int) := l.length = result.length ∧ let every_third_idx := (List.range l.length).filter (Ξ» i => i % 3 = 0); let every_third_val_in_result := every_third_idx.map (Ξ» i => result.get! i); let every_third_val := every_third_idx.map (Ξ» i => l.get! i); (βˆ€ i, i < l.length β†’ (i % 3 β‰  0 β†’ l.get! i = result.get! i)) ∧ List.Sorted Int.le every_third_val_in_result ∧ every_third_val.all (Ξ» x => every_third_val_in_result.count x = every_third_val.count x); -- program termination βˆƒ result, implementation l = result ∧ spec result
def generated_spec -- function signature (implementation: List Int β†’ List Int) -- inputs (l: List Int) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ l, problem_spec implementation l) ↔ (βˆ€ l, generated_spec implementation l) :=
sorry
def implementation (l: List Int) : List Int :=
let every_third_idx := (List.range l.length).filter (Ξ» i => i % 3 = 0); let every_third_val := every_third_idx.map (Ξ» i => l.get! i); let every_third_val_sorted := List.mergeSort every_third_val; let result := l.mapIdx (Ξ» i v => if i % 3 = 0 then every_third_val_sorted.get! (i / 3) else v); result
#test implementation [1, 2, 3] = [1, 2, 3] #test implementation [5, 6, 3, 4, 8, 9, 2] = [2, 6, 3, 4, 8, 9, 5]
theorem correctness (l: List Int) : problem_spec implementation l :=
by unfold problem_spec let result := implementation l use result simp [result] repeat sorry
null
null
null
def unique(l: list)
Return sorted unique elements in a list.
[{"input": [5, 3, 5, 2, 3, 3, 9, 0, 123], "output": [0, 2, 3, 5, 9, 123]}]
def unique(l: list) """Return sorted unique elements in a list. """
def problem_spec -- function signature (implementation: List Int β†’ List Int) -- inputs (l: List Int) := -- spec let spec (result: List Int) := (βˆ€ x, x ∈ result ↔ x ∈ l ∧ result.count x = 1) ∧ List.Sorted Int.le result -- program termination βˆƒ result, implementation l = result ∧ spec result
def generated_spec -- function signature (implementation: List Int β†’ List Int) -- inputs (l: List Int) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ l, problem_spec implementation l) ↔ (βˆ€ l, generated_spec implementation l) :=
sorry
def implementation (l: List Int) : List Int :=
l.eraseDup.mergeSort
#test implementation [5, 3, 5, 2, 3, 3, 9, 0, 123] = [0, 2, 3, 5, 9, 123]
theorem correctness (l: List Int) : problem_spec implementation l :=
by unfold problem_spec let result := implementation l use result simp [result] simp [implementation] apply And.intro intro x apply Iff.intro intro h apply And.intro repeat sorry
null
null
null
def max_element(l: list)
Return maximum element in the list.
[{"input": [1, 2, 3], "output": 3}, {"input": [5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10], "output": 123}]
def max_element(l: list) """Return maximum element in the list. """
def problem_spec -- function signature (implementation: List Int β†’ Int) -- inputs (l: List Int) := -- spec let spec (result: Int) := l.length > 0 β†’ ((βˆ€ i, i < l.length β†’ l.get! i ≀ result) ∧ (βˆƒ i, i < l.length ∧ l.get! i = result)); -- program termination βˆƒ result, implementation l = result ∧ spec result
def generated_spec -- function signature (implementation: List Int β†’ Int) -- inputs (l: List Int) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ l, problem_spec implementation l) ↔ (βˆ€ l, generated_spec implementation l) :=
sorry
def implementation (l: List Int) : Int :=
match List.maximum l with | none => panic! "empty list" | some x => x
#test implementation [1, 2, 3] = 3 #test implementation [5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10] = 123
theorem correctness (l: List Int) : problem_spec implementation l :=
by unfold problem_spec let result := implementation l use result simp [result] simp [implementation] by_cases h_l_not_nil: l β‰  [] have h_max_not_none := List.maximum_ne_bot_of_ne_nil h_l_not_nil intro h_len_gt_zero apply And.intro have h_l_max : βˆƒ r, l.maximum = some r := by have h_l_max' : l.maximum β‰  none := h_max_not_none rw [Option.ne_none_iff_exists] at h_l_max' obtain ⟨r, h_l_max⟩ := h_l_max' use r simp [h_l_max] obtain ⟨r, h_l_max ⟩ := h_l_max simp [h_l_max] induction l intro i h_i_lt_len simp simp at h_len_gt_zero rename_i head tail ih rw [List.maximum_cons] at h_l_max by_cases h_tail_nil: tail = [] simp [h_tail_nil] at * have h_head_eq_r : head = r := by simp [WithBot.some] at h_l_max rw [Option.some_inj] at h_l_max exact h_l_max rw [h_head_eq_r] simp [h_tail_nil] at ih have h_0_lt_len: 0 < tail.length := List.length_pos_of_ne_nil h_tail_nil simp [h_0_lt_len] at ih repeat sorry
null
null
null
def fizz_buzz(n: int)
Return the number of times the digit 7 appears in integers less than n which are divisible by 11 or 13.
[{"input": 50, "output": 0}, {"input": 78, "output": 2}, {"input": 79, "output": 3}]
def fizz_buzz(n: int) """Return the number of times the digit 7 appears in integers less than n which are divisible by 11 or 13. """
def problem_spec -- function signature (implementation: Nat β†’ Nat) -- inputs (n: Nat) := -- spec let spec (result: Nat) := (n = 0 β†’ result = 0) ∧ (0 < n β†’ result = implementation (n - 1) β†’ (n % 11 β‰  0 ∧ n % 13 β‰  0) ∨ n.repr.count '7' = 0) ∧ (0 < n β†’ result β‰  implementation (n - 1) β†’ (n % 11 = 0 ∨ n % 13 = 0) ∧ result - implementation (n - 1) = n.repr.count '7') -- program termination βˆƒ result, implementation n = result ∧ spec result
def generated_spec -- function signature (implementation: Nat β†’ Nat) -- inputs (n: Nat) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ n, problem_spec implementation n) ↔ (βˆ€ n, generated_spec implementation n) :=
sorry
def implementation (n: Nat) : Nat :=
let all_numbers_lt_n := {x | x < n}; let multiples_of_11_or_13 := all_numbers_lt_n ∩ {x | x % 11 = 0 ∨ x % 13 = 0}; let possible_multiple_with_7 := multiples_of_11_or_13 ∩ {x | x.repr.contains '7'}; possible_multiple_with_7.toFinset.sum (λ x => x.repr.count '7')
#test implementation 50 = 0 #test implementation 78 = 2 #test implementation 79 = 3
theorem correctness (n: Nat) : problem_spec implementation n :=
by unfold problem_spec let result := implementation n use result simp [result] simp [implementation] sorry
null
null
null
def sort_even(l: list)
This function takes a list l and returns a list l' such that l' is identical to l in the odd indicies, while its values at the even indicies are equal to the values of the even indicies of l, but sorted.
[{"input": [1, 2, 3], "output": [1, 2, 3]}, {"input": [5, 6, 3, 4], "output": [3, 6, 5, 4]}]
def sort_even(l: list) """This function takes a list l and returns a list l' such that l' is identical to l in the odd indicies, while its values at the even indicies are equal to the values of the even indicies of l, but sorted. """
def problem_spec -- function signature (implementation: List Int β†’ List Int) -- inputs (l: List Int) := -- spec let spec (result: List Int) := l.length = result.length ∧ let even_idx := (List.range l.length).filter (Ξ» i => i % 2 = 0); let even_val_in_result := even_idx.map (Ξ» i => result.get! i); let even_val := even_idx.map (Ξ» i => l.get! i); (βˆ€ i, i < l.length β†’ (i % 2 β‰  0 β†’ l.get! i = result.get! i)) ∧ List.Sorted Int.le even_val_in_result ∧ even_val.all (Ξ» x => even_val_in_result.count x = even_val.count x); -- program termination βˆƒ result, implementation l = result ∧ spec result
def generated_spec -- function signature (implementation: List Int β†’ List Int) -- inputs (l: List Int) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ l, problem_spec implementation l) ↔ (βˆ€ l, generated_spec implementation l) :=
sorry
def implementation (l: List Int) : List Int :=
let even_idx := (List.range l.length).filter (Ξ» i => i % 2 = 0); let even_val := even_idx.map (Ξ» i => l.get! i); let even_val_sorted := List.mergeSort even_val; let result := l.mapIdx (Ξ» i v => if i % 2 = 0 then even_val_sorted.get! (i / 2) else v); result
#test implementation [1, 2, 3] = [1, 2, 3] #test implementation [5, 6, 3, 4] = [3, 6, 5, 4]
theorem correctness (l: List Int) : problem_spec implementation l :=
by unfold problem_spec let result := implementation l use result simp [result] sorry
null
null
null
def encode_cyclic(s: str) -> str
Returns an encoded string by cycling each group of three consecutive characters. Specifically, each group of exactly three characters 'abc' is transformed to 'bca'. Groups of fewer than three characters at the end of the string remain unchanged.
[{"input": "abcdef", "expected_output": "bcaefd"}, {"input": "abcde", "expected_output": "bcade"}, {"input": "ab", "expected_output": "ab"}]
def encode_cyclic(s: str) -> str """Returns an encoded string by cycling each group of three consecutive characters. Specifically, each group of exactly three characters 'abc' is transformed to 'bca'. Groups of fewer than three characters at the end of the string remain unchanged. """
def problem_spec (impl: String β†’ String) (s: String) := let n := s.length; let extract (chars: List Char) (start_index: β„•) (end_index: β„•) := (chars.drop start_index).take (end_index - start_index + 1); let spec (result: String) := let encoded_chars := result.toList; let original_chars := s.toList; encoded_chars.length = n ∧ (βˆ€ i : β„•, i * 3 + 3 ≀ n β†’ extract encoded_chars (i * 3) (i * 3 + 2) = [original_chars.get! (i * 3 + 1), original_chars.get! (i * 3 + 2), original_chars.get! (i * 3)]) ∧ (n % 3 β‰  0 β†’ extract encoded_chars (n - n % 3) (n - 1) = extract original_chars (n - n % 3) (n - 1)); -- program termination βˆƒ result, impl 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 "abcdef" = "bcaefd" -- #test implementation "abcde" = "bcade" -- #test implementation "ab" = "ab"
theorem correctness (s: String) : problem_spec implementation s :=
by unfold problem_spec let result := implementation s use result simp [result] sorry
null
null
null
def prime_fib(n: int)
prime_fib returns n-th prime Fibonacci number. Note(George): A proof of this problem requires the resolution of the open conjecture: there are infinitely many prime Fibonacci numbers.
[{"input": 1, "output": 2}, {"input": 2, "output": 3}, {"input": 3, "output": 5}, {"input": 4, "output": 13}, {"input": 5, "output": 89}]
def prime_fib(n: int) """prime_fib returns n-th prime Fibonacci number. Note(George): A proof of this problem requires the resolution of the open conjecture: there are infinitely many prime Fibonacci numbers. """
def problem_spec -- function signature (implementation: Nat β†’ Nat) -- inputs (n: Nat) := -- spec let spec (result: Nat) := n > 0 β†’ (βˆƒ i, Nat.fib i = result ∧ Nat.Prime result ∧ (βˆƒ! S : Finset Nat, S.card = n - 1 ∧ (βˆ€ y ∈ S, (βˆƒ k, y = Nat.fib k) ∧ y < result ∧ Nat.Prime y)) ) -- program termination βˆƒ result, implementation n = result ∧ spec result
def generated_spec -- function signature (implementation: Nat β†’ Nat) -- inputs (n: Nat) : Prop :=
theorem spec_isomorphism: βˆ€ implementation, (βˆ€ n, problem_spec implementation n) ↔ (βˆ€ n, generated_spec implementation n) :=
sorry
def implementation (n: Nat) : Nat :=
let rec fib_prime (n: Nat) (i: Nat) : Nat := if Nat.Prime (Nat.fib i) then if n = 1 ∨ n = 0 then Nat.fib i else fib_prime (n - 1) (i + 1) else fib_prime n (i + 1) termination_by n decreasing_by -- Really hard to prove termination here sorry sorry fib_prime n 0
-- NOTE we changed to #eval! instead of #test -- because we can't use #test for implementation -- without a proof of termination #eval! implementation 1 = 2 #eval! implementation 2 = 3 #eval! implementation 3 = 5 #eval! implementation 4 = 13 #eval! implementation 5 = 89
theorem correctness (n: Nat) : problem_spec implementation n :=
by unfold problem_spec let result := implementation n use result simp [result] repeat sorry
null
null
null
def triples_sum_to_zero(numbers: List[int]) -> Bool
triples_sum_to_zero takes a list of integers as an input. it returns True if there are three distinct elements in the list that sum to zero, and False otherwise.
[{"input": [1, 3, 5, 0], "expected_output": false}, {"input": [1, 3, -2, 1], "expected_output": true}]
def triples_sum_to_zero(numbers: List[int]) -> Bool """triples_sum_to_zero takes a list of integers as an input. it returns True if there are three distinct elements in the list that sum to zero, and False otherwise. """
def problem_spec -- function signature (implementation: List Int β†’ Bool) -- inputs (numbers: List Int) := let sum_i_j_k (i j k: Nat) : Bool := numbers[i]! + numbers[j]! + numbers[k]! = 0; let exists_zero := 3 ≀ numbers.length ∧ (βˆƒ i j k, i β‰  j ∧ i β‰  k ∧ j β‰  k ∧ i < numbers.length ∧ j < numbers.length ∧ k < numbers.length ∧ sum_i_j_k i j k) -- spec let spec (result: Bool) := result ↔ exists_zero -- -- program termination βˆƒ result, implementation numbers = result ∧ spec result --
def generated_spec -- function signature (impl: List Int β†’ Bool) -- inputs (x: List Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ x, problem_spec impl x) ↔ (βˆ€ x, generated_spec impl x) :=
sorry
def implementation (numbers: List Int) : Bool :=
sorry
-- #test implementation [1, 3, 5, 0] = false -- #test implementation [1, 3, -2, 1] = true -- #test implementation [1, 2, 3, 7] = false -- #test implementation [2, 4, -5, 3, 9, 7] = true -- #test implementation [1] = false
theorem correctness (numbers : List Int) : problem_spec implementation numbers :=
by unfold problem_spec let result := implementation numbers use result simp [result] sorry
null
null
null
def car_race_collision(x: Nat) -> Nat
Imagine a road that's a perfectly straight infinitely long line. n cars are driving left to right; simultaneously, a different set of n cars are driving right to left. The two sets of cars start out being very far from each other. All cars move in the same speed. Two cars are said to collide when a car that's moving left to right hits a car that's moving right to left. However, the cars are infinitely sturdy and strong; as a result, they continue moving in their trajectory as if they did not collide. This function outputs the number of such collisions.
[{"input": 0, "expected_output": 0}, {"input": 5, "expected_output": 25}]
def car_race_collision(x: Nat) -> Nat """Imagine a road that's a perfectly straight infinitely long line. n cars are driving left to right; simultaneously, a different set of n cars are driving right to left. The two sets of cars start out being very far from each other. All cars move in the same speed. Two cars are said to collide when a car that's moving left to right hits a car that's moving right to left. However, the cars are infinitely sturdy and strong; as a result, they continue moving in their trajectory as if they did not collide. This function outputs the number of such collisions. """
def problem_spec -- function signature (implementation: Nat β†’ Nat) -- inputs (x : Nat) := -- spec let spec (result: Nat) := βˆƒ x_list : List Nat, x_list.length = x ∧ x_list.all (fun i => i = x) ∧ x_list.sum = result -- -- program termination βˆƒ result, implementation x = result ∧ spec result --
def generated_spec -- function signature (impl: Nat β†’ Nat) -- inputs (x: Nat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ x, problem_spec impl x) ↔ (βˆ€ x, generated_spec impl x) :=
sorry
def implementation (x : Nat) : Nat :=
sorry
-- #test implementation 0 = 0 -- #test implementation 5 = 25
theorem correctness (x : Nat) : problem_spec implementation x :=
by unfold problem_spec let result := implementation x use result simp [result] sorry
null
null
null
def incr_list(numbers: List[Int]) -> List[Int]
incr_list takes a list of integers as input and returns a new list where each element is incremented by 1.
[{"input": [], "expected_output": []}, {"input": [1, 3, -2, 1], "expected_output": [2, 4, -1, 2]}]
def incr_list(numbers: List[Int]) -> List[Int] """incr_list takes a list of integers as input and returns a new list where each element is incremented by 1. """
def problem_spec -- function signature (implementation: List Int β†’ List Int) -- inputs (numbers: List Int) := -- spec let spec (result: List Int) := (result.length = numbers.length) ∧ βˆ€ i, i < numbers.length β†’ result[i]! = numbers[i]! + 1 -- -- program termination βˆƒ result, implementation numbers = result ∧ spec result --
def generated_spec -- function signature (impl: List Int β†’ List Int) -- inputs (x: List Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ x, problem_spec impl x) ↔ (βˆ€ x, generated_spec impl x) :=
sorry
def implementation (numbers: List Int) : List Int :=
sorry
-- #test implementation [1, 2, 3] = [2, 3, 4] -- #test implementation [5, 3, 5, 2, 3, 3, 9, 0, 123] = [6, 4, 6, 3, 4, 4, 10, 1, 124]
theorem correctness (numbers : List Int) : problem_spec implementation numbers :=
by unfold problem_spec let result := implementation numbers use result simp [result] sorry
null
null
null
def pairs_sum_to_zero(numbers: List[int]) -> Bool
pairs_sum_to_zero takes a list of integers as an input. it returns True if there are two distinct elements in the list that sum to zero, and False otherwise.
[{"input": [1, 3, 5, 0], "expected_output": false}, {"input": [1, 3, -2, 1], "expected_output": false}, {"input": [1], "expected_output": false}, {"input": [2, 4, -5, 3, 5, 7], "expected_output": true}]
def pairs_sum_to_zero(numbers: List[int]) -> Bool """pairs_sum_to_zero takes a list of integers as an input. it returns True if there are two distinct elements in the list that sum to zero, and False otherwise. """
def problem_spec -- function signature (implementation: List Int β†’ Bool) -- inputs (numbers: List Int) := let sum_i_j (i j: Nat) : Bool := numbers[i]! + numbers[j]! = 0; let exists_zero := 2 ≀ numbers.length ∧ (βˆƒ i j, i β‰  j ∧ i < numbers.length ∧ j < numbers.length ∧ sum_i_j i j) -- spec let spec (result: Bool) := result ↔ exists_zero -- -- program termination βˆƒ result, implementation numbers = result ∧ spec result --
def generated_spec -- function signature (impl: List Int β†’ Bool) -- inputs (x: List Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ x, problem_spec impl x) ↔ (βˆ€ x, generated_spec impl x) :=
sorry
def implementation (numbers: List Int) : Bool :=
sorry
-- #test implementation [1, 3, 5, 0] = false -- #test implementation [1, 3, -2, 1] = false -- #test implementation [1, 2, 3, 7] = false -- #test implementation [2, 4, -5, 3, 5, 7] = true -- #test implementation [1] = false
theorem correctness (numbers : List Int) : problem_spec implementation numbers :=
by unfold problem_spec let result := implementation numbers use result simp [result] sorry
null
null
null
def change_base(x: Nat, base: Nat) -> String
Change numerical base of input number x to base. return string representation after the conversion. base numbers are less than 10.
[{"input": "(8, 3)", "expected_output": "22"}, {"input": "(8, 2)", "expected_output": "1000"}, {"input": "(7, 2)", "expected_output": "111"}]
def change_base(x: Nat, base: Nat) -> String """Change numerical base of input number x to base. return string representation after the conversion. base numbers are less than 10. """
def problem_spec -- function signature (implementation: Nat β†’ Nat -> String) -- inputs (x base: Nat) := -- spec let spec (result: String) := let result_array := result.toList.map (fun c => c.toNat - '0'.toNat); let pow_array := (List.range result_array.length).map (fun i => base^(result_array.length - i - 1) * result_array[i]!); let pow_sum := pow_array.sum; (0 < base ∧ base ≀ 10) ∧ (βˆ€ i, i < result_array.length β†’ result_array[i]! < base ∧ 0 ≀ result_array[i]! β†’ pow_sum = x); -- program termination βˆƒ result, implementation x base = result ∧ spec result
def generated_spec -- function signature (impl: Nat β†’ Nat β†’ String) -- inputs (x base: Nat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ x y, problem_spec impl x y) ↔ (βˆ€ x y, generated_spec impl x y) :=
sorry
def implementation (x base: Nat) : String :=
sorry
-- #test implementation 8 3 = '22' -- #test implementation 8 2 = '1000' -- #test implementation 7 2 = '111'
theorem correctness (x base : Nat) : problem_spec implementation x base :=
by unfold problem_spec let result := implementation x base use result simp [result] sorry
null
null
null
def triangle_area(a: float, h: float) -> float
Given length of a side and high return area for a triangle.
[{"input": "(5, 3)", "expected_output": 7.5}, {"input": "(8, 2)", "expected_output": 8.0}]
def triangle_area(a: float, h: float) -> float """Given length of a side and high return area for a triangle. """
def problem_spec -- function signature (implementation: Rat β†’ Rat -> Rat) -- inputs (a h: Rat) := -- spec let spec (result: Rat) := a = 0 β†’ result = 0 ∧ (a β‰  0 β†’ (2 * result) / a = h); -- -- program termination βˆƒ result, implementation a h = result ∧ spec result --
def generated_spec -- function signature (impl: Rat β†’ Rat β†’ Rat) -- inputs (a h: Rat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ x y, problem_spec impl x y) ↔ (βˆ€ x y, generated_spec impl x y) :=
sorry
def implementation (a h: Rat) : Rat :=
0.5 * a * h
-- #test implementation 5 3 = 7.5 -- #test implementation 8 2 = 8.0
theorem correctness (a h : Rat) : problem_spec implementation a h :=
by unfold problem_spec let result := implementation a h use result simp [result] sorry
null
null
null
def fib4(n: int)
The Fib4 number sequence is a sequence similar to the Fibbonacci sequnece that's defined as follows: fib4(0) -> 0 fib4(1) -> 0 fib4(2) -> 2 fib4(3) -> 0 fib4(n) -> fib4(n-1) + fib4(n-2) + fib4(n-3) + fib4(n-4). Please write a function to efficiently compute the n-th element of the fib4 number sequence. Do not use recursion.
[{"input": 5, "output": 4}, {"input": 6, "output": 8}, {"input": 7, "output": 14}]
def fib4(n: int) """The Fib4 number sequence is a sequence similar to the Fibbonacci sequnece that's defined as follows: fib4(0) -> 0 fib4(1) -> 0 fib4(2) -> 2 fib4(3) -> 0 fib4(n) -> fib4(n-1) + fib4(n-2) + fib4(n-3) + fib4(n-4). Please write a function to efficiently compute the n-th element of the fib4 number sequence. Do not use recursion. """
def problem_spec -- function signature (impl: Nat β†’ Nat) -- inputs (n: Nat) := -- spec let spec (result: Nat) := fibonacci_non_computable_4 n result -- program terminates βˆƒ result, impl n = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: Nat β†’ Nat) -- inputs (x: Nat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ x, problem_spec impl x) ↔ (βˆ€ x, generated_spec impl x) :=
sorry
def implementation (n: Nat) : Nat :=
sorry
-- #test implementation 5 = 4 -- #test implementation 6 = 8 -- #test implementation 7 = 14
theorem correctness (n: Nat) : problem_spec implementation n :=
by unfold problem_spec let result := implementation n use result simp [result] repeat sorry
/-- name: fibonacci_non_computable_4 use: | Non-computable definition to check if a number is a Fibonacci number such that fib(n) = fib(n - 1) + fib(n - 2) + fib(n - 3) + fib(n - 4). problems: - 46 -/ inductive fibonacci_non_computable_4 : β„• β†’ β„• β†’ Prop | base0 : fibonacci_non_computable_4 0 0 | base1 : fibonacci_non_computable_4 1 0 | base2 : fibonacci_non_computable_4 2 2 | base3 : fibonacci_non_computable_4 3 0 | step : βˆ€ n f₁ fβ‚‚ f₃ fβ‚„, fibonacci_non_computable_4 n f₁ β†’ fibonacci_non_computable_4 (n + 1) fβ‚‚ β†’ fibonacci_non_computable_4 (n + 2) f₃ β†’ fibonacci_non_computable_4 (n + 3) fβ‚„ β†’ fibonacci_non_computable_4 (n + 4) (f₁ + fβ‚‚ + f₃ + fβ‚„)
null
null
def median(numbers: List[float]) -> float
Return median of elements in the list l
[{"input": [3, 1, 2, 4, 5], "output": 3}, {"input": [-10, 4, 6, 1000, 10, 20], "output": 15.0}]
def median(numbers: List[float]) -> float """Return median of elements in the list l """
def problem_spec -- function signature (implementation: List Rat β†’ Rat) -- inputs (numbers: List Rat) := -- spec let spec (result: Rat) := 0 < numbers.length β†’ let less_eq := (numbers.filter (fun x => x ≀ result)); let more_eq := (numbers.filter (fun x => result ≀ x)); let max_more_eq := more_eq.max?; let min_less_eq := less_eq.min?; let less_eq_count := less_eq.length; let more_eq_count := more_eq.length; let eq_count := (numbers.filter (fun x => x = result)).length; (less_eq_count + more_eq_count - eq_count = numbers.length β†’ numbers.length ≀ 2 * less_eq_count β†’ numbers.length ≀ 2 * more_eq_count) ∧ ((numbers.length % 2 = 1 β†’ result ∈ numbers) ∧ (numbers.length % 2 = 0 β†’ max_more_eq.isSome ∧ min_less_eq.isSome ∧ 2 * result = max_more_eq.get! + min_less_eq.get!)); -- program termination βˆƒ result, implementation numbers = result ∧ spec result
def generated_spec -- function signature (impl: List Rat β†’ Rat) -- inputs (x: List Rat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ x, problem_spec impl x) ↔ (βˆ€ x, generated_spec impl x) :=
sorry
def implementation (numbers: List Rat) : Rat :=
sorry
-- #test implementation [3, 1, 2, 4, 5] = 3 -- #test implementation [-10, 4, 6, 1000, 10, 20] = 15.0
theorem correctness (numbers: List Rat) : problem_spec implementation numbers :=
by unfold problem_spec let result := implementation numbers use result simp [result] repeat sorry
null
null
null
def is_palindrome(string: str) -> Bool
Checks if given string is a palindrome
[{"input": "", "expected_output": true}, {"input": "aba", "expected_output": true}, {"input": "aaaaa", "expected_output": "True"}, {"input": "zbcd", "expected_output": "False"}]
def is_palindrome(string: str) -> Bool """Checks if given string is a palindrome """
def problem_spec -- function signature (implementation: String β†’ Bool) -- inputs (string: String) := -- spec let spec (result: Bool) := result ↔ is_palindrome string -- program termination βˆƒ result, implementation string = result ∧ spec result
def generated_spec -- function signature (impl: String β†’ Bool) -- inputs (x: String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ x, problem_spec impl x) ↔ (βˆ€ x, generated_spec impl x) :=
sorry
def implementation (string: String) : Bool :=
sorry
-- #test implementation "" = true -- #test implementation "aba" = true -- #test implementation "aaaaa" = true -- #test implementation "zbcd" = false
theorem correctness (s: String) : problem_spec implementation s :=
by unfold problem_spec let result := implementation s use result simp [result] sorry
/-- name: is_palindrome use: | Helper to check if a string is a palindrome. problems: - 10 - 48 -/ def is_palindrome (s: String): Bool := s = s.toList.reverse.asString
null
null
def modp(n: Nat, p: Nat) -> Nat
Return 2^n modulo p (be aware of numerics).
[{"input": [3, 5], "expected_output": 3}, {"input": [1101, 101], "expected_output": 2}, {"input": [0, 101], "expected_output": 0}, {"input": [100, 101], "expected_output": 1}]
def modp(n: Nat, p: Nat) -> Nat """Return 2^n modulo p (be aware of numerics). """
def problem_spec -- function signature (implementation: Nat β†’ Nat β†’ Nat) -- inputs (n p: Nat) := -- spec let spec (result: Nat) := 0 < p ∧ result < p ∧ (βˆƒ k : Nat, p * k + result = Nat.pow 2 n) -- program termination βˆƒ result, implementation n p = result ∧ spec result
def generated_spec -- function signature (impl: Nat β†’ Nat β†’ Nat) -- inputs (n p: Nat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ x y, problem_spec impl x y) ↔ (βˆ€ x y, generated_spec impl x y) :=
sorry
def implementation (n p: Nat) : Nat :=
sorry
-- #test implementation 3 5 = 3 -- #test implementation 1101 101 = 2 -- #test implementation 0 101 = 1 -- #test implementation 3 11 = 8 -- #test implementation 100 101 = 1
theorem correctness (n p: Nat) : problem_spec implementation n p :=
by unfold problem_spec let result := implementation n p use result simp [result] sorry
null
null
null
def encode_shift(s: String) -> String
returns encoded string by shifting every character by 5 in the alphabet.
[{"input": "abc", "expected_output": "fgh"}, {"input": "xyz", "expected_output": "cde"}, {"input": "aaa", "expected_output": "fff"}]
def encode_shift(s: String) -> String """returns encoded string by shifting every character by 5 in the alphabet. """
def problem_spec -- function signature (implementation: String β†’ String) -- inputs (s : String) := let isAlphabetic (string: String) : Bool := βˆ€ i, i < string.length β†’ let c := string.get! ⟨i⟩; ('a'.toNat ≀ c.toNat ∧ c.toNat ≀ 'z'.toNat) ∨ ('A'.toNat ≀ c.toNat ∧ c.toNat ≀ 'Z'.toNat) -- spec let spec (result: String) := isAlphabetic result ∧ isAlphabetic s ∧ result.length = s.length ∧ βˆƒ k : Nat, k < 26 ∧ βˆ€ i : Nat, i < s.length β†’ ((s.get! ⟨i⟩).toNat + k) % 26 = (result.get! ⟨i⟩).toNat β†’ k = 5 -- program termination βˆƒ result, implementation s = result ∧ spec result
def generated_spec -- function signature (impl: String β†’ String) -- inputs (s: String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ x, problem_spec impl x) ↔ (βˆ€ x, generated_spec impl x) :=
sorry
def implementation (s: String) : String :=
sorry
-- #test implementation "abc" = "fgh" -- #test implementation "xyz" = "cde" -- #test implementation "aaa" = "fff"
theorem correctness (s: String) : problem_spec implementation s :=
by unfold problem_spec let result := implementation s use result simp [result] sorry
null
null
null
def remove_vowels(string: str) -> string
remove_vowels is a function that takes string and returns string without vowels.
[{"input": "", "expected_output": ""}, {"input": "abcdef\nghijklm", "expected_output": "bcdf\nghjklm"}, {"input": "abcdef", "expected_output": "bcdf"}, {"input": "aaaaa", "expected_output": ""}, {"input": "aaBAA", "expected_output": "B"}]
def remove_vowels(string: str) -> string """remove_vowels is a function that takes string and returns string without vowels. """
def problem_spec -- function signature (implementation: String β†’ String) -- inputs (string: String) := let is_consonant (c: Char): Bool := let vowels := "aeiouAEIOU" not (vowels.contains c); -- spec let spec (result: String) := result.all (Ξ» c => is_consonant c) ∧ result.length ≀ string.length ∧ βˆ€ c, result.contains c β†’ string.contains c ∧ βˆ€ c , string.contains c ∧ is_consonant c β†’ (result.contains c); -- program termination βˆƒ result, implementation string = result ∧ spec result
def generated_spec -- function signature (impl: String β†’ String) -- inputs (string : String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ string, problem_spec impl string) ↔ (βˆ€ string, generated_spec impl string) :=
sorry
def implementation (string: String) : String :=
sorry
-- #test implementation "" = "" -- #test implementation "cat" = "catac" -- #test implementation "cata" = "catac"
theorem correctness (s: String) : problem_spec implementation s :=
by unfold problem_spec let result := implementation s use result simp [result] sorry
null
null
null
def below_threshold(numbers: List[Int], threshold: Int) -> bool
Return True if all numbers in the list l are below threshold t, and False otherwise.
[{"input": [[1, 2, 4, 10], 100], "expected_output": true}, {"input": [[1, 20, 4, 10], 5], "expected_output": false}]
def below_threshold(numbers: List[Int], threshold: Int) -> bool """Return True if all numbers in the list l are below threshold t, and False otherwise."""
def problem_spec -- function signature (impl: List Int β†’ Int β†’ Bool) -- inputs (numbers: List Int) (threshold: Int) := -- spec let numbers_below_threshold := βˆ€ i, i < numbers.length β†’ numbers[i]! < threshold; let spec (res: Bool) := (numbers.length = 0 β†’ res) ∧ (res ↔ numbers_below_threshold) -- program terminates βˆƒ result, impl numbers threshold = result ∧ -- return value satisfies spec spec result -- if result then spec else Β¬spec
def generated_spec -- function signature (impl: List Int β†’ Int β†’ Bool) -- inputs (numbers : List Int) (threshold : Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ numbers threshold, problem_spec impl numbers threshold) ↔ (βˆ€ numbers threshold, generated_spec impl numbers threshold) :=
sorry
def implementation (numbers: List Int) (threshold: Int) : Bool :=
sorry
-- #test implementation ([1, 2, 4, 10]: List Int) 100 = true -- #test implementation ([1, 20, 4, 10]: List Int) 5 = false
theorem correctness (numbers: List Int) (threshold: Int) : problem_spec implementation numbers threshold :=
by unfold problem_spec let result := implementation numbers threshold use result simp [result] sorry
null
null
null
def add(x: Int, y: Int) -> Int
Add two numbers x and y
[{"input": [2, 3], "expected_output": 5}, {"input": [5, 7], "expected_output": 12}]
def add(x: Int, y: Int) -> Int """Add two numbers x and y"""
def problem_spec -- function signature (impl: Int β†’ Int β†’ Int) -- inputs (x y: Int) := -- spec let spec (res: Int) := res - x - y = 0 -- program terminates βˆƒ result, impl x y = result ∧ -- return value satisfies spec spec result -- if result then spec else Β¬spec
def generated_spec -- function signature (impl: Int β†’ Int β†’ Int) -- inputs (x 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 y: Int) : Int :=
sorry
-- #test implementation 2 3 = 5 -- #test implementation 5 7 = 12
theorem correctness (x y: Int) : problem_spec implementation x y :=
by unfold problem_spec let result := implementation x y use result simp [result] sorry
null
null
null
def same_chars(s0: string, s1: string) -> Bool
Check if two words have the same characters.
[{"input": ["eabcdzzzz", "dddzzzzzzzddeddabc"], "expected_output": true}, {"input": ["eabcd", "dddddddabc"], "expected_output": false}]
def same_chars(s0: string, s1: string) -> Bool """Check if two words have the same characters."""
def problem_spec -- function signature (impl: String β†’ String β†’ Bool) -- inputs (s0 s1: String) := -- spec let spec (res: Bool) := res ↔ (βˆ€ c : Char, c ∈ s0.toList ↔ c ∈ s1.toList) -- program terminates βˆƒ result, impl s0 s1 = result ∧ -- return value satisfies spec spec result -- if result then spec else Β¬spec
def generated_spec -- function signature (impl: String β†’ String β†’ Bool) -- inputs (s0 s1: String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ s0 s1, problem_spec impl s0 s1) ↔ (βˆ€ s0 s1, generated_spec impl s0 s1) :=
sorry
def implementation (s0 s1: String) : Bool :=
sorry
-- #test implementation 'eabcdzzzz' 'dddzzzzzzzddeddabc' = true -- #test implementation 'abcd' 'dddddddabc' = true -- #test implementation 'dddddddabc' 'abcd' = true -- #test implementation 'eabcd' 'dddddddabc' = false -- #test implementation 'abcd' 'dddddddabce' = false -- #test implementation 'eabcdzzzz' 'dddzzzzzzzddddabc' = false
theorem correctness (s0 s1: String) : problem_spec implementation s0 s1 :=
by unfold problem_spec let result := implementation s0 s1 use result simp [result] sorry
null
null
null
def fib(n: int) -> int
Return n-th Fibonacci number.
[{"input": 10, "expected_output": 55}, {"input": 1, "expected_output": 1}, {"input": 8, "expected_output": 21}]
def fib(n: int) -> int """Return n-th Fibonacci number. """
def problem_spec -- function signature (implementation: Nat β†’ Nat) -- inputs (n: Nat) := -- spec let spec (result: Nat) := fibonacci_non_computable n result -- program termination βˆƒ result, implementation n = result ∧ spec result
def generated_spec -- function signature (impl: Nat β†’ Nat) -- inputs (x : 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 10 = 55 -- #test implementation 1 = 1 -- #test implementation 8 = 21
theorem correctness (n: Nat) : problem_spec implementation n :=
by unfold problem_spec let result := implementation n use result simp [result] repeat sorry
/-- name: fibonacci_non_computable use: | Non-computable definition to check if a number is a Fibonacci number. problems: - 55 sample_problems: - 3 -/ inductive fibonacci_non_computable : β„• β†’ β„• β†’ Prop | base0 : fibonacci_non_computable 0 0 | base1 : fibonacci_non_computable 1 1 | step : βˆ€ n f₁ fβ‚‚, fibonacci_non_computable n f₁ β†’ fibonacci_non_computable (n + 1) fβ‚‚ β†’ fibonacci_non_computable (n + 2) (f₁ + fβ‚‚)
null
null
def correct_bracketing(brackets: str) -> Bool
brackets is a string of "<" and ">". return True if every opening bracket has a corresponding closing bracket, i.e., (each open bracket is properly closed)
[{"input": "<", "expected_output": false}, {"input": "<>", "expected_output": true}, {"input": "<<><>>", "expected_output": true}, {"input": "><<>", "expected_output": false}]
def correct_bracketing(brackets: str) -> Bool """brackets is a string of "<" and ">". return True if every opening bracket has a corresponding closing bracket, i.e., (each open bracket is properly closed) """
def problem_spec -- function signature (impl: String β†’ Bool) -- inputs (brackets: String) := -- spec let spec (result: Bool) := brackets.data.all (fun c => c = '<' ∨ c = '>') β†’ (result ↔ balanced_paren_non_computable brackets '<' '>') -- program terminates βˆƒ result, impl brackets = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: String β†’ Bool) -- inputs (brackets : String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ brackets, problem_spec impl brackets) ↔ (βˆ€ brackets, generated_spec impl brackets) :=
sorry
def implementation (brackets: String) : Bool :=
sorry
-- #test implementation "<" = false -- #test implementation "<>" = true -- #test implementation "<<><>>" = true -- #test implementation "><<>" = false
theorem correctness (brackets: String) : problem_spec implementation brackets :=
by unfold problem_spec let result := implementation brackets use result simp [result] sorry
null
null
null
def monotonic(numbers: List[int]) -> Bool
Return True if list elements are monotonically increasing or decreasing.
[{"input": [1, 2, 4, 20], "expected_output": true}, {"input": [1, 20, 4, 10], "expected_output": false}, {"input": [4, 1, 0, -10], "expected_output": true}]
def monotonic(numbers: List[int]) -> Bool """Return True if list elements are monotonically increasing or decreasing. """
def problem_spec -- function signature (implementation: List Int β†’ Bool) -- inputs (numbers: List Int) := let non_ordered := βˆƒ i j, i < numbers.length - 1 ∧ j < numbers.length - 1 ∧ (numbers[i]! < numbers[i+1]!) ∧ (numbers[j+1]! < numbers[j]!); -- spec let spec (result: Bool) := 1 < numbers.length β†’ result ↔ Β¬non_ordered; -- program termination βˆƒ result, implementation numbers = result ∧ spec result
def generated_spec -- function signature (impl: List Int β†’ Bool) -- inputs (numbers : List Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ numbers, problem_spec impl numbers) ↔ (βˆ€ numbers, generated_spec impl numbers) :=
sorry
def implementation (numbers: List Int) : Bool :=
sorry
-- #test implementation [1, 2, 4, 20] = true -- #test implementation [1, 20, 4, 10] = false -- #test implementation [4, 1, 0, -10] = true
theorem correctness (numbers: List Int) : problem_spec implementation numbers :=
by unfold problem_spec let result := implementation numbers use result simp [result] sorry
null
null
null
def common(l1: List[Int], l2: List[Int]) -> List[Int]
Return sorted unique common elements for two lists.
[{"input": [[1, 4, 3, 34, 653, 2, 5], [5, 7, 1, 5, 9, 653, 121]], "expected_output": [1, 5, 653]}, {"input": [[5, 3, 2, 8], [3, 2]], "expected_output": [2, 3]}]
def common(l1: List[Int], l2: List[Int]) -> List[Int] """Return sorted unique common elements for two lists. """
def problem_spec -- function signature (implementation: List Int β†’ List Int β†’ List Int) -- inputs (l1 l2: List Int) := let is_unique (result: List Int) := βˆ€ i j, i < result.length β†’ j < result.length β†’ i β‰  j β†’ result[i]! β‰  result[j]!; let is_sorted (result: List Int) := βˆ€ i, i < result.length - 1 β†’ result[i]! ≀ result[i + 1]!; -- spec let spec (result: List Int) := is_unique result ∧ is_sorted result ∧ (βˆ€ i : Int, i ∈ result ↔ i ∈ l1 ∧ i ∈ l2) -- program termination βˆƒ result, implementation l1 l2 = result ∧ spec result
def generated_spec -- function signature (impl: List Int β†’ List Int β†’ List Int) -- inputs (x y: List Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ x y, problem_spec impl x y) ↔ (βˆ€ x y, generated_spec impl x y) :=
sorry
def implementation (l1 l2: List Int) : List Int :=
sorry
-- #test implementation [1, 4, 3, 34, 653, 2, 5] [5, 7, 1, 5, 9, 653, 121] = [1, 5, 653] -- #test implementation [5, 3, 2, 8] [3, 2] = [2, 3]
theorem correctness (l1 l2: List Int) : problem_spec implementation l1 l2 :=
by unfold problem_spec let result := implementation l1 l2 use result simp [result] sorry
null
null
null
def largest_prime_factor(n: Nat) -> Nat
Return the largest prime factor of n. Assume n > 1 and is not a prime.
[{"input": 13195, "expected_output": 29}, {"input": 2048, "expected_output": 2}]
def largest_prime_factor(n: Nat) -> Nat """Return the largest prime factor of n. Assume n > 1 and is not a prime. """
def problem_spec -- function signature (implementation: Nat β†’ Nat) -- inputs (n: Nat) := -- spec let spec (result: Nat) := 1 < n ∧ Β¬ Nat.Prime n β†’ (Nat.Prime result ∧ result ∣ n ∧ βˆ€ i, i < n ∧ i ∣ n ∧ Nat.Prime i β†’ i ≀ result); -- program termination βˆƒ result, implementation n = result ∧ spec result
def generated_spec -- function signature (impl: Nat β†’ Nat) -- inputs (x : Nat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ x, problem_spec impl x) ↔ (βˆ€ x, generated_spec impl x) :=
sorry
def implementation (n: Nat) : Nat :=
sorry
-- #test implementation 13195 = 29 -- #test implementation 2048 = 2
theorem correctness (n: Nat) : problem_spec implementation n :=
by unfold problem_spec let result := implementation n use result simp [result] sorry
null
null
null
def sum_to_n(n: Nat) -> Nat
sum_to_n is a function that sums numbers from 1 to n.
[{"input": 30, "expected_output": 465}, {"input": 100, "expected_output": 4950}]
def sum_to_n(n: Nat) -> Nat """sum_to_n is a function that sums numbers from 1 to n. """
def problem_spec -- function signature (implementation: Nat β†’ Nat) -- inputs (n : Nat) := -- spec let spec (result: Nat) := 0 < n β†’ (result = 1 ↔ n = 1) ∧ (βˆ€ i, implementation (i + 1) - (implementation i) = i + 1) -- program termination βˆƒ result, implementation n = result ∧ 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 30 = 465 -- #test implementation 100 = 5050 -- #test implementation 5 = 15 -- #test implementation 10 = 55 -- #test implementation 1 = 1
theorem correctness (n: Nat) : problem_spec implementation n :=
by unfold problem_spec let result := implementation n use result simp [result] sorry
null
null
null
def correct_bracketing(brackets: str) -> Bool
brackets is a string of "(" and ")". return True if every opening bracket has a corresponding closing bracket.
[{"input": "(", "expected_output": false}, {"input": "()", "expected_output": true}, {"input": "(()())", "expected_output": true}, {"input": ")(()", "expected_output": false}]
def correct_bracketing(brackets: str) -> Bool """brackets is a string of "(" and ")". return True if every opening bracket has a corresponding closing bracket. """
def problem_spec -- function signature (impl: String β†’ Bool) -- inputs (brackets: String) := -- spec let spec (result: Bool) := brackets.data.all (fun c => c = '(' ∨ c = ')') β†’ (result ↔ balanced_paren_non_computable brackets '(' ')') -- program terminates βˆƒ result, impl brackets = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: String β†’ Bool) -- inputs (x : String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ x, problem_spec impl x) ↔ (βˆ€ x, generated_spec impl x) :=
sorry
def implementation (paren_string: String) : Bool :=
sorry
-- #test implementation "(" = false -- #test implementation "()" = true -- #test implementation "(()())" = true -- #test implementation ")(()" = false
theorem correctness (brackets: String) : problem_spec implementation brackets :=
by unfold problem_spec let result := implementation brackets use result simp [result] sorry
null
null
null
def derivative(xs: List Int) -> List Int
xs represent coefficients of a polynomial. xs[0] + xs[1] * x + xs[2] * x^2 + .... Return derivative of this polynomial in the same form.
[{"input": [3, 1, 2, 4, 5], "expected_output": [1, 4, 12, 20]}, {"input": [1, 2, 3], "expected_output": [2, 6]}]
def derivative(xs: List Int) -> List Int """xs represent coefficients of a polynomial. xs[0] + xs[1] * x + xs[2] * x^2 + .... Return derivative of this polynomial in the same form. """
def problem_spec -- function signature (impl: List Int β†’ List Int) -- inputs (xs: List Int) := -- spec let spec (result: List Int) := result.length = xs.length - 1 ∧ result = (check_derivative xs.reverse).reverse -- program terminates βˆƒ result, impl xs = result ∧ -- return value satisfies spec spec result
def generated_spec -- function signature (impl: List Int β†’ List Int) -- inputs (xs : List Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ xs, problem_spec impl xs) ↔ (βˆ€ xs, generated_spec impl xs) :=
sorry
def implementation (xs: List Int) : List Int :=
sorry
-- #test implementation [3, 1, 2, 4, 5] : List Int = [1, 4, 12, 20] -- #test implementation [1, 2, 3] : List Int = [2, 6]
theorem correctness (xs: List Int) : problem_spec implementation xs :=
by unfold problem_spec let result := implementation xs use result simp [result] sorry
null
null
null
def fibfib(n: int)
The FibFib number sequence is a sequence similar to the Fibbonacci sequnece that's defined as follows: fibfib(0) == 0 fibfib(1) == 0 fibfib(2) == 1 fibfib(n) == fibfib(n-1) + fibfib(n-2) + fibfib(n-3). Please write a function to efficiently compute the n-th element of the fibfib number sequence.
[{"input": 1, "output": 0}, {"input": 5, "output": 4}, {"input": 8, "output": 24}]
def fibfib(n: int) """The FibFib number sequence is a sequence similar to the Fibbonacci sequnece that's defined as follows: fibfib(0) == 0 fibfib(1) == 0 fibfib(2) == 1 fibfib(n) == fibfib(n-1) + fibfib(n-2) + fibfib(n-3). Please write a function to efficiently compute the n-th element of the fibfib number sequence. """
def problem_spec -- function signature (implementation: Nat β†’ Nat) -- inputs (n: Nat) := -- spec let spec (result: Nat) := fibonacci_non_computable_3 n result -- program termination βˆƒ result, implementation n = result ∧ spec result
def generated_spec -- function signature (impl: Nat β†’ Nat) -- inputs (x : Nat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ x, problem_spec impl x) ↔ (βˆ€ x, generated_spec impl x) :=
sorry
def implementation (n: Nat) : Nat :=
sorry
-- #test implementation 1 = 0 -- #test implementation 5 = 4 -- #test implementation 8 = 24
theorem correctness (n: Nat) : problem_spec implementation n :=
by unfold problem_spec let result := implementation n use result simp [result] repeat sorry
/-- name: fibonacci_non_computable_3 use: | Non-computable definition to check if a number is a Fibonacci number such that fib(n) = fib(n - 1) + fib(n - 2) + fib(n - 3). problems: - 63 -/ inductive fibonacci_non_computable_3 : β„• β†’ β„• β†’ Prop | base0 : fibonacci_non_computable_3 0 0 | base1 : fibonacci_non_computable_3 1 0 | base2 : fibonacci_non_computable_3 2 1 | step : βˆ€ n f₁ fβ‚‚ f₃, fibonacci_non_computable_3 n f₁ β†’ fibonacci_non_computable_3 (n + 1) fβ‚‚ β†’ fibonacci_non_computable_3 (n + 2) f₃ β†’ fibonacci_non_computable_3 (n + 3) (f₁ + fβ‚‚ + f₃)
null
null
def remove_vowels(string: str) -> Nat
Write a function vowels_count which takes a string representing a word as input and returns the number of vowels in the string. Vowels in this case are 'a', 'e', 'i', 'o', 'u'. Here, 'y' is also a vowel, but only when it is at the end of the given word.
[{"input": "abcde", "expected_output": 2}, {"input": "ACEDY", "expected_output": 3}]
def remove_vowels(string: str) -> Nat """Write a function vowels_count which takes a string representing a word as input and returns the number of vowels in the string. Vowels in this case are 'a', 'e', 'i', 'o', 'u'. Here, 'y' is also a vowel, but only when it is at the end of the given word. """
def problem_spec -- function signature (implementation: String β†’ Nat) -- inputs (string: String) := let isVowel (c : Char) := let vowels := "aeiouAEIOU" vowels.contains c let isY (c : Char) := c = 'y' ∨ c = 'Y' -- spec let spec (result: Nat) := string.data.all (fun c => c.isAlpha) β†’ if string.length = 1 then result = if isVowel string.data[0]! ∨ isY string.data[0]! then 1 else 0 else result = (if isVowel string.data[0]! then 1 else 0) + implementation (string.drop 1); -- program termination βˆƒ result, implementation string = result ∧ spec result
def generated_spec -- function signature (impl: String β†’ Nat) -- inputs (x : String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ x, problem_spec impl x) ↔ (βˆ€ x, generated_spec impl x) :=
sorry
def implementation (string: String) : Nat :=
sorry
-- #test implementation "abcde" = 2 -- #test implementation "ACEDY" = 3
theorem correctness (s: String) : problem_spec implementation s :=
by unfold problem_spec let result := implementation s use result simp [result] sorry
null
null
null
def circular_shift(x: Int, shift: Int) -> String
Circular shift the digits of the integer x, shift the digits right by shift and return the result as a string. If shift > number of digits, return digits reversed.
[{"input": [12, 1], "expected_output": 21}, {"input": [12, 2], "expected_output": 12}]
def circular_shift(x: Int, shift: Int) -> String """Circular shift the digits of the integer x, shift the digits right by shift and return the result as a string. If shift > number of digits, return digits reversed. """
def problem_spec -- function signature (implementation: Nat β†’ Nat β†’ String) -- inputs (x shift: Nat) := let isReverse (s: String) : Bool := let n := s.length; βˆ€ i, i < n / 2 β†’ s.get! ⟨i⟩ = s.get! ⟨n - 1 - i⟩; -- spec let spec (result: String) := let x_str := Nat.repr x; result.length = x_str.length ∧ (x_str.length < shift β†’ isReverse x_str) ∧ (shift ≀ x_str.length β†’ x_str.take shift = result.drop (x_str.length - shift) ∧ x_str.drop shift = result.take (x_str.length - shift)); -- program termination βˆƒ result, implementation x shift = result ∧ spec result
def generated_spec -- function signature (impl: Nat β†’ Nat β†’ String) -- inputs (x y: Nat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ x y, problem_spec impl x y) ↔ (βˆ€ x y, generated_spec impl x y) :=
sorry
def implementation (x shift: Nat) : String :=
sorry
-- #test implementation (12 : Int) (1 : Int) = "21" -- #test implementation (12 : Int) (2 : Int) = "12"
theorem correctness (x shift: Nat) : problem_spec implementation x shift :=
by unfold problem_spec let result := implementation x shift use result simp [result] sorry
null
null
null
def digitSum(string: str) -> Nat
Write a function that takes a string as input and returns the sum of the upper characters only' ASCII codes.
[{"input": "", "expected_output": 0}, {"input": "abAB", "expected_output": 131}, {"input": "helloE", "expected_output": 69}]
def digitSum(string: str) -> Nat """Write a function that takes a string as input and returns the sum of the upper characters only' ASCII codes. """
def problem_spec -- function signature (implementation: String β†’ Nat) -- inputs (string: String) := let isUpper (c : Char) := 65 ≀ c.toNat ∧ c.toNat ≀ 90 -- spec let spec (result: Nat) := if string.length = 1 then result = if isUpper string.data[0]! then string.data[0]!.toNat else 0 else result = (if isUpper string.data[0]! then string.data[0]!.toNat else 0) + implementation (string.drop 1); -- program termination βˆƒ result, implementation string = result ∧ spec result
def generated_spec -- function signature (impl: String β†’ Nat) -- inputs (x: String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ x, problem_spec impl x) ↔ (βˆ€ x, generated_spec impl x) :=
sorry
def implementation (string: String) : Nat :=
sorry
-- #test implementation "" = 0 -- #test implementation "abAB" = 131 -- #test implementation "abcCd" = 67 -- #test implementation "helloE" = 69 -- #test implementation "woArBld" = 131 -- #test implementation "aAaaaXa" = 153
theorem correctness (s: String) : problem_spec implementation s :=
by unfold problem_spec let result := implementation s use result simp [result] sorry
null
null
null
def fruit_distribution(string: str, Nat n) -> Nat
In this task, you will be given a string that represents a number of apples and oranges that are distributed in a basket of fruit this basket contains apples, oranges, and mango fruits. Given the string that represents the total number of the oranges and apples and an integer that represent the total number of the fruits in the basket return the number of the mango fruits in the basket.
[{"input": ["5 apples and 6 oranges", 19], "expected_output": 8}, {"input": ["0 apples and 1 oranges", 3], "expected_output": 2}, {"input": ["2 apples and 3 oranges", 100], "expected_output": 95}, {"input": ["100 apples and 1 oranges", 120], "expected_output": 19}]
def fruit_distribution(string: str, Nat n) -> Nat """In this task, you will be given a string that represents a number of apples and oranges that are distributed in a basket of fruit this basket contains apples, oranges, and mango fruits. Given the string that represents the total number of the oranges and apples and an integer that represent the total number of the fruits in the basket return the number of the mango fruits in the basket. """
def problem_spec -- function signature (implementation: String β†’ Nat β†’ Nat) -- inputs (string: String) (n : Nat) := -- spec let spec (result: Nat) := βˆƒ x y : Nat, x + y = n - result ∧ (String.join [x.repr, " apples and ", y.repr, " oranges"] = string) -- program termination βˆƒ result, implementation string n = result ∧ spec result
def generated_spec -- function signature (impl: String β†’ Nat β†’ Nat) -- inputs (x: String) (y: Nat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ x y, problem_spec impl x y) ↔ (βˆ€ x y, generated_spec impl x y) :=
sorry
def implementation (string: String) (n: Nat) : Nat :=
sorry
-- #test implementation "5 apples and 6 oranges" 19 = 8 -- #test implementation "0 apples and 1 oranges" 3 = 2 -- #test implementation "2 apples and 3 oranges" 100 = 95 -- #test implementation "100 apples and 1 oranges" 120 = 19
theorem correctness (s: String) (n : Nat) : problem_spec implementation s n :=
by unfold problem_spec let result := implementation s n use result simp [result] sorry
null
null
null
def pluck(numbers: List[Int]) -> List[Int]
Given an array representing a branch of a tree that has non-negative integer nodes your task is to pluck one of the nodes and return it. The plucked node should be the node with the smallest even value. If multiple nodes with the same smallest even value are found return the node that has smallest index. The plucked node should be returned in a list, [ smallest_value, its index ], If there are no even values or the given array is empty, return [].
[{"input": [4, 2, 3], "expected_output": [2, 1]}, {"input": [1, 2, 3], "expected_output": [2, 1]}, {"input": [], "expected_output": []}, {"input": [5, 0, 3, 0, 4, 2], "expected_output": [0, 1]}]
def pluck(numbers: List[Int]) -> List[Int] """Given an array representing a branch of a tree that has non-negative integer nodes your task is to pluck one of the nodes and return it. The plucked node should be the node with the smallest even value. If multiple nodes with the same smallest even value are found return the node that has smallest index. The plucked node should be returned in a list, [ smallest_value, its index ], If there are no even values or the given array is empty, return []. """
def problem_spec -- function signature (implementation: List Nat β†’ List Nat) -- inputs (numbers: List Nat) := -- spec let spec (result: List Nat) := (result.length = 0 ↔ βˆ€ i, i < numbers.length β†’ numbers[i]! % 2 = 1) ∧ (result.length = 2 ↔ βˆƒ i, i < numbers.length ∧ numbers[i]! % 2 = 0 ∧ result[0]! = numbers[i]! ∧ result[1]! = i ∧ (βˆ€ j, j < numbers.length β†’ j < i β†’ (numbers[j]! % 2 = 1 ∨ numbers[i]! < numbers[j]!)) ∧ (βˆ€ k, k < numbers.length β†’ numbers[k]! % 2 = 0 β†’ numbers[i]! ≀ numbers[k]!)); -- program termination βˆƒ result, implementation numbers = 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 (numbers: List Nat) : List Nat :=
sorry
-- #test implementation [4, 2, 3] = [2, 1] -- #test implementation [1, 2, 3] = [2, 1] -- #test implementation [] = [] -- #test implementation [5, 0, 3, 0, 4, 2] = [0, 1] --
theorem correctness (numbers: List Nat) : problem_spec implementation numbers :=
by unfold problem_spec let result := implementation numbers use result simp [result] sorry
null
null
null
def search(numbers: List[int]) -> int
You are given a non-empty list of positive integers. Return the greatest integer that is greater than zero, and has a frequency greater than or equal to the value of the integer itself. The frequency of an integer is the number of times it appears in the list. If no such a value exist, return -1.
[{"input": [4, 1, 2, 2, 3, 1], "expected_output": 2}, {"input": [1, 2, 2, 3, 3, 3, 4, 4, 4], "expected_output": 3}, {"input": [5, 5, 4, 4, 4], "expected_output": -1}]
def search(numbers: List[int]) -> int """You are given a non-empty list of positive integers. Return the greatest integer that is greater than zero, and has a frequency greater than or equal to the value of the integer itself. The frequency of an integer is the number of times it appears in the list. If no such a value exist, return -1. """
def problem_spec -- function signature (implementation: List Int β†’ Int) -- inputs (numbers: List Int) := -- spec let spec (result: Int) := 0 < numbers.length ∧ numbers.all (fun n => 0 < n) β†’ (result β‰  -1 ↔ βˆƒ i : Nat, i < numbers.length ∧ numbers[i]! = result ∧ numbers[i]! > 0 ∧ numbers[i]! ≀ (numbers.filter (fun x => x = numbers[i]!)).length ∧ (Β¬βˆƒ j : Nat, j < numbers.length ∧ numbers[i]! < numbers[j]! ∧ numbers[j]! ≀ numbers.count numbers[j]!)); -- program termination βˆƒ result, implementation numbers = result ∧ spec result
def generated_spec -- function signature (impl: List Int β†’ Int) -- inputs (x: List Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ x, problem_spec impl x) ↔ (βˆ€ x, generated_spec impl x) :=
sorry
def implementation (numbers: List Int) : (Int) :=
sorry
-- #test implementation [4, 1, 2, 2, 3, 1] = 2 -- #test implementation [1, 2, 2, 3, 3, 4, 4, 4] = 3 -- #test implementation [5, 5, 4, 4, 4] = -1
theorem correctness (numbers: List Int) : problem_spec implementation numbers :=
by unfold problem_spec let result := implementation numbers use result simp [result] sorry
null
null
null
def strange_sort_list(lst: List[int]) -> List[int]
Given list of integers, return list in strange order. Strange sorting is when you start with the minimum value, then the maximum of the remaining integers, then the minimum and so on.
[{"input": [1, 2, 3, 4], "expected_output": [1, 4, 2, 3]}, {"input": [5, 5, 5, 5], "expected_output": [5, 5, 5, 5]}, {"input": [], "expected_output": []}]
def strange_sort_list(lst: List[int]) -> List[int] """Given list of integers, return list in strange order. Strange sorting is when you start with the minimum value, then the maximum of the remaining integers, then the minimum and so on. """
def problem_spec -- function signature (implementation: List Int β†’ List Int) -- inputs (lst: List Int) := -- spec let spec (result: List Int) := let sorted_lst := lst.mergeSort; (List.Perm lst result) ∧ (forall i, (0 <= i ∧ i < lst.length ∧ i % 2 = 0) β†’ result[i]! = sorted_lst[i / 2]!) ∧ (forall i, (0 <= i ∧ i < lst.length ∧ i % 2 = 1) β†’ result[i]! = sorted_lst[lst.length - (i-1)/2 - 1]!) -- program termination βˆƒ result, implementation lst = result ∧ spec result
def generated_spec -- function signature (impl: List Int β†’ List 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): List Int :=
sorry
-- #test implementation [1, 2, 3, 4] = [1, 4, 2, 3] -- #test implementation [5, 6, 7, 8, 9] = [5, 9, 6, 8, 7] -- #test implementation [1, 2, 3, 4, 5] = [1, 5, 2, 4, 3] -- #test implementation [5, 6, 7, 8, 9, 1] = [1, 9, 5, 8, 6, 7] -- #test implementation [5, 5, 5, 5] = [5, 5, 5, 5] -- #test implementation [] = [] -- #test implementation [1,2,3,4,5,6,7,8] = [1, 8, 2, 7, 3, 6, 4, 5] -- #test implementation [0,2,2,2,5,5,-5,-5] = [-5, 5, -5, 5, 0, 2, 2, 2] -- #test implementation [111111] = [111111]
theorem correctness (lst: List Int) : problem_spec implementation lst :=
by sorry
null
null
null
def triangle_area(a: float, b: float, c: float) -> float
Given the lengths of the three sides of a triangle. Return the area of the triangle rounded to 2 decimal points if the three sides form a valid triangle. Otherwise return -1. Three sides make a valid triangle when the sum of any two sides is greater than the third side.
[{"input": "(3, 4, 5)", "expected_output": 6}, {"input": "(1, 2, 10)", "expected_output": -1}]
def triangle_area(a: float, b: float, c: float) -> float """Given the lengths of the three sides of a triangle. Return the area of the triangle rounded to 2 decimal points if the three sides form a valid triangle. Otherwise return -1. Three sides make a valid triangle when the sum of any two sides is greater than the third side. """
def problem_spec -- function signature (implementation: Rat β†’ Rat β†’ Rat β†’ Rat) -- inputs (a: Rat) (b: Rat) (c: Rat) := -- spec let spec (result : Rat) := let is_valid_triangle := (a + b > c) ∧ (a + c > b) ∧ (b + c > a); let s := (a + b + c) / 2; if is_valid_triangle then |result^2 - (s * (s-a) * (s-b) * (s-c))| ≀ ((1: Rat)/10000) else result = -1 -- program termination βˆƒ result, implementation a b c = result ∧ spec result
def generated_spec -- function signature (impl: Rat β†’ Rat β†’ Rat β†’ Rat) -- inputs (a: Rat) (b: Rat) (c: Rat) : 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: Rat) (b: Rat) (c: Rat): Rat :=
sorry
-- #test implementation 3 4 5 = 6.00 -- #test implementation 1 2 10 = -1 -- #test implementation 4 8 5 = 8.18 -- #test implementation 2 2 2 = 1.73 -- #test implementation 1 2 3 = -1 -- #test implementation 10 5 7 = 16.25 -- #test implementation 2 6 3 = -1 -- #test implementation 1 1 1 = 0.43 -- #test implementation 2 2 10 = -1
theorem correctness (a: Rat) (b: Rat) (c: Rat) : problem_spec implementation a b c :=
by sorry
null
null
null
def will_it_fly(q: List[int], w: int) -> bool
Write a function that returns True if the object q will fly, and False otherwise. The object q will fly if it's balanced (it is a palindromic list) and the sum of its elements is less than or equal the maximum possible weight w.
[{"input": "([1, 2], 5)", "expected_output": false}, {"input": "([3, 2, 3], 1)", "expected_output": false}, {"input": "([3, 2, 3], 9)", "expected_output": true}, {"input": "([3], 5)", "expected_output": true}]
def will_it_fly(q: List[int], w: int) -> bool """Write a function that returns True if the object q will fly, and False otherwise. The object q will fly if it's balanced (it is a palindromic list) and the sum of its elements is less than or equal the maximum possible weight w. """
def problem_spec -- function signature (implementation: List Int β†’ Int β†’ Bool) -- inputs (q: List Int) (w: Int) := -- spec let spec (result : Bool) := (result β†’ (List.Palindrome q)) ∧ (result β†’ (List.sum q ≀ w)) ∧ (Β¬(List.Palindrome q) β†’ Β¬ result) ∧ (Β¬(List.sum q ≀ w) β†’ Β¬ result) -- program termination βˆƒ result, implementation q w = result ∧ spec result
def generated_spec -- function signature (impl: List Int β†’ Int β†’ Bool) -- inputs (q: List Int) (w: Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ q w, problem_spec impl q w) ↔ (βˆ€ q w, generated_spec impl q w) :=
sorry
def implementation (q: List Int) (w: Int) : Bool :=
(List.Palindrome q) ∧ (List.sum q ≀ w)
-- #test implementation [3, 2, 3] 9 = True -- #test implementation [1, 2] 5 = False -- #test implementation [3] 5 = True -- #test implementation [3, 2, 3] 1 = False -- #test implementation [1, 2, 3] 6 = False -- #test implementation [5] 5 = True
theorem correctness (q: List Int) (w: Int) : problem_spec implementation q w :=
by unfold problem_spec let result := implementation q w use result simp [result] simp [implementation] apply And.intro intro h_is_palindrome h_q_sum assumption apply And.intro intro h_palindrome simp [h_palindrome] intro h_q_sum h_palindrome assumption
null
null
null
def smallest_change(arr: List[int]) -> int
Given an array arr of integers, find the minimum number of elements that need to be changed to make the array palindromic. A palindromic array is an array that is read the same backwards and forwards. In one change, you can change one element to any other element.
[{"input": [1, 2, 3, 5, 4, 7, 9, 6], "expected_output": 4}, {"input": [1, 2, 3, 4, 3, 2, 2], "expected_output": 1}, {"input": [1, 2, 3, 2, 1], "expected_output": 0}]
def smallest_change(arr: List[int]) -> int """Given an array arr of integers, find the minimum number of elements that need to be changed to make the array palindromic. A palindromic array is an array that is read the same backwards and forwards. In one change, you can change one element to any other element. """
def problem_spec -- function signature (implementation: List Int β†’ Int) -- inputs (arr: List Int) := -- spec let spec (result : Int) := let swaps_done (arr1: List Int) (arr2: List Int) := ((List.finRange (arr1.length)).filter (fun idx => arr1[idx]? β‰  arr2[idx]?)).length/2 βˆ€ palin_perm, (List.Perm arr palin_perm) ∧ (List.Palindrome palin_perm) β†’ result ≀ (swaps_done arr palin_perm) -- 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 :=
((List.finRange (arr.length/2)).filter (fun i => arr[i]? != arr[arr.length - 1 - i]?)).length
-- #test implementation [1,2,3,5,4,7,9,6] = 4 -- #test implementation [1, 2, 3, 4, 3, 2, 2] = 1 -- #test implementation [1, 4, 2] = 1 -- #test implementation [1, 4, 4, 2] = 1 -- #test implementation [1, 2, 3, 2, 1] = 0 -- #test implementation [3, 1, 1, 3] = 0 -- #test implementation [1] = 0 -- #test implementation [0, 1] = 1
theorem correctness (arr: List Int) : problem_spec implementation arr :=
by sorry
null
null
null
def total_match(lst1: List[str], lst2: List[str]) -> List[str]
Write a function that accepts two lists of strings and returns the list that has total number of chars in the all strings of the list less than the other list. If the two lists have the same number of chars, return the first list.
[{"input": "([], [])", "expected_output": []}, {"input": "(['hi', 'admin'], ['hI', 'Hi'])", "expected_output": ["hI", "Hi"]}, {"input": "(['hi', 'admin'], ['hi', 'hi', 'admin', 'project'])", "expected_output": ["hi", "admin"]}, {"input": "(['hi', 'admin'], ['hI', 'hi', 'hi'])", "expected_output": ["hI", "hi", "hi"]}, {"input": "(['4'], ['1', '2', '3', '4', '5'])", "expected_output": ["4"]}]
def total_match(lst1: List[str], lst2: List[str]) -> List[str] """Write a function that accepts two lists of strings and returns the list that has total number of chars in the all strings of the list less than the other list. If the two lists have the same number of chars, return the first list. """
def problem_spec -- function signature (implementation: List String β†’ List String β†’ List String) -- inputs (lst1: List String) (lst2: List String) := let sum_chars (xs: List String) : Int := xs.foldl (Ξ» acc a => acc + a.length) 0; -- spec let spec (result : List String) := ((result = lst1) ∨ (result = lst2)) ∧ (sum_chars result ≀ sum_chars lst1) ∧ (sum_chars result ≀ sum_chars lst2) ∧ ((sum_chars lst1 = sum_chars lst2) β†’ (result = lst1)) -- program termination βˆƒ result, implementation lst1 lst2 = result ∧ spec result
def generated_spec -- function signature (impl: List String β†’ List String β†’ List String) -- inputs (lst1: List String) (lst2: List String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ lst1 lst2, problem_spec impl lst1 lst2) ↔ (βˆ€ lst1 lst2, generated_spec impl lst1 lst2) :=
sorry
def implementation (lst1: List String) (lst2: List String) : List String :=
sorry
-- #test implementation [] [] = [] -- #test implementation ["hi", "admin"] ["hi", "hi"] = ["hi", "hi"] -- #test implementation ["hi", "admin"] ["hi", "hi", "admin", "project"] = ["hi", "admin"] -- #test implementation ["4"] ["1", "2", "3", "4", "5"] = ["4"] -- #test implementation ["hi", "admin"] ["hI", "Hi"] = ["hI", "Hi"] -- #test implementation ["hi", "admin"] ["hI", "hi", "hi"] = ["hI", "hi", "hi"] -- #test implementation ["hi", "admin"] ["hI", "hi", "hii"] = ["hi", "admin"] -- #test implementation [] ["this"] = [] -- #test implementation ["this"] [] == []
theorem correctness (lst1: List String) (lst2: List String) : problem_spec implementation lst1 lst2 :=
by sorry
null
null
null
def is_multiply_prime(a: int) -> bool
Write a function that returns true if the given number is the multiplication of 3 prime numbers and false otherwise. Knowing that (a) is less then 100.
[{"input": 30, "expected_output": true}]
def is_multiply_prime(a: int) -> bool """Write a function that returns true if the given number is the multiplication of 3 prime numbers and false otherwise. Knowing that (a) is less then 100. """
def problem_spec -- function signature (implementation: Int β†’ Bool) -- inputs (a: Int) := -- spec let spec (result: Bool) := (a < 100) β†’ result ↔ exists a' b c, (Nat.Prime a') ∧ (Nat.Prime b) ∧ (Nat.Prime c) ∧ (a == a'*b*c) -- program termination βˆƒ result, implementation a = result ∧ spec result
def generated_spec -- function signature (impl: Int β†’ Bool) -- inputs (a: Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ a, problem_spec impl a) ↔ (βˆ€ a, generated_spec impl a) :=
sorry
def implementation (a: Int) : Bool :=
sorry
-- #test implementation 5 = False -- #test implementation 30 = True -- #test implementation 8 = True -- #test implementation 10 = False -- #test implementation 125 = True -- #test implementation (3 * 5 * 7) = True -- #test implementation (3 * 6 * 7) = False -- #test implementation (9 * 9 * 9) = False -- #test implementation (11 * 9 * 9) = False -- #test implementation (11*13*7) = True
theorem correctness (a: Int) : problem_spec implementation a :=
by sorry
null
null
null
def is_simple_power(x: int, n: int) -> bool
Your task is to write a function that returns true if a number x is a simple power of n and false in other cases. x is a simple power of n if n**int=x
[{"input": "(1, 4)", "expected_output": true}, {"input": "(2, 2)", "expected_output": true}, {"input": "(8, 2)", "expected_output": true}, {"input": "(3, 2)", "expected_output": false}, {"input": "(3, 1)", "expected_output": false}, {"input": "(5, 3)", "expected_output": false}]
def is_simple_power(x: int, n: int) -> bool """Your task is to write a function that returns true if a number x is a simple power of n and false in other cases. x is a simple power of n if n**int=x """
def problem_spec -- function signature (implementation: Int β†’ Int β†’ Bool) -- inputs (x: Int) (n: Int) := -- spec let spec (result: Bool) := result ↔ exists k: Nat, x = n^k -- program termination βˆƒ result, implementation x n = result ∧ spec result
def generated_spec -- function signature (impl: Int β†’ Int β†’ Bool) -- inputs (x: Int) (n: Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ x n, problem_spec impl x n) ↔ (βˆ€ x n, generated_spec impl x n) :=
sorry
def implementation (x: Int) (n: Int) : Bool :=
sorry
-- #test implementation 16 2 = True -- #test implementation 143214 16 = False -- #test implementation 4 2 = True -- #test implementation 9 3 = True -- #test implementation 16 4 = True -- #test implementation 24 2 = False -- #test implementation 128 4 = False -- #test implementation 12 6 = False -- #test implementation 1 1 = True -- #test implementation 1 12 = True
theorem correctness (x: Int) (n: Int) : problem_spec implementation x n :=
by sorry
null
null
null
def iscube(a: int) -> bool
Write a function that takes an integer a and returns True if this integer is a cube of some integer number. Note: you may assume the input is always valid.
[{"input": 1, "expected_output": true}, {"input": 2, "expected_output": false}, {"input": -1, "expected_output": true}, {"input": 64, "expected_output": true}, {"input": 0, "expected_output": true}, {"input": 180, "expected_output": false}]
def iscube(a: int) -> bool """Write a function that takes an integer a and returns True if this integer is a cube of some integer number. Note: you may assume the input is always valid. """
def problem_spec -- function signature (implementation: Int β†’ Bool) -- inputs (a: Int) := -- spec let spec (result: Bool) := result ↔ exists n: Int, a = n^3 -- program termination βˆƒ result, implementation a = result ∧ spec result
def generated_spec -- function signature (impl: Int β†’ Bool) -- inputs (a: Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ a, problem_spec impl a) ↔ (βˆ€ a, generated_spec impl a) :=
sorry
def implementation (a: Int) : Bool :=
sorry
-- #test implementation 1 = True -- #test implementation 2 = False -- #test implementation -1 = True -- #test implementation 64 = True -- #test implementation 180 = False -- #test implementation 1000 = True -- #test implementation 0 = True -- #test implementation 1729 = False
theorem correctness (a: Int) : problem_spec implementation a :=
by sorry
null
null
null
def hex_key(num: string) -> int
You have been tasked to write a function that receives a hexadecimal number as a string and counts the number of hexadecimal digits that are primes (prime number, or a prime, is a natural number greater than 1 that is not a product of two smaller natural numbers). Hexadecimal digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. Prime numbers are 2, 3, 5, 7, 11, 13, 17,... So you have to determine a number of the following digits: 2, 3, 5, 7, B (=decimal 11), D (=decimal 13). Note: you may assume the input is always correct or empty string, and symbols A,B,C,D,E,F are always uppercase.
[{"input": "AB", "expected_output": 1}, {"input": "1077E", "expected_output": 2}, {"input": "ABED1A33", "expected_output": 4}, {"input": "123456789ABCDEF0", "expected_output": 6}, {"input": "2020", "expected_output": 2}]
def hex_key(num: string) -> int """You have been tasked to write a function that receives a hexadecimal number as a string and counts the number of hexadecimal digits that are primes (prime number, or a prime, is a natural number greater than 1 that is not a product of two smaller natural numbers). Hexadecimal digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. Prime numbers are 2, 3, 5, 7, 11, 13, 17,... So you have to determine a number of the following digits: 2, 3, 5, 7, B (=decimal 11), D (=decimal 13). Note: you may assume the input is always correct or empty string, and symbols A,B,C,D,E,F are always uppercase. """
def problem_spec -- function signature (implementation: String β†’ Int) -- inputs (num: String) := -- spec let spec (result: Int) := let num_val (ch : Char) := if ch.isDigit then (ch.toNat - '0'.toNat) else if ch.isUpper then ((ch.toNat - 'A'.toNat) + 10) else 0; 0 < num.length β†’ ( let char_val := num_val num.toList[0]!; (Nat.Prime char_val β†’ (1 < num.length β†’ result = char_val + implementation (num.drop 1)) ∧ (1 = num.length β†’ result = char_val)) ∧ (Β¬Nat.Prime char_val β†’ (1 < num.length β†’ result = implementation (num.drop 1)) ∧ (1 = num.length β†’ result = 0)) ) -- program termination βˆƒ result, implementation num = result ∧ spec result
def generated_spec -- function signature (impl: String β†’ Int) -- inputs (num: String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ num, problem_spec impl num) ↔ (βˆ€ num, generated_spec impl num) :=
sorry
def implementation (num: String) : Int :=
let IsPrimeHexDigit (c: Char): Int := if c = '2' ∨ c = '3' ∨ c = '5' ∨ c = '7' ∨ c = 'B' ∨ c = 'D' then 1 else 0; num.foldl (λ acc a => acc + (IsPrimeHexDigit a)) 0
-- #test implementation "AB" = 1 -- #test implementation "1077E" = 2 -- #test implementation "ABED1A33" = 4 -- #test implementation "2020" = 2 -- #test implementation "123456789ABCDEF0" = 6 -- #test implementation "112233445566778899AABBCCDDEEFF00" = 12 -- #test implementation "" = 0
theorem correctness (num: String) : problem_spec implementation num :=
by sorry
null
null
null
def decimal_to_binary(decimal: nat) -> string
You will be given a number in decimal form and your task is to convert it to binary format. The function should return a string, with each character representing a binary number. Each character in the string will be '0' or '1'. There will be an extra couple of characters 'db' at the beginning and at the end of the string. The extra characters are there to help with the format.
[{"input": 15, "expected_output": "db1111db"}, {"input": 32, "expected_output": "db100000db"}]
def decimal_to_binary(decimal: nat) -> string """You will be given a number in decimal form and your task is to convert it to binary format. The function should return a string, with each character representing a binary number. Each character in the string will be '0' or '1'. There will be an extra couple of characters 'db' at the beginning and at the end of the string. The extra characters are there to help with the format. """
def problem_spec -- function signature (implementation: Nat β†’ String) -- inputs (decimal: Nat) := -- spec let spec (result: String) := 4 < result.length ∧ result.drop (result.length - 2) = "db" ∧ result.take 2 = "db" ∧ let resultTrimmed := (result.toList.drop 2).dropLast.dropLast.map (fun c => c.toNat - '0'.toNat) decimal = Nat.ofDigits 2 resultTrimmed.reverse -- program termination βˆƒ result, implementation decimal = result ∧ spec result
def generated_spec -- function signature (impl: Nat β†’ String) -- inputs (decimal: Nat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ decimal, problem_spec impl decimal) ↔ (βˆ€ decimal, generated_spec impl decimal) :=
sorry
def implementation (decimal: Nat) : String :=
"db" ++ (Nat.toDigits 2 decimal).asString ++ "db"
-- #test implementation 0 = "db0db" -- #test implementation 32 = "db100000db" -- #test implementation 103 = "db1100111db" -- #test implementation 15 = "db1111db"
theorem correctness (decimal: Nat) : problem_spec implementation decimal :=
by sorry
null
null
null
def is_happy(s: str) -> bool
You are given a string s. Your task is to check if the string is happy or not. A string is happy if its length is at least 3 and every 3 consecutive letters are distinct
[{"input": "a", "output": false}, {"input": "aa", "output": false}, {"input": "abcd", "output": true}, {"input": "aabb", "output": false}, {"input": "adb", "output": true}]
def is_happy(s: str) -> bool """You are given a string s. Your task is to check if the string is happy or not. A string is happy if its length is at least 3 and every 3 consecutive letters are distinct """
def problem_spec -- function signature (implementation: String β†’ Bool) -- inputs (s: String) := -- spec let spec (result : Bool) := result ↔ (3 ≀ s.length) ∧ Β¬ (βˆƒ i j, i < j ∧ j < s.length ∧ j - i ≀ 2 ∧ s.data.get! i = s.data.get! j) -- program termination βˆƒ result, implementation s = result ∧ spec result
def generated_spec -- function signature (impl: String β†’ Bool) -- inputs (s: String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ s, problem_spec impl s) ↔ (βˆ€ s, generated_spec impl s) :=
sorry
def implementation (s: String) : Bool :=
sorry
-- #test implementation "a" = False -- #test implementation "aa" = False -- #test implementation "abcd" = True -- #test implementation "aabb" = False -- #test implementation "adb" = True
theorem correctness (s: String) : problem_spec implementation s :=
by sorry
null
null
null
def numerical_letter_grade(grades: list[float]) -> list[str]
It is the last week of the semester and the teacher has to give the grades to students. The teacher has been making her own algorithm for grading. The only problem is, she has lost the code she used for grading. She has given you a list of GPAs for some students and you have to write a function that can output a list of letter grades using the following table: GPA | Letter grade 4.0 A+ > 3.7 A > 3.3 A- > 3.0 B+ > 2.7 B > 2.3 B- > 2.0 C+ > 1.7 C > 1.3 C- > 1.0 D+ > 0.7 D > 0.0 D- 0.0 E Note: I have included a hypothesis that Float is hashable, not sure if this will mess up proving attempts but we can modify it if so. Reviewer: please think if there's a better way. Example: grade_equation([4.0, 3, 1.7, 2, 3.5]) ==> ['A+', 'B', 'C-', 'C', 'A-']
[{"input": [4.0, 3, 1.7, 2, 3.5], "output": ["A+", "B", "C-", "C", "A-"]}]
def numerical_letter_grade(grades: list[float]) -> list[str] """It is the last week of the semester and the teacher has to give the grades to students. The teacher has been making her own algorithm for grading. The only problem is, she has lost the code she used for grading. She has given you a list of GPAs for some students and you have to write a function that can output a list of letter grades using the following table: GPA | Letter grade 4.0 A+ > 3.7 A > 3.3 A- > 3.0 B+ > 2.7 B > 2.3 B- > 2.0 C+ > 1.7 C > 1.3 C- > 1.0 D+ > 0.7 D > 0.0 D- 0.0 E Note: I have included a hypothesis that Float is hashable, not sure if this will mess up proving attempts but we can modify it if so. Reviewer: please think if there's a better way. Example: grade_equation([4.0, 3, 1.7, 2, 3.5]) ==> ['A+', 'B', 'C-', 'C', 'A-'] """
def problem_spec -- function signature (implementation: List Float β†’ List String) -- inputs (grades: List Float) := -- spec let grade_dict : List (Float Γ— String) := [ (4.0, "A+"), (3.7, "A"), (3.3, "A-"), (3.0, "B+"), (2.7, "B"), (2.3, "B-"), (2.0, "C+"), (1.7, "C"), (1.3, "C-"), (1.0, "D+"), (0.7, "D"), (0.0, "D-") ] let spec (result : List String) := grades.all (fun grade => 0.0 ≀ grade ∧ grade ≀ 4.0) ∧ result.length = grades.length ∧ βˆ€ i, i < grades.length β†’ let number_grade := grades[i]! let number_grade_keys := grade_dict.map (fun (g, _) => g) if 0.0 < number_grade then βˆƒ i : Nat, i < number_grade_keys.length ∧ number_grade_keys[i]! ≀ number_grade ∧ (βˆ€ k' : Nat, k' < number_grade_keys.length β†’ number_grade_keys[k']! ≀ number_grade β†’ number_grade_keys[k']! ≀ number_grade_keys[i]!) ∧ result[i]! = (grade_dict[i]!).snd else result[i]! = "E" -- program termination βˆƒ result, implementation grades = result ∧ spec result
def generated_spec -- function signature (impl: List Float β†’ List String) -- inputs (grades: List Float) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ grades, problem_spec impl grades) ↔ (βˆ€ grades, generated_spec impl grades) :=
sorry
def implementation (grades: List Float) : List String :=
sorry
-- #test implementation [4.0, 3, 1.7, 2, 3.5] = ['A+', 'B', 'C-', 'C', 'A-']
theorem correctness (grades: List Float) : problem_spec implementation grades :=
by sorry
null
null
null
def prime_length(s: str) -> bool
Write a function that takes a string and returns True if the string length is a prime number or False otherwise
[{"input": "Hello", "output": true}, {"input": "abcdcba", "output": true}, {"input": "kittens", "output": true}, {"input": "orange", "output": false}]
def prime_length(s: str) -> bool """Write a function that takes a string and returns True if the string length is a prime number or False otherwise """
def problem_spec -- function signature (implementation: String β†’ Bool) -- inputs (s: String) := -- spec let spec (result : Bool) := let is_prime (n: Nat) : Prop := Β¬ (βˆƒ k, 2 ≀ k ∧ k < n ∧ n % k = 0); result ↔ is_prime s.length -- program termination βˆƒ result, implementation s = result ∧ spec result
def generated_spec -- function signature (impl: String β†’ Bool) -- inputs (s: String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ s, problem_spec impl s) ↔ (βˆ€ s, generated_spec impl s) :=
sorry
def implementation (s: String) : Bool :=
Nat.Prime s.length
-- #test implementation "Hello" = True -- #test implementation "abcdcba" = True -- #test implementation "kittens" = True -- #test implementation "orange" = False
theorem correctness (s: String) : problem_spec implementation s :=
by unfold problem_spec let result := implementation s use result simp [result] simp [implementation] apply Iff.intro intro h_is_prime simp [Nat.prime_def] at h_is_prime intro x h_2_le_x h_x_lt_s_len have h_p' := h_is_prime.2 x by_contra h_s_len_mod_x have h_x_dvd_s_len: x ∣ s.length := by apply Nat.dvd_of_mod_eq_zero exact h_s_len_mod_x simp [h_x_dvd_s_len] at h_p' have h_x_eq_s_len_false: ¬ (x = s.length) := by linarith simp [h_x_eq_s_len_false] at h_p' have h_x_ne_1: ¬ (x = 1) := by linarith simp [h_x_ne_1] at h_p' sorry
null
null
null
def starts_one_ends(n: int) -> int
Given a positive integer n, return the count of the numbers of n-digit positive integers that start or end with 1. Note: For reviewer, I believe this is the most straightforward spec, and I am relying on Set cardianlity not being computable in general. The point of this problem is really to privide a formula. Note: But I guess a program that goes through each number and adds 1 will be the same as a program that computes in O(1) under this view.
[{"input": 1, "output": 1}, {"input": 2, "output": 18}]
def starts_one_ends(n: int) -> int """Given a positive integer n, return the count of the numbers of n-digit positive integers that start or end with 1. Note: For reviewer, I believe this is the most straightforward spec, and I am relying on Set cardianlity not being computable in general. The point of this problem is really to privide a formula. Note: But I guess a program that goes through each number and adds 1 will be the same as a program that computes in O(1) under this view. """
def problem_spec -- function signature (implementation: Nat β†’ Nat) -- inputs (n: Nat) := -- spec let spec (result : Nat) := 0 < n β†’ result = {k : β„• | 10 ^ (n - 1) ≀ k ∧ k < 10 ^ n ∧ (k.repr.front = '1' ∨ k.repr.back = '1')}.ncard -- program termination βˆƒ result, implementation n = result ∧ 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 2 = 18
theorem correctness (n: Nat) : problem_spec implementation n :=
by sorry
null
null
null
def solve(n: int) -> str
Given a positive integer N, return the total sum of its digits in binary.
[{"input": 1000, "output": "1"}, {"input": 150, "output": "110"}, {"input": 147, "output": "1100"}]
def solve(n: int) -> str """Given a positive integer N, return the total sum of its digits in binary. """
def problem_spec -- function signature (implementation: Nat β†’ String) -- inputs (n: Nat) := -- spec let spec (result : String) := 0 < n β†’ result.all (fun c => c = '0' ∨ c = '1') β†’ Nat.ofDigits 2 (result.data.map (fun c => if c = '0' then 0 else 1)).reverse = (Nat.digits 10 n).sum -- program termination βˆƒ result, implementation n = result ∧ spec result
def generated_spec -- function signature (impl: Nat β†’ String) -- inputs (n: Nat) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ n, problem_spec impl n) ↔ (βˆ€ n, generated_spec impl n) :=
sorry
def implementation (n: Nat) : String :=
sorry
-- #test implementation 1000 = "1" -- #test implementation 150 = "110" -- #test implementation 147 = "1100"
theorem correctness (n: Nat) : problem_spec implementation n :=
by sorry
null
null
null
def solve(n: list[int]) -> int
Given a non-empty list of integers lst, add the even elements that are at odd indices.
[{"input": [4, 2, 6, 7], "output": 2}]
def solve(n: list[int]) -> int """Given a non-empty list of integers lst, add the even elements that are at odd indices. """
def problem_spec -- function signature (implementation: List Int β†’ Int) -- inputs (lst: List Int) := -- spec let spec (result : Int) := lst.length = 0 β†’ result = 0 ∧ lst.length > 0 β†’ if lst.length > 1 then result = (if Even lst[1]! then lst[1]! else 0) + implementation (lst.drop 2) else result = (if Even lst[1]! then lst[1]! else 0) -- program termination βˆƒ result, implementation lst = result ∧ 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 [4, 2, 6, 7] = 2
theorem correctness (lst: List Int) : problem_spec implementation lst :=
by sorry
null
null
null
def anti_shuffle(s : str) -> str
""" Write a function that takes a string and returns an ordered version of it. Ordered version of string, is a string where all words (separated by space) are replaced by a new word where all the characters arranged in ascending order based on ascii value. Note: You should keep the order of words and blank spaces in the sentence.
[{"input": "Hi", "output": "Hi"}, {"input": "hello", "output": "ehllo"}, {"input": "Hello World!!!", "output": "Hello !!!Wdlor"}]
def anti_shuffle(s : str) -> str """""" Write a function that takes a string and returns an ordered version of it. Ordered version of string, is a string where all words (separated by space) are replaced by a new word where all the characters arranged in ascending order based on ascii value. Note: You should keep the order of words and blank spaces in the sentence. """
def problem_spec -- function signature (implementation: String β†’ String) -- inputs (s: String) := -- spec let spec (result : String) := result.length = s.length ∧ let words := result.split (fun c => c = ' '); let s_words := s.split (fun c => c = ' '); s_words.length = words.length ∧ βˆ€ i, i < words.length β†’ words[i]!.length = s_words[i]!.length ∧ ((βˆ€ j, j < words[i]!.length β†’ (words[i]!.data[j]! ∈ s_words[i]!.data ∧ s_words[i]!.data[j]! ∈ words[i]!.data ∧ words[i]!.data.count (words[i]!.data[j]!) = s_words[i]!.data.count (s_words[i]!.data[j]!))) ∧ List.Sorted Nat.le (words[i]!.data.map (fun c => c.val.toNat))) -- 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 "Hi" = "Hi" -- #test implementation "hello" = "ehllo" -- #test implementation "Hello World!!!" = "Hello !!!Wdlor"
theorem correctness (s: String) : problem_spec implementation s :=
by sorry
null
null
null
def get_coords_sorted(lst : list(list(int)), x : int) -> list((int, int))
You are given a 2 dimensional data, as a nested lists, which is similar to matrix, however, unlike matrices, each row may contain a different number of columns. Given lst, and integer x, find integers x in the list, and return list of tuples, [(x1, y1), (x2, y2) ...] such that each tuple is a coordinate - (row, columns), starting with 0. Sort coordinates initially by rows in ascending order. Also, sort coordinates of the row by columns in descending order.
[{"input": [[[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 1, 6], [1, 2, 3, 4, 5, 1]], 1], "output": ["(0", "0)", "(1", "4)", "(1", "0)", "(2", "5)", "(2", "0)"]}, {"input": [[], 1], "output": []}, {"input": [[[], [1], [1, 2, 3]], 3], "output": ["(2", "2)"]}]
def get_coords_sorted(lst : list(list(int)), x : int) -> list((int, int)) """You are given a 2 dimensional data, as a nested lists, which is similar to matrix, however, unlike matrices, each row may contain a different number of columns. Given lst, and integer x, find integers x in the list, and return list of tuples, [(x1, y1), (x2, y2) ...] such that each tuple is a coordinate - (row, columns), starting with 0. Sort coordinates initially by rows in ascending order. Also, sort coordinates of the row by columns in descending order. """
def problem_spec -- function signature (implementation: List (List Int) β†’ Int β†’ List (Nat Γ— Nat)) -- inputs (lst: List (List Int)) (x: Int) := -- spec let spec (result : List (Nat Γ— Nat)) := (βˆ€ i, i < result.length β†’ let (row, col) := result[i]! row < lst.length ∧ col < lst[row]!.length ∧ (lst[row]!)[col]! = x) ∧ (βˆ€α΅‰ (i < lst.length) (j < lst[i]!.length), (lst[i]!)[j]! = x β†’ (i, j) ∈ result) ∧ (result.map (fun (r, c) => r)).Sorted Nat.le ∧ (βˆ€ i < result.length, let (row, col) := result[i]! ((result.filter (fun (r, c) => r = row)).map (fun (r, c) => c)).Sorted (fun a b => a β‰₯ b)) -- program termination βˆƒ result, implementation lst x = result ∧ spec result
def generated_spec -- function signature (impl: List (List Int) β†’ Int β†’ List (Nat Γ— Nat)) -- inputs (lst: List (List Int)) (x: Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ lst x, problem_spec impl lst x) ↔ (βˆ€ lst x, generated_spec impl lst x) :=
sorry
def implementation (lst: List (List Int)) (x: Int) : List (Nat Γ— Nat) :=
sorry
-- #test implementation "Hi" = "Hi" -- #test implementation "hello" = "ehllo" -- #test implementation "Hello World!!!" = "Hello !!!Wdlor"
theorem correctness (lst: List (List Int)) (x: Int) : problem_spec implementation lst x :=
by sorry
null
null
null
def sort_array(lst : list(int)) -> list(int)
""" Given an array of non-negative integers, return a copy of the given array after sorting, you will sort the given array in ascending order if the sum( first index value, last index value) is odd, or sort it in descending order if the sum( first index value, last index value) is even. Note(George): I have elected to ignore the copy part.
[{"input": [], "output": []}, {"input": [5], "output": [5]}, {"input": [2, 4, 3, 0, 1, 5], "output": [0, 1, 2, 3, 4, 5]}, {"input": [2, 4, 3, 0, 1, 5, 6], "output": [6, 5, 4, 3, 2, 1, 0]}]
def sort_array(lst : list(int)) -> list(int) """""" Given an array of non-negative integers, return a copy of the given array after sorting, you will sort the given array in ascending order if the sum( first index value, last index value) is odd, or sort it in descending order if the sum( first index value, last index value) is even. Note(George): I have elected to ignore the copy part. """
def problem_spec -- function signature (implementation: List Nat β†’ List Nat) -- inputs (lst: List Nat) := -- spec let spec (result : List Nat) := lst.length > 0 β†’ result.length = lst.length ∧ (βˆ€ i, i < result.length β†’ result[i]! ∈ lst ∧ lst[i]! ∈ result ∧ result.count (lst[i]!) = lst.count (lst[i]!)) ∧ (lst.head! + lst.getLast!) ≑ 1 [MOD 2] β†’ result.Sorted Nat.le ∧ (lst.head! + lst.getLast!) ≑ 0 [MOD 2] β†’ result.Sorted (fun a b => a β‰₯ b) -- 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 [] = [] -- #test implementation [5] = [5] -- #test implementation [2, 4, 3, 0, 1, 5] = [0, 1, 2, 3, 4, 5] -- #test implementation [2, 4, 3, 0, 1, 5, 6] = [6, 5, 4, 3, 2, 1, 0]
theorem correctness (lst: List Nat) : problem_spec implementation lst :=
by sorry
null
null
null
def encrypt(str : str) -> str
Create a function encrypt that takes a string as an argument and returns a string encrypted with the alphabet being rotated. The alphabet should be rotated in a manner such that the letters shift down by two multiplied to two places.
[{"input": "hi", "output": "lm"}, {"input": "asdfghjkl", "output": "ewhjklnop"}, {"input": "gf", "output": "kj"}, {"input": "et", "output": "ix"}]
def encrypt(str : str) -> str """Create a function encrypt that takes a string as an argument and returns a string encrypted with the alphabet being rotated. The alphabet should be rotated in a manner such that the letters shift down by two multiplied to two places. """
def problem_spec -- function signature (implementation: String β†’ String) -- inputs (str: String) := -- spec let spec (result : String) := result.data.all (fun c => c.isLower) β†’ result.length = str.length ∧ (βˆ€ i, i < str.length β†’ let c := str.data[i]! let c' := result.data[i]! ((c'.toNat - 97) + 2 * 2) % 26 = (c.toNat - 97)) -- program termination βˆƒ result, implementation str = result ∧ spec result
def generated_spec -- function signature (impl: String β†’ String) -- inputs (str: String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ str, problem_spec impl str) ↔ (βˆ€ str, generated_spec impl str) :=
sorry
def implementation (str: String) : String :=
sorry
-- #test implementation "hi" = "lm" -- #test implementation "asdfghjkl" = "ewhjklnop" -- #test implementation "gf" = "kj" -- #test implementation "et" = "ix"
theorem correctness (str: String) : problem_spec implementation str :=
by sorry
null
null
null
def next_smallest(lst: List[int]) -> Optional[int]
You are given a list of integers. Write a function next_smallest() that returns the 2nd smallest element of the list. Return None if there is no such element. TODO(George): Remove this when being reviewed The spec is defined as: if result is none there is no second smallest element, which exists in a finite list iff there are at least two distinct elements in the list. If result is some x, then x is the second smallest element of the list, the spec obtains the sublist of elements smaller than the result, and checks that this sublist does not contain two distinct elements (they are all the same).
[{"input": [1, 2, 3, 4, 5], "output": 2}, {"input": [5, 1, 4, 3, 2], "output": 2}, {"input": [], "output": "None"}, {"input": [1, 1], "output": "None"}]
def next_smallest(lst: List[int]) -> Optional[int] """You are given a list of integers. Write a function next_smallest() that returns the 2nd smallest element of the list. Return None if there is no such element. TODO(George): Remove this when being reviewed The spec is defined as: if result is none there is no second smallest element, which exists in a finite list iff there are at least two distinct elements in the list. If result is some x, then x is the second smallest element of the list, the spec obtains the sublist of elements smaller than the result, and checks that this sublist does not contain two distinct elements (they are all the same). """
def problem_spec -- function signature (implementation: List Int β†’ Option Int) -- inputs (lst: List Int) := -- spec let spec (result : Option Int) := match result with | none => Β¬ (βˆƒ i j, i < lst.length ∧ j < lst.length ∧ i β‰  j ∧ lst.get! i < lst.get! j) | some result => let smaller_els := lst.filter (Β· < result); 0 < smaller_els.length ∧ smaller_els.all (Ξ» x => x = smaller_els.get! 0); -- program termination βˆƒ result, implementation lst = result ∧ spec result
def generated_spec -- function signature (impl: List 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 :=
sorry
-- #test implementation [1, 2, 3, 4, 5] = some 2 -- #test implementation [5, 1, 4, 3, 2] = some 2 -- #test implementation [] = none -- #test implementation [1, 1] = none
theorem correctness (lst: List Int) : problem_spec implementation lst :=
by sorry
null
null
null
def is_bored(s: str) -> int
You'll be given a string of words, and your task is to count the number of boredoms. A boredom is a sentence that starts with the word "I". Sentences are delimited by '.', '?' or '!'.
[{"input": "Hello world", "expected_output": 0}, {"input": "The sky is blue. The sun is shining. I love this weather", "expected_output": 1}]
def is_bored(s: str) -> int """You'll be given a string of words, and your task is to count the number of boredoms. A boredom is a sentence that starts with the word "I". Sentences are delimited by '.', '?' or '!'. """
def problem_spec -- function signature (implementation: String β†’ Nat) -- inputs (s: String) := -- spec let spec (result : Nat) := let is_sentence_is_boredom (s: String) : Bool := (s.startsWith "I " ∨ s.startsWith " I") ∧ '.' βˆ‰ s.data ∧ '?' βˆ‰ s.data ∧ '!' βˆ‰ s.data match s.data.findIdx? (Ξ» c => c = '.' ∨ c = '?' ∨ c = '!') with | some i => let j := i + 1; let substring := s.drop j; result = (if is_sentence_is_boredom substring then 1 else 0) + implementation substring | none => result = if is_sentence_is_boredom s then 1 else 0 -- program termination βˆƒ result, implementation s = result ∧ spec result
def generated_spec -- function signature (impl: String β†’ Nat) -- inputs (s: String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ s, problem_spec impl s) ↔ (βˆ€ s, generated_spec impl s) :=
sorry
def implementation (s: String) : Nat :=
sorry
-- #test implementation "Hello world" = 0 -- #test implementation "The sky is blue. The sun is shining. I love this weather" = 1
theorem correctness (s: String) : problem_spec implementation s :=
by sorry
null
null
null
def any_int(a: float, b: float, c: float) -> bool
Create a function that takes 3 numbers. Returns true if one of the numbers is equal to the sum of the other two, and all numbers are integers. Returns false in any other cases.
[{"input": [5, 2, 7], "expected_output": true}, {"input": [3, 2, 2], "expected_output": false}, {"input": [3, -2, 1], "expected_output": true}, {"input": [3.6, -2.2, 2], "expected_output": false}]
def any_int(a: float, b: float, c: float) -> bool """Create a function that takes 3 numbers. Returns true if one of the numbers is equal to the sum of the other two, and all numbers are integers. Returns false in any other cases. """
def problem_spec -- function signature (implementation: Rat β†’ Rat β†’ Rat β†’ Bool) -- inputs (a: Rat) (b: Rat) (c: Rat) := -- spec let spec (result : Bool) := let nums := [a, b, c]; result = true ↔ βˆƒ i j k : β„•, {i, j, k} βŠ† ({0, 1, 2} : Set β„•) ∧ i β‰  j ∧ j β‰  k ∧ k β‰  i ∧ (nums[i]! + nums[j]! = nums[k]!) ∧ a.den = 1 ∧ a.den = b.den ∧ a.den = c.den -- program termination βˆƒ result, implementation a b c = result ∧ spec result
def generated_spec -- function signature (impl: Rat β†’ Rat β†’ Rat β†’ Bool) -- inputs (a: Rat) (b: Rat) (c: Rat) : 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: Rat) (b: Rat) (c: Rat) : Bool :=
sorry
-- #test implementation 5 2 7 = true -- #test implementation 3 2 2 = false -- #test implementation 3 (-2) 1 = true -- #test implementation 3.6 (-2.2) 2 = false
theorem correctness (a: Rat) (b: Rat) (c: Rat) : problem_spec implementation a b c :=
by sorry
null
null
null
def encode(s : str) -> str
Write a function that takes a message, and encodes in such a way that it swaps case of all letters, replaces all vowels in the message with the letter that appears 2 places ahead of that vowel in the english alphabet. Assume only letters.
[{"input": "test", "expected_output": "TGST"}, {"input": "This is a message", "expected_output": "tHKS KS C MGSSCGG"}]
def encode(s : str) -> str """Write a function that takes a message, and encodes in such a way that it swaps case of all letters, replaces all vowels in the message with the letter that appears 2 places ahead of that vowel in the english alphabet. Assume only letters. """
def problem_spec -- function signature (implementation: String β†’ String) -- inputs (s: String) := -- spec let spec (result : String) := s.data.all (Ξ» c => c.isAlpha) β†’ result.length = s.length ∧ (βˆ€ i, i < s.length β†’ let c := s.data.get! i; let c' := result.data.get! i; match c with | 'a' | 'e' | 'i' | 'o' | 'u' | 'A' | 'E' | 'I' | 'O' | 'U' => c.isUpper β†’ c'.val = c.toLower.val + 2 ∧ c.isLower β†’ c'.val = c.toUpper.val + 2 | _ => c.isUpper β†’ c' = c.toLower ∧ c.isLower β†’ c' = c.toUpper) -- 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 "test" = "TGST" -- #test implementation "This is a message" = "tHKS KS C MGSSCGG"
theorem correctness (s: String) : problem_spec implementation s :=
by sorry
null
null
null
def sum_largest_prime(lst : list[int]) -> int
You are given a list of integers. You need to find the largest prime value and return the sum of its digits. Note(George): Modified to use List of nats because all examples are nats.
[{"input": [0, 3, 2, 1, 3, 5, 7, 4, 5, 5, 5, 2, 181, 32, 4, 32, 3, 2, 32, 324, 4, 3], "expected_output": 10}, {"input": [1, 0, 1, 8, 2, 4597, 2, 1, 3, 40, 1, 2, 1, 2, 4, 2, 5, 1], "expected_output": 25}, {"input": [1, 3, 1, 32, 5107, 34, 83278, 109, 163, 23, 2323, 32, 30, 1, 9, 3], "expected_output": 13}, {"input": [0, 724, 32, 71, 99, 32, 6, 0, 5, 91, 83, 0, 5, 6], "expected_output": 11}, {"input": [0, 81, 12, 3, 1, 21], "expected_output": 3}, {"input": [0, 8, 1, 2, 1, 7], "expected_output": 7}]
def sum_largest_prime(lst : list[int]) -> int """You are given a list of integers. You need to find the largest prime value and return the sum of its digits. Note(George): Modified to use List of nats because all examples are nats. """
def problem_spec -- function signature (implementation: List Nat β†’ Nat) -- inputs (lst: List Nat) := -- spec let spec (result : Nat) := lst.any (fun num => Nat.Prime num) β†’ result > 0 ∧ βˆƒ i, i < lst.length ∧ Prime (lst.get! i) ∧ (βˆ€ j, j < lst.length ∧ Prime (lst.get! j) β†’ lst.get! i ≀ lst.get! j) ∧ result = (Nat.digits 10 (lst.get! i)).sum -- program termination βˆƒ result, implementation lst = result ∧ spec result
def generated_spec -- function signature (impl: List Nat β†’ 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) : Nat :=
sorry
-- #test implementation [0,3,2,1,3,5,7,4,5,5,5,2,181,32,4,32,3,2,32,324,4,3] = 10 -- #test implementation [1,0,1,8,2,4597,2,1,3,40,1,2,1,2,4,2,5,1] = 25 -- #test implementation [1,3,1,32,5107,34,83278,109,163,23,2323,32,30,1,9,3] = 13 -- #test implementation [0,724,32,71,99,32,6,0,5,91,83,0,5,6] = 11 -- #test implementation [0,81,12,3,1,21] = 3 -- #test implementation [0,8,1,2,1,7] = 7
theorem correctness (lst: List Nat) : problem_spec implementation lst :=
by sorry
null
null
null
def check_dict_case(s : dict[str, str]) -> bool
Given a dictionary, return True if all keys are strings in lower case or all keys are strings in upper case, else return False. The function should return False is the given dictionary is empty. Note(George): Modified the problem to use strings only for both keys and values.
[{"input": {"a": "apple", "b": "banana"}, "expected_output": true}, {"input": {"a": "apple", "A": "banana", "B": "banana"}, "expected_output": false}, {"input": {"a": "apple", "b": "banana"}, "expected_output": false}, {"input": {"Name": "John", "Age": "36", "City": "Houston"}, "expected_output": false}, {"input": {"STATE": "NC", "ZIP": "12345"}, "expected_output": true}]
def check_dict_case(s : dict[str, str]) -> bool """Given a dictionary, return True if all keys are strings in lower case or all keys are strings in upper case, else return False. The function should return False is the given dictionary is empty. Note(George): Modified the problem to use strings only for both keys and values. """
def problem_spec -- function signature (implementation: Std.HashMap String String β†’ Bool) -- inputs (D: Std.HashMap String String) := -- spec let spec (result : Bool) := match D.isEmpty with | true => result = false | false => let keys := D.keys let all_lower := keys.all (fun s => s.toLower = s) let all_upper := keys.all (fun s => s.toUpper = s) result = true ↔ all_lower || all_upper -- program termination βˆƒ result, implementation D = result ∧ spec result
def generated_spec -- function signature (impl: Std.HashMap String String β†’ Bool) -- inputs (D: Std.HashMap String String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ D, problem_spec impl D) ↔ (βˆ€ D, generated_spec impl D) :=
sorry
def implementation (D: Std.HashMap String String) : Bool :=
sorry
-- #test implementation (Std.HashMap.ofList [("a", "apple"), ("b", "banana")]) = true -- #test implementation (Std.HashMap.ofList [("a", "apple"), ("A", "banana"), ("B", "banana")]) = false -- #test implementation (Std.HashMap.ofList [("a", "apple"), ("b", "banana"), ("a", "apple")]) = false -- #test implementation (Std.HashMap.ofList [("Name", "John"), ("Age", "36"), ("City", "Houston")]) = false -- #test implementation (Std.HashMap.ofList [("STATE", "NC"), ("ZIP", "12345")]) = true
theorem correctness (D: Std.HashMap String String) : problem_spec implementation D :=
by sorry
null
null
null
def count_up_to(n : int) -> list[int]
Implement a function that takes an non-negative integer and returns an array of the first n integers that are prime numbers and less than n.
[{"input": 5, "expected_output": [2, 3]}, {"input": 11, "expected_output": [2, 3, 5, 7]}, {"input": 0, "expected_output": []}, {"input": 20, "expected_output": [2, 3, 5, 7, 11, 13, 17, 19]}, {"input": 1, "expected_output": []}, {"input": 18, "expected_output": [2, 3, 5, 7, 11, 13, 17]}]
def count_up_to(n : int) -> list[int] """Implement a function that takes an non-negative integer and returns an array of the first n integers that are prime numbers and less than n. """
def problem_spec -- function signature (implementation: Nat β†’ List Nat) -- inputs (n: Nat) := -- spec let spec (result : List Nat) := match n with | 0 => result = [] | n => n > 0 β†’ (βˆ€ i, i < result.length β†’ (Nat.Prime (result.get! i)) ∧ (result.get! i) < n) ∧ (βˆ€ i : Nat, i < n β†’ Nat.Prime i β†’ i ∈ result) -- program termination βˆƒ result, implementation n = result ∧ 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 = [2,3] -- #test implementation 11 = [2,3,5,7] -- #test implementation 0 = [] -- #test implementation 20 = [2,3,5,7,11,13,17,19] -- #test implementation 1 = [] -- #test implementation 18 = [2,3,5,7,11,13,17]
theorem correctness (n: Nat) : problem_spec implementation n :=
by sorry
null
null
null
def multiply(a : Int, b : Int) -> Int
Complete the function that takes two integers and returns the product of their unit digits. Assume the input is always valid. -- Note(George): I'm finding it hard to not leak the implementation here, so I opted to make the spec more convoluted.
[{"input": "148, 412", "expected_output": 16}, {"input": "19, 28", "expected_output": 72}, {"input": "2020, 1851", "expected_output": 0}, {"input": "14, -15", "expected_output": 20}]
def multiply(a : Int, b : Int) -> Int """Complete the function that takes two integers and returns the product of their unit digits. Assume the input is always valid. -- Note(George): I'm finding it hard to not leak the implementation here, so I opted to make the spec more convoluted. """
def problem_spec -- function signature (implementation: Int β†’ Int β†’ Int) -- inputs (a b: Int) := -- spec let spec (result : Int) := |result| ≀ 81 ∧ result % 10 = (a * b) % 10 ∧ ((b%10) β‰  0 β†’ (result % (b%10) = 0 ∧ (result/ (b%10)) % 100 = (a%10))) ∧ ((a%10) β‰  0 β†’ (result % (a%10) = 0 ∧ (result/ (a%10)) % 100 = (b%10))) ∧ ((a%10 = 0) ∨ (b%10 = 0) β†’ result = 0) -- program termination βˆƒ result, implementation a b = result ∧ spec result
def generated_spec -- function signature (impl: Int β†’ Int β†’ Int) -- inputs (a b: Int) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ a b, problem_spec impl a b) ↔ (βˆ€ a b, generated_spec impl a b) :=
sorry
def implementation (a b: Int) : Int :=
sorry
-- #test implementation 148 412 = 16 -- #test implementation 19 28 = 72 -- #test implementation 2020 1851 = 0 -- #test implementation 14 -15 = 20
theorem correctness (a b: Int) : problem_spec implementation a b :=
by sorry
null
null
null
def count_upper(s : String) -> Int
Given a string s, count the number of uppercase vowels in even indices. -- Note(George): I also feel like this one is hard to not leak, I tried a trick about keeping implementation for a recursive call in the spec. Let me know if this doesn't work..
[{"input": "aBCdEf", "expected_output": 1}, {"input": "abcdefg", "expected_output": 0}, {"input": "dBBE", "expected_output": 0}]
def count_upper(s : String) -> Int """Given a string s, count the number of uppercase vowels in even indices. -- Note(George): I also feel like this one is hard to not leak, I tried a trick about keeping implementation for a recursive call in the spec. Let me know if this doesn't work.. """
def problem_spec -- function signature (implementation: String β†’ Int) -- inputs (s: String) := -- spec let uppercase_vowels := ['A', 'E', 'I', 'O', 'U'] let spec (result : Int) := let chars := s.toList (result = 0 ↔ βˆ€ i, i < chars.length β†’ chars[i]! βˆ‰ uppercase_vowels) ∧ (1 < chars.length β†’ (result - 1 = implementation (chars.drop 2).toString ↔ chars[0]! ∈ uppercase_vowels) ∨ (result = implementation (chars.drop 2).toString ↔ chars[0]! βˆ‰ uppercase_vowels) ) -- program termination βˆƒ result, implementation s = result ∧ spec result
def generated_spec -- function signature (impl: String β†’ Int) -- inputs (s: String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ s, problem_spec impl s) ↔ (βˆ€ s, generated_spec impl s) :=
sorry
def implementation (s: String) : Int :=
sorry
-- #test implementation "aBCdEf" = 1 -- #test implementation "abcdefg" = 0 -- #test implementation "dBBE" = 0
theorem correctness (s: String) : problem_spec implementation s :=
by sorry
null
null
null
def closest_integer(s : String) -> Option Int
Create a function that takes a value (string) representing a number and returns the closest integer to it. If the number is equidistant from two integers, round it away from zero.
[{"input": "10", "expected_output": 10}, {"input": "15.3", "expected_output": 15}]
def closest_integer(s : String) -> Option Int """Create a function that takes a value (string) representing a number and returns the closest integer to it. If the number is equidistant from two integers, round it away from zero. """
def problem_spec -- function signature (implementation: String β†’ Option Int) -- inputs (s: String) := -- spec let numeric_characters := ["-", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "."] let is_valid_string := s.length > 0 ∧ s.count (".".get! 0) ≀ 1 ∧ s.count ("-".get! 0) ≀ 1 ∧ s.data.all (fun c => numeric_characters.contains (String.mk [c])) ∧ (s.count ("-".get! 0) = 1 β†’ s.data.head! = '-') let spec (result : Option Int) := match result with | some result => is_valid_string ∧ let parts := s.split (fun c => c = '.') (parts.length = 1 β†’ result = s.toInt!) ∧ (parts.length = 2 β†’ let integer_part := parts.get! 0 let is_negative := s.data.head! = '-' |((integer_part.toInt! - result) : β„š)| ≀ 0.5 ∧ (is_negative β†’ |((integer_part.toInt! - result) : β„š)| = 0.5 β†’ integer_part.toInt? < result) ∧ (Β¬is_negative β†’ |((integer_part.toInt! - result) : β„š)| = 0.5 β†’ integer_part.toInt? > result) ) | none => Β¬ is_valid_string -- program termination βˆƒ result, implementation s = result ∧ spec result
def generated_spec -- function signature (impl: String β†’ Option Int) -- inputs (s: String) : Prop :=
theorem spec_isomorphism: βˆ€ impl, (βˆ€ s, problem_spec impl s) ↔ (βˆ€ s, generated_spec impl s) :=
sorry
def implementation (s: String) : Option Int :=
sorry
-- #test implementation "10" = some 10 -- #test implementation "15.3" = some 15
theorem correctness (s: String) : problem_spec implementation s :=
by sorry
null
null
null
def make_a_pile(n: int) -> List[int]
Given a positive integer n, you have to make a pile of n levels of stones. The first level has n stones. The number of stones in the next level is: - the next odd number if n is odd. - the next even number if n is even. Return the number of stones in each level in a list, where element at index i represents the number of stones in the level (i+1).
[{"input": 3, "expected_output": [3, 5, 7]}]
def make_a_pile(n: int) -> List[int] """Given a positive integer n, you have to make a pile of n levels of stones. The first level has n stones. The number of stones in the next level is: - the next odd number if n is odd. - the next even number if n is even. Return the number of stones in each level in a list, where element at index i represents the number of stones in the level (i+1). """
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) β†’ (result[i]! = result[i-1]! + 2)) ∧ result[0]! = n -- 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 3 = [3, 5, 7] -- #test implementation 4 = [4,6,8,10] -- #test implementation 5 = [5, 7, 9, 11, 13] -- #test implementation 6 = [6, 8, 10, 12, 14, 16] -- #test implementation 8 = [8, 10, 12, 14, 16, 18, 20, 22]
theorem correctness (n: Int) : problem_spec implementation n :=
by sorry
null
null
null