Dataset Viewer
id
stringlengths 14
18
| description
stringlengths 321
1.77k
| lean_code
stringlengths 1.1k
7.34k
| signature
dict | metadata
dict | tests
sequence | reject_inputs
sequence | difficulty
stringclasses 2
values |
---|---|---|---|---|---|---|---|
verina_advanced_68 | -----Description-----
This task requires implementing a Run-Length Encoding (RLE) algorithm in Lean 4. The method should take a string as input and return a compressed string where consecutive duplicate characters are replaced by the character followed by its count. The output must strictly alternate between characters and digits, reconstruct to the original input when decoded, and return a non-empty string if and only if the input is non-empty.
-----Input-----
The input is a string consisting of any characters (including special characters and digits).
-----Output-----
The output is a string where each sequence of identical characters is replaced by the character followed by its count. The output must:
1. Alternate between characters and digits (e.g., "a3b2").
2. Reconstruct to the original input when decoded.
3. Be non-empty if and only if the input is non-empty.
| -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible]
def runLengthEncoder_precond (input : String) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def runLengthEncoder (input : String) (h_precond : runLengthEncoder_precond (input)) : String :=
-- !benchmark @start code
-- Convert string to character list
let chars : String β List Char := fun s => s.data
-- Check character equality
let charEq : Char β Char β Bool := fun c1 c2 => c1 == c2
-- Convert number to string
let numToString : Nat β String := fun n =>
let rec digits : Nat β List Char := fun n =>
if n < 10 then
[Char.ofNat (n + 48)] -- ASCII '0' is 48
else
digits (n / 10) ++ [Char.ofNat (n % 10 + 48)]
String.mk (digits n)
-- Main encoding logic (fixed version)
let rec encode : List Char β Option Char β Nat β String :=
fun input currentChar count =>
match input with
| [] =>
-- Process remaining characters
match currentChar with
| none => ""
| some c => String.mk [c] ++ numToString count
| c::rest =>
match currentChar with
| none => encode rest c 1
| some c' =>
if charEq c c' then
encode rest c' (count + 1)
else
let currentPart := String.mk [c'] ++ numToString count
currentPart ++ encode rest c 1
-- Handle empty input
if input.isEmpty then
""
else
let firstChar := (chars input).head?
encode (chars input).tail firstChar 1
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible]
def runLengthEncoder_postcond (input : String) (result: String) (h_precond : runLengthEncoder_precond (input)) : Prop :=
-- !benchmark @start postcond
-- Helper functions
let chars : String β List Char := fun s => s.data
-- Parse encoded string into (char, count) pairs
let parseEncodedString : String β List (Char Γ Nat) :=
let rec parseState : List Char β Option Char β Option Nat β List (Char Γ Nat) β List (Char Γ Nat) :=
fun remaining currentChar currentCount acc =>
match remaining with
| [] =>
-- Add final pair if we have both char and count
match currentChar, currentCount with
| some c, some n => (c, n) :: acc
| _, _ => acc
| c :: cs =>
if c.isDigit then
match currentChar with
| none => [] -- Invalid format: digit without preceding character
| some ch =>
-- Update current count
let digit := c.toNat - 48
let newCount :=
match currentCount with
| none => digit
| some n => n * 10 + digit
parseState cs currentChar (some newCount) acc
else
-- We found a new character, save previous pair if exists
let newAcc :=
match currentChar, currentCount with
| some ch, some n => (ch, n) :: acc
| _, _ => acc
parseState cs (some c) none newAcc
fun s =>
let result := parseState (chars s) none none []
result.reverse
-- Format check: characters followed by at least one digit
let formatValid : Bool :=
let rec checkPairs (chars : List Char) (nowDigit : Bool) : Bool :=
match chars with
| [] => true
| c :: cs =>
if nowDigit && c.isDigit then
checkPairs cs true
else
-- Need at least one digit after character
match cs with
| [] => false -- Ending with character, no digits
| d :: ds =>
if d.isDigit then
checkPairs ds true
else
false -- No digit after character
checkPairs (chars result) false
-- Content validation
let contentValid : Bool :=
let pairs := parseEncodedString result
let expanded := pairs.flatMap (fun (c, n) => List.replicate n c)
expanded == chars input
-- Empty check
let nonEmptyValid : Bool :=
input.isEmpty = result.isEmpty
formatValid && contentValid && nonEmptyValid
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem runLengthEncoder_spec_satisfied (input: String) (h_precond : runLengthEncoder_precond (input)) :
runLengthEncoder_postcond (input) (runLengthEncoder (input) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "runLengthEncoder",
"parameters": {
"param_name": [
"input"
],
"param_type": [
"String"
]
},
"return_type": "String"
} | {
"upstream": {
"name": "lab_assignment",
"link": "inspired by: https://leetcode.com/problems/string-compression/description/",
"task_id": "lab_runLengthEncoder_325008552",
"student_id": [
49
]
}
} | {
"input": [
"{\"input\": \"aaabbbcc\"}",
"{\"input\": \"!!!$$$%%%\"}",
"{\"input\": \"aaaaa\"}",
"{\"input\": \"abcd\"}",
"{\"input\": \"\"}",
"{\"input\": \"AaABb\"}",
"{\"input\": \"wwwwwwwwwwwwwwwww\"}",
"{\"input\": \"a\"}",
"{\"input\": \" \"}"
],
"expected": [
[
"a3b3c2"
],
[
"!3$3%3"
],
[
"a5"
],
[
"a1b1c1d1"
],
[
""
],
[
"A1a1A1B1b1"
],
[
"w17"
],
[
"a1"
],
[
" 2"
]
],
"unexpected": [
[
"a3b3",
"a3b3c2x",
"abc"
],
[
"!3$3%",
"!!!$$$%%",
"!3$3%4"
],
[
"a4",
"a6",
"a"
],
[
"abcd",
"a1b1c1",
"a1b1c1d2"
],
[
"a1",
" "
],
[
"AaABb",
"A1a1A1B1",
"A1a1A1B1b2"
],
[
"w16",
"w18",
"w"
],
[
"a",
"a2",
""
],
[
" ",
" 1",
" 3"
]
]
} | {
"input": []
} | advanced |
verina_basic_70 | -----Description-----
This task involves determining the first index in an array where a given condition holds true. The goal is to identify the position of the first element that meets a specified criterion, ensuring that no preceding element does.
-----Input-----
The input consists of:
β’ a: An array of elements (for testing purposes, you can assume it is an array of integers).
β’ P: A predicate function on the elements (represented as a string for test cases, e.g., "fun x => x > 5"). It is assumed that at least one element in the array satisfies P.
-----Output-----
The output is a natural number (Nat) which represents the index of the first element in the array that satisfies the predicate P.
β’ The index returned is less than the size of the array.
β’ The element at the returned index satisfies P.
β’ All elements before the returned index do not satisfy P.
-----Note-----
It is assumed that the array contains at least one element that satisfies P. In cases where this precondition does not hold, the behavior of the function is not guaranteed by the specification. | -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def LinearSearch3_precond (a : Array Int) (P : Int -> Bool) : Prop :=
-- !benchmark @start precond
β i, i < a.size β§ P (a[i]!)
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def LinearSearch3 (a : Array Int) (P : Int -> Bool) (h_precond : LinearSearch3_precond (a) (P)) : Nat :=
-- !benchmark @start code
let rec loop (n : Nat) : Nat :=
if n < a.size then
if P (a[n]!) then n else loop (n + 1)
else
0
loop 0
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def LinearSearch3_postcond (a : Array Int) (P : Int -> Bool) (result: Nat) (h_precond : LinearSearch3_precond (a) (P)) :=
-- !benchmark @start postcond
result < a.size β§ P (a[result]!) β§ (β k, k < result β Β¬ P (a[k]!))
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem LinearSearch3_spec_satisfied (a: Array Int) (P: Int -> Bool) (h_precond : LinearSearch3_precond (a) (P)) :
LinearSearch3_postcond (a) (P) (LinearSearch3 (a) (P) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "LinearSearch3",
"parameters": {
"param_name": [
"a",
"P"
],
"param_type": [
"Array Int",
"Int -> Bool"
]
},
"return_type": "Nat"
} | {
"upstream": {
"name": "clover",
"link": "https://github.com/ChuyueSun/Clover",
"task_id": "Clover_linear_search3",
"student_id": null
}
} | {
"input": [
"{\"a\": \"#[4, 7, 2, 9]\", \"P\": \"fun x => x > 5\"}",
"{\"a\": \"#[10, 8, 6, 4, 2]\", \"P\": \"fun x => x < 5\"}",
"{\"a\": \"#[5, 3, 1, 2]\", \"P\": \"fun x => x == 1\"}",
"{\"a\": \"#[0, 1, 2, 3]\", \"P\": \"fun x => x == 0\"}",
"{\"a\": \"#[9, 9, 9, 9]\", \"P\": \"fun x => x == 9\"}"
],
"expected": [
[
"1"
],
[
"3"
],
[
"2"
],
[
"0"
],
[
"0"
]
],
"unexpected": [
[
"0",
"2",
"3"
],
[
"0",
"1",
"4"
],
[
"0",
"1",
"3"
],
[
"1",
"2",
"3"
],
[
"1",
"2",
"3"
]
]
} | {
"input": [
"{'a': '#[1, 2, 3, 4, 5]', 'P': 'fun x => x > 10'}"
]
} | basic |
verina_advanced_42 | -----Description-----
This task requires writing a Lean 4 function that takes a list of stock prices and returns the maximum profit achievable by buying on one day and selling on a later day.
If no profit is possible, the function should return 0.
-----Input-----
The input consists of:
prices: A list of natural numbers representing stock prices on each day.
-----Output-----
The output is a natural number:
Returns the maximum profit achievable with one transaction (buy once, sell once), or 0 if no profitable transaction is possible.
| -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def maxProfit_precond (prices : List Nat) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
def updateMinAndProfit (price : Nat) (minSoFar : Nat) (maxProfit : Nat) : (Nat Γ Nat) :=
let newMin := Nat.min minSoFar price
let profit := if price > minSoFar then price - minSoFar else 0
let newMaxProfit := Nat.max maxProfit profit
(newMin, newMaxProfit)
def maxProfitAux (prices : List Nat) (minSoFar : Nat) (maxProfit : Nat) : Nat :=
match prices with
| [] => maxProfit
| p :: ps =>
let (newMin, newProfit) := updateMinAndProfit p minSoFar maxProfit
maxProfitAux ps newMin newProfit
-- !benchmark @end code_aux
def maxProfit (prices : List Nat) (h_precond : maxProfit_precond (prices)) : Nat :=
-- !benchmark @start code
match prices with
| [] => 0
| p :: ps => maxProfitAux ps p 0
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def maxProfit_postcond (prices : List Nat) (result: Nat) (h_precond : maxProfit_precond (prices)) : Prop :=
-- !benchmark @start postcond
(result = 0 β§ prices = []) β¨
(
-- All valid transactions have profit β€ result (using pairwise)
List.Pairwise (fun β¨pi, iβ© β¨pj, jβ© => i < j β pj - pi β€ result) prices.zipIdx β§
-- There exists a transaction with profit = result (using any)
prices.zipIdx.any (fun β¨pi, iβ© =>
prices.zipIdx.any (fun β¨pj, jβ© =>
i < j β§ pj - pi = result))
)
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem maxProfit_spec_satisfied (prices: List Nat) (h_precond : maxProfit_precond (prices)) :
maxProfit_postcond (prices) (maxProfit (prices) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "maxProfit",
"parameters": {
"param_name": [
"prices"
],
"param_type": [
"List Nat"
]
},
"return_type": "Nat"
} | {
"upstream": {
"name": "lab_assignment",
"link": "",
"task_id": "lab_maxProfit_324256904",
"student_id": [
34
]
}
} | {
"input": [
"{\"prices\": \"[7, 1, 5, 3, 6, 4]\"}",
"{\"prices\": \"[7, 6, 4, 3, 1]\"}",
"{\"prices\": \"[2, 4, 1]\"}",
"{\"prices\": \"[1, 2]\"}",
"{\"prices\": \"[]\"}"
],
"expected": [
[
"5"
],
[
"0"
],
[
"2"
],
[
"1"
],
[
"0"
]
],
"unexpected": [
[
"4",
"6"
],
[
"1",
"2"
],
[
"1"
],
[
"0"
],
[
"1"
]
]
} | {
"input": []
} | advanced |
verina_basic_100 | -----Description-----
This task involves determining the triple of a given integer. The goal is to create a function that, for any integer provided as input, returns a value equal to three times that integer, including handling the case when the input is zero.
-----Input-----
The input consists of:
β’ x: An integer.
-----Output-----
The output is an integer that represents three times the input integer.
β’ If x = 0, the output will be 0.
β’ Otherwise, the output will be computed as x + 2 * x, which is equivalent to 3 * x.
-----Note-----
There are no additional preconditions. It is assumed that x is a valid integer. | -- !benchmark @start import type=solution
import Mathlib
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def Triple_precond (x : Int) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def Triple (x : Int) (h_precond : Triple_precond (x)) : Int :=
-- !benchmark @start code
if x = 0 then 0 else
let y := 2 * x
x + y
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def Triple_postcond (x : Int) (result: Int) (h_precond : Triple_precond (x)) :=
-- !benchmark @start postcond
result / 3 = x β§ result / 3 * 3 = result
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem Triple_spec_satisfied (x: Int) (h_precond : Triple_precond (x)) :
Triple_postcond (x) (Triple (x) h_precond) h_precond := by
-- !benchmark @start proof
unfold Triple_postcond Triple
split_ifs with hβ
. rw [hβ]
simp
. simp
rw (occs := [1]) [βInt.one_mul x]
rw [βInt.add_mul]
simp +arith
-- !benchmark @end proof
| {
"name": "Triple",
"parameters": {
"param_name": [
"x"
],
"param_type": [
"Int"
]
},
"return_type": "Int"
} | {
"upstream": {
"name": "clover",
"link": "https://github.com/ChuyueSun/Clover",
"task_id": "Clover_triple3",
"student_id": null
}
} | {
"input": [
"{\"x\": 0}",
"{\"x\": 1}",
"{\"x\": -2}",
"{\"x\": 10}",
"{\"x\": -5}"
],
"expected": [
[
"0"
],
[
"3"
],
[
"-6"
],
[
"30"
],
[
"-15"
]
],
"unexpected": [
[
"1",
"-1",
"10"
],
[
"2",
"4",
"0"
],
[
"-4",
"-2",
"6"
],
[
"20",
"40",
"0"
],
[
"-10",
"-5",
"15"
]
]
} | {
"input": []
} | basic |
verina_basic_95 | -----Description-----
This problem involves swapping two elements in an array of integers at specified positions. Given an array and two indices, the task is to exchange these elements so that the element from the first index moves to the second index and vice versa, while all other elements remain unchanged.
-----Input-----
The input consists of:
β’ arr: An array of integers.
β’ i: An integer representing the first index (0-indexed) whose element is to be swapped.
β’ j: An integer representing the second index (0-indexed) whose element is to be swapped.
-----Output-----
The output is an array of integers which:
β’ Has the same size as the input array.
β’ Contains the element originally at index i in position j and the element originally at index j in position i.
β’ Leaves all other elements unchanged.
-----Note-----
It is assumed that both indices i and j are non-negative and within the bounds of the array (i.e., Int.toNat i and Int.toNat j are less than arr.size). | -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def swap_precond (arr : Array Int) (i : Int) (j : Int) : Prop :=
-- !benchmark @start precond
i β₯ 0 β§
j β₯ 0 β§
Int.toNat i < arr.size β§
Int.toNat j < arr.size
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def swap (arr : Array Int) (i : Int) (j : Int) (h_precond : swap_precond (arr) (i) (j)) : Array Int :=
-- !benchmark @start code
let i_nat := Int.toNat i
let j_nat := Int.toNat j
let arr1 := arr.set! i_nat (arr[j_nat]!)
let arr2 := arr1.set! j_nat (arr[i_nat]!)
arr2
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def swap_postcond (arr : Array Int) (i : Int) (j : Int) (result: Array Int) (h_precond : swap_precond (arr) (i) (j)) :=
-- !benchmark @start postcond
(result[Int.toNat i]! = arr[Int.toNat j]!) β§
(result[Int.toNat j]! = arr[Int.toNat i]!) β§
(β (k : Nat), k < arr.size β k β Int.toNat i β k β Int.toNat j β result[k]! = arr[k]!)
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem swap_spec_satisfied (arr: Array Int) (i: Int) (j: Int) (h_precond : swap_precond (arr) (i) (j)) :
swap_postcond (arr) (i) (j) (swap (arr) (i) (j) h_precond) h_precond := by
-- !benchmark @start proof
unfold swap_postcond swap
unfold swap_precond at h_precond
obtain β¨hβ, hβ, hβ, hββ© := h_precond
apply And.intro
. simp
by_cases h_eq : (i = j)
. rw [h_eq]
rw [Array.getElem!_eq_getD]
rw [Array.setIfInBounds]
simp [hβ]
. rw [Array.setIfInBounds_comm]
let arrβ := arr.setIfInBounds j.toNat arr[i.toNat]!
have haβ : arrβ = arr.setIfInBounds j.toNat arr[i.toNat]! := rfl
let arr_j := arr[j.toNat]!
have hi : arr_j = arr[j.toNat]! := rfl
rw [βhaβ, βhi]
have hβ' : i.toNat < (arrβ.setIfInBounds i.toNat arr_j).size := by
rw [haβ]
unfold Array.setIfInBounds
split
. simp
split
. simp
exact hβ
. simp
exact hβ
. split
. simp
exact hβ
. simp
exact hβ
rw [Array.getElem!_eq_getD]
unfold Array.getD
split
. simp
. simp
intro h
have h_contr : i = j := by
rw [β Int.toNat_of_nonneg hβ, β Int.toNat_of_nonneg hβ]
rw [h]
contradiction
. apply And.intro
. simp
by_cases h_eq : (i = j)
. rw [h_eq]
rw [Array.getElem!_eq_getD]
rw [Array.setIfInBounds]
simp [hβ]
. let arrβ := arr.setIfInBounds i.toNat arr[j.toNat]!
have haβ : arrβ = arr.setIfInBounds i.toNat arr[j.toNat]! := rfl
let arr_i := arr[i.toNat]!
have hi : arr_i = arr[i.toNat]! := rfl
rw [βhaβ, βhi]
have hβ' : j.toNat < (arrβ.setIfInBounds j.toNat arr_i).size := by
rw [haβ]
unfold Array.setIfInBounds
split
. simp
split
. simp
exact hβ
. simp
exact hβ
. split
. simp
exact hβ
. simp
exact hβ
rw [Array.getElem!_eq_getD]
unfold Array.getD
split
. simp
. rename_i h
contradiction
. simp
intro k hk hki hkj
let arrβ := (arr.setIfInBounds i.toNat arr[j.toNat]!)
let harrβ : arrβ = (arr.setIfInBounds i.toNat arr[j.toNat]!) := rfl
rw [βharrβ]
have hβ : arrβ[k]! = arr[k]! := by
rw [Array.getElem!_eq_getD]
rw [Array.getD]
simp
split
. rw [Array.getElem_setIfInBounds_ne arr arr[j.toNat]! hk]
rw [Array.getElem!_eq_getD]
rw [Array.getD]
simp
split
. rfl
. rfl
apply ne_comm.mp
exact hki
. rename_i h_contr
rw [harrβ] at h_contr
simp only [Array.size_setIfInBounds] at h_contr
contradiction
rw [Array.getElem!_eq_getD]
rw [Array.getD]
simp
split
. rw [Array.getElem_setIfInBounds_ne arrβ arr[i.toNat]!]
rw [βhβ]
rw [Array.getElem!_eq_getD]
rw [Array.getD]
simp
split
. simp
. simp
rename_i h
exact h
rw [ne_comm]
exact hkj
. rename_i h_contr
have h : arr.size = arrβ.size := by
rw [harrβ]
simp
rw [βh] at h_contr
contradiction
-- !benchmark @end proof
| {
"name": "swap",
"parameters": {
"param_name": [
"arr",
"i",
"j"
],
"param_type": [
"Array Int",
"Int",
"Int"
]
},
"return_type": "Array Int"
} | {
"upstream": {
"name": "clover",
"link": "https://github.com/ChuyueSun/Clover",
"task_id": "Clover_swap_in_array",
"student_id": null
}
} | {
"input": [
"{\"arr\": \"#[1, 2, 3, 4, 5]\", \"i\": 1, \"j\": 3}",
"{\"arr\": \"#[10, 20, 30, 40]\", \"i\": 0, \"j\": 3}",
"{\"arr\": \"#[7, 8, 9]\", \"i\": 1, \"j\": 2}",
"{\"arr\": \"#[1, 2, 3, 4]\", \"i\": 0, \"j\": 0}",
"{\"arr\": \"#[-1, -2, -3]\", \"i\": 0, \"j\": 2}"
],
"expected": [
[
"#[1, 4, 3, 2, 5]"
],
[
"#[40, 20, 30, 10]"
],
[
"#[7, 9, 8]"
],
[
"#[1, 2, 3, 4]"
],
[
"#[-3, -2, -1]"
]
],
"unexpected": [
[
"#[1, 2, 3, 4, 5]",
"#[1, 3, 2, 4, 5]"
],
[
"#[10, 40, 30, 20]",
"#[10, 20, 40, 30]"
],
[
"#[8, 7, 9]",
"#[9, 8, 7]"
],
[
"#[1, 2, 4, 3]",
"#[4, 2, 3, 1]"
],
[
"#[-1, -2, -3]",
"#[-3, -1, -2]"
]
]
} | {
"input": [
"{'arr': '#[1, 2, 3, 4]', 'i': -1, 'j': 2}"
]
} | basic |
verina_advanced_81 | -----Description-----
Implement a Lean 4 function that, given a list of integers, removes all duplicates and returns the resulting list in ascending order.
-----Input-----
The input consists of a single list of integers:
arr: A list of integers.
-----Output-----
The output is a list of integers:
Returns a list containing the unique elements of the input, sorted in ascending order. The returned list must not contain any duplicates, and every element in the output must appear in the original input list.
| -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def uniqueSorted_precond (arr : List Int) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def uniqueSorted (arr : List Int) (h_precond : uniqueSorted_precond (arr)) : List Int :=
-- !benchmark @start code
let rec insert (x : Int) (sorted : List Int) : List Int :=
match sorted with
| [] =>
[x]
| head :: tail =>
if x <= head then
x :: head :: tail
else
head :: insert x tail
let rec insertionSort (xs : List Int) : List Int :=
match xs with
| [] =>
[]
| h :: t =>
let sortedTail := insertionSort t
insert h sortedTail
let removeDups : List Int β List Int
| xs =>
let rec aux (remaining : List Int) (seen : List Int) (acc : List Int) : List Int :=
match remaining with
| [] =>
acc.reverse
| h :: t =>
if h β seen then
aux t seen acc
else
aux t (h :: seen) (h :: acc)
aux xs [] []
insertionSort (removeDups arr)
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def uniqueSorted_postcond (arr : List Int) (result: List Int) (h_precond : uniqueSorted_precond (arr)) : Prop :=
-- !benchmark @start postcond
List.isPerm arr.eraseDups result β§ List.Pairwise (Β· β€ Β·) result
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem uniqueSorted_spec_satisfied (arr: List Int) (h_precond : uniqueSorted_precond (arr)) :
uniqueSorted_postcond (arr) (uniqueSorted (arr) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "uniqueSorted",
"parameters": {
"param_name": [
"arr"
],
"param_type": [
"List Int"
]
},
"return_type": "List Int"
} | {
"upstream": {
"name": "lab_assignment",
"link": "",
"task_id": "lab_uniqueSorted_325097530",
"student_id": [
17
]
}
} | {
"input": [
"{\"arr\": \"[1, 1, 2, 3]\"}",
"{\"arr\": \"[3, 3, 3]\"}",
"{\"arr\": \"[]\"}",
"{\"arr\": \"[5, 2, 2, 5]\"}",
"{\"arr\": \"[1, 2, 3, 4, 5]\"}"
],
"expected": [
[
"[1, 2, 3]"
],
[
"[3]"
],
[
"[]"
],
[
"[2, 5]"
],
[
"[1, 2, 3, 4, 5]"
]
],
"unexpected": [
[
"[1, 1, 2, 3]",
"[2, 3, 1]",
"[1, 3, 2]"
],
[
"[3, 3, 3]",
"[3, 3]",
"[3, 3, 3, 3]"
],
[
"[0]",
"[1]",
"[999]"
],
[
"[5, 2]",
"[2, 2, 5]",
"[2]"
],
[
"[1, 2, 3]",
"[2, 3, 4, 5]",
"[5, 4, 3, 2, 1]"
]
]
} | {
"input": []
} | advanced |
verina_basic_46 | -----Description-----
This task requires writing a Lean 4 method that finds the last occurrence of a specified element in a sorted array of integers. The method should return the index corresponding to the last occurrence of the element if it is present; if the element is absent, it should return -1. Additionally, the array must remain unchanged after the method is executed.
-----Input-----
The input consists of:
arr: A sorted array of integers in non-decreasing order.
elem: An integer whose last occurrence position is to be determined.
-----Output-----
The output is an integer:
Returns the index of the last occurrence of the specified integer in the array if it exists.
Returns -1 if the integer is not found in the array.
-----Note-----
The input array is assumed to be sorted in non-decreasing order and remains unchanged by the method. | -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def lastPosition_precond (arr : Array Int) (elem : Int) : Prop :=
-- !benchmark @start precond
List.Pairwise (Β· β€ Β·) arr.toList
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def lastPosition (arr : Array Int) (elem : Int) (h_precond : lastPosition_precond (arr) (elem)) : Int :=
-- !benchmark @start code
let rec loop (i : Nat) (pos : Int) : Int :=
if i < arr.size then
let a := arr[i]!
if a = elem then loop (i + 1) i
else loop (i + 1) pos
else pos
loop 0 (-1)
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def lastPosition_postcond (arr : Array Int) (elem : Int) (result: Int) (h_precond : lastPosition_precond (arr) (elem)) :=
-- !benchmark @start postcond
(result β₯ 0 β
arr[result.toNat]! = elem β§ (arr.toList.drop (result.toNat + 1)).all (Β· β elem)) β§
(result = -1 β arr.toList.all (Β· β elem))
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem lastPosition_spec_satisfied (arr: Array Int) (elem: Int) (h_precond : lastPosition_precond (arr) (elem)) :
lastPosition_postcond (arr) (elem) (lastPosition (arr) (elem) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "lastPosition",
"parameters": {
"param_name": [
"arr",
"elem"
],
"param_type": [
"Array Int",
"Int"
]
},
"return_type": "Int"
} | {
"upstream": {
"name": "dafny-synthesis",
"link": "https://github.com/Mondego/dafny-synthesis",
"task_id": "task_id_793",
"student_id": null
}
} | {
"input": [
"{\"arr\": \"#[1, 2, 2, 3, 4, 5]\", \"elem\": 2}",
"{\"arr\": \"#[1, 2, 2, 3, 4, 5]\", \"elem\": 6}",
"{\"arr\": \"#[1, 2, 2, 3, 4, 5]\", \"elem\": 5}",
"{\"arr\": \"#[1]\", \"elem\": 1}",
"{\"arr\": \"#[1, 1, 1, 1]\", \"elem\": 1}",
"{\"arr\": \"#[2, 2, 3, 3, 3]\", \"elem\": 3}"
],
"expected": [
[
"2"
],
[
"-1"
],
[
"5"
],
[
"0"
],
[
"3"
],
[
"4"
]
],
"unexpected": [
[
"0",
"1",
"3"
],
[
"0",
"1",
"2"
],
[
"3",
"4",
"0"
],
[
"1",
"-1",
"2"
],
[
"0",
"1",
"2"
],
[
"2",
"3",
"5"
]
]
} | {
"input": [
"{'arr': '#[3, 2, 1]', 'elem': 2}"
]
} | basic |
verina_basic_14 | -----Description-----
This task requires writing a Lean 4 method that determines whether a given string contains the character 'z' or 'Z'. The method should return true if the string includes either the lowercase or uppercase letter 'z', and false otherwise.
-----Input-----
The input consists of:
s: A string.
-----Output-----
The output is a Boolean value:
Returns true if the input string contains the character 'z' or 'Z'.
Returns false if the input string does not contain the character 'z' or 'Z'.
-----Note-----
There are no preconditions; the method will always work as strings and sequences are considered non-null. | -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def containsZ_precond (s : String) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def containsZ (s : String) (h_precond : containsZ_precond (s)) : Bool :=
-- !benchmark @start code
s.toList.any fun c => c = 'z' || c = 'Z'
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def containsZ_postcond (s : String) (result: Bool) (h_precond : containsZ_precond (s)) :=
-- !benchmark @start postcond
let cs := s.toList
(β x, x β cs β§ (x = 'z' β¨ x = 'Z')) β result
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem containsZ_spec_satisfied (s: String) (h_precond : containsZ_precond (s)) :
containsZ_postcond (s) (containsZ (s) h_precond) h_precond := by
-- !benchmark @start proof
unfold containsZ containsZ_postcond
simp_all
-- !benchmark @end proof
| {
"name": "containsZ",
"parameters": {
"param_name": [
"s"
],
"param_type": [
"String"
]
},
"return_type": "Bool"
} | {
"upstream": {
"name": "dafny-synthesis",
"link": "https://github.com/Mondego/dafny-synthesis",
"task_id": "task_id_454",
"student_id": null
}
} | {
"input": [
"{\"s\": \"hello\"}",
"{\"s\": \"zebra\"}",
"{\"s\": \"Zebra\"}",
"{\"s\": \"\"}",
"{\"s\": \"crazy\"}",
"{\"s\": \"AZ\"}",
"{\"s\": \"abc\"}",
"{\"s\": \"Zz\"}",
"{\"s\": \"no letter\"}"
],
"expected": [
[
"False"
],
[
"True"
],
[
"True"
],
[
"False"
],
[
"True"
],
[
"True"
],
[
"False"
],
[
"True"
],
[
"False"
]
],
"unexpected": [
[
"True"
],
[
"False"
],
[
"False"
],
[
"True"
],
[
"False"
],
[
"False"
],
[
"True"
],
[
"False"
],
[
"True"
]
]
} | {
"input": []
} | basic |
verina_basic_36 | -----Description-----
This task requires writing a Lean 4 method that takes a given string and returns a new string where every occurrence of a space, comma, or dot is replaced with a colon. The transformation must preserve the original stringβs length and leave all other characters unmodified.
-----Input-----
The input consists of:
s: A string.
-----Output-----
The output is a string:
- The returned string must have the same length as the input string.
- Every space, comma, or dot in the input string is replaced with a colon.
- All other characters remain unchanged.
-----Note-----
There are no preconditions; the input string is assumed to be non-null. | -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
def isSpaceCommaDot (c : Char) : Bool :=
if c = ' ' then true
else if c = ',' then true
else if c = '.' then true
else false
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def replaceWithColon_precond (s : String) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def replaceWithColon (s : String) (h_precond : replaceWithColon_precond (s)) : String :=
-- !benchmark @start code
let cs := s.toList
let cs' := cs.map (fun c => if isSpaceCommaDot c then ':' else c)
String.mk cs'
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def replaceWithColon_postcond (s : String) (result: String) (h_precond : replaceWithColon_precond (s)) :=
-- !benchmark @start postcond
let cs := s.toList
let cs' := result.toList
result.length = s.length β§
(β i, i < s.length β
(isSpaceCommaDot cs[i]! β cs'[i]! = ':') β§
(Β¬isSpaceCommaDot cs[i]! β cs'[i]! = cs[i]!))
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem replaceWithColon_spec_satisfied (s: String) (h_precond : replaceWithColon_precond (s)) :
replaceWithColon_postcond (s) (replaceWithColon (s) h_precond) h_precond := by
-- !benchmark @start proof
unfold replaceWithColon replaceWithColon_postcond
simp
constructor
Β· unfold String.length
simp
Β· intro i hi
have hi' : i < s.data.length := by
unfold String.length at hi
simp at hi
exact hi
constructor <;> simp_all
-- !benchmark @end proof
| {
"name": "replaceWithColon",
"parameters": {
"param_name": [
"s"
],
"param_type": [
"String"
]
},
"return_type": "String"
} | {
"upstream": {
"name": "dafny-synthesis",
"link": "https://github.com/Mondego/dafny-synthesis",
"task_id": "task_id_732",
"student_id": null
}
} | {
"input": [
"{\"s\": \"Hello, world. How are you?\"}",
"{\"s\": \"No-changes!\"}",
"{\"s\": \",. \"}",
"{\"s\": \"\"}"
],
"expected": [
[
"Hello::world::How:are:you?"
],
[
"No-changes!"
],
[
":::"
],
[
""
]
],
"unexpected": [
[
"Hello,world,How,are,you?",
"Hello: world: How: are: you?"
],
[
"No changes!",
"Noβchanges!"
],
[
"::",
";:;",
"::: "
],
[
" ",
"a"
]
]
} | {
"input": []
} | basic |
verina_advanced_64 | -----Description-----
This task requires writing a Lean 4 method that removes all occurrences of a given element from a list of natural numbers. The method should return a new list that contains all the elements of the original list except those equal to the target number. The order of the remaining elements must be preserved.
-----Input-----
The input consists of two elements:
lst: A list of natural numbers (List Nat).
target: A natural number to be removed from the list.
-----Output-----
The output is a list of natural numbers:
Returns a new list with all occurrences of the target number removed. The relative order of the remaining elements must be the same as in the input list.
| -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible]
def removeElement_precond (lst : List Nat) (target : Nat) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def removeElement (lst : List Nat) (target : Nat) (h_precond : removeElement_precond (lst) (target)) : List Nat :=
-- !benchmark @start code
let rec helper (lst : List Nat) (target : Nat) : List Nat :=
match lst with
| [] => []
| x :: xs =>
let rest := helper xs target
if x = target then rest else x :: rest
helper lst target
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible]
def removeElement_postcond (lst : List Nat) (target : Nat) (result: List Nat) (h_precond : removeElement_precond (lst) (target)): Prop :=
-- !benchmark @start postcond
-- 1. All elements equal to target are removed from the result.
-- 2. All other elements are preserved in order.
-- 3. No new elements are added.
-- Helper predicate: result contains exactly the elements of lst that are not equal to target, in order
let lst' := lst.filter (fun x => x β target)
result.zipIdx.all (fun (x, i) =>
match lst'[i]? with
| some y => x = y
| none => false) β§ result.length = lst'.length
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem removeElement_spec_satisfied (lst: List Nat) (target: Nat) (h_precond : removeElement_precond (lst) (target)):
removeElement_postcond (lst) (target) (removeElement (lst) (target) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "removeElement",
"parameters": {
"param_name": [
"lst",
"target"
],
"param_type": [
"List Nat",
"Nat"
]
},
"return_type": "List Nat"
} | {
"upstream": {
"name": "lab_assignment",
"link": "https://leetcode.com/problems/remove-element/",
"task_id": "lab_removeElement_323929890",
"student_id": [
47
]
}
} | {
"input": [
"{\"lst\": \"[1, 2, 3, 2, 4]\", \"target\": 2}",
"{\"lst\": \"[5, 5, 5, 5]\", \"target\": 5}",
"{\"lst\": \"[7, 8, 9]\", \"target\": 4}",
"{\"lst\": \"[]\", \"target\": 3}",
"{\"lst\": \"[0, 1, 0, 2, 0]\", \"target\": 0}"
],
"expected": [
[
"[1, 3, 4]"
],
[
"[]"
],
[
"[7, 8, 9]"
],
[
"[]"
],
[
"[1, 2]"
]
],
"unexpected": [
[
"[1, 2, 3, 4]",
"[1, 2, 3]",
"[1, 4]"
],
[
"[5]",
"[0]",
"[5, 5]"
],
[
"[]",
"[7, 8]",
"[8, 9]"
],
[
"[3]",
"[0]",
"[1, 2, 3]"
],
[
"[0, 1, 2]",
"[1]",
"[1, 0, 2]"
]
]
} | {
"input": []
} | advanced |
verina_basic_49 | -----Description-----
This task requires writing a Lean 4 method that searches an array of integers to locate the first odd number. The method should return a pair where the first element is a Boolean indicating whether an odd number was found, and the second element is the index of that odd number if found, or -1 if no odd number exists. When an odd number is found, the method should return the smallest index at which an odd number occurs.
-----Input-----
The input consists of:
a: An array of integers.
-----Output-----
The output is a pair (Bool, Int):
- If the Boolean is true, then the integer represents the smallest index of an odd number in the array.
- If the Boolean is false, then there are no odd numbers in the array, and the accompanying integer is -1.
-----Note-----
- The input array is assumed to be non-null.
- If multiple odd numbers are present, the index returned should correspond to the first occurrence. | -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
def isOdd (x : Int) : Bool :=
x % 2 β 0
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def findFirstOdd_precond (a : Array Int) : Prop :=
-- !benchmark @start precond
a.size > 0
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def findFirstOdd (a : Array Int) (h_precond : findFirstOdd_precond (a)) : Option Nat :=
-- !benchmark @start code
-- Creates list of (index, value) pairs
let indexed := a.toList.zipIdx
-- Find the first pair where the value is odd
let found := List.find? (fun (x, _) => isOdd x) indexed
-- Extract the index from the found pair (if any)
Option.map (fun (_, i) => i) found
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def findFirstOdd_postcond (a : Array Int) (result: Option Nat) (h_precond : findFirstOdd_precond (a)) :=
-- !benchmark @start postcond
match result with
| some idx => idx < a.size β§ isOdd (a[idx]!) β§
(β j, j < idx β Β¬ isOdd (a[j]!))
| none => β i, i < a.size β Β¬ isOdd (a[i]!)
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem findFirstOdd_spec_satisfied (a: Array Int) (h_precond : findFirstOdd_precond (a)) :
findFirstOdd_postcond (a) (findFirstOdd (a) h_precond) h_precond := by
-- !benchmark @start proof
unfold findFirstOdd findFirstOdd_postcond
let la := a.toList
have h_la : la = a.toList := by rfl
let indexed := la.zipIdx
have h_indexed : indexed = la.zipIdx := by rfl
let found := List.find? (fun (x, _) => isOdd x) indexed
have h_found : found = List.find? (fun (x, _) => isOdd x) indexed := by rfl
let res := Option.map (fun (_, i) => i) found
have h_res : res = Option.map (fun (_, i) => i) found := by rfl
simp_all
cases h_rescase : res with
| none =>
rw [β h_res, h_rescase]
simp
rw [h_rescase] at h_res
have h_notfound : found = none := by
rw [h_found]
exact Option.map_eq_none.mp h_rescase
rw [List.find?_eq_none] at h_notfound
simp at h_notfound
intro i hi
have hi' : i < la.length := by exact hi
have h_mem : (la[i], i) β indexed := by
have : la[i]? = some la[i] := by
exact (List.getElem?_eq_some_getElem_iff la i hi').mpr trivial
apply List.mem_zipIdx_iff_getElem?.mpr
simp
have hai : a[i]! = a[i] := by
exact getElem!_pos a i hi'
rw [hai]
exact h_notfound a[i] i h_mem
| some i =>
rw [β h_res, h_rescase]
rw [h_res] at h_rescase
simp
rw [Option.map_eq_some'] at h_rescase
rcases h_rescase with β¨p, β¨h_found', hpβ©β©
have h_mem : p β indexed := by
exact List.mem_of_find?_eq_some h_found'
have β¨_, hi, hxβ© := List.mem_zipIdx h_mem
have β¨h_odd, β¨i', hi', hii', h_prefixβ©β© := List.find?_eq_some_iff_getElem.mp h_found'
simp_all
have hai : a[i]! = a[i] := by
exact getElem!_pos a i hi
rw [hai]
constructor
Β· exact h_odd
Β· intro j hj
have hii' : i = i' := by
rw [β hii'] at hp
simp_all
have hj' : j < a.size := by
exact Nat.lt_trans hj hi
have haj : a[j]! = a[j] := by
exact getElem!_pos a j hj'
rw [haj]
rw [hii'] at hj
exact h_prefix j hj
-- !benchmark @end proof
| {
"name": "findFirstOdd",
"parameters": {
"param_name": [
"a"
],
"param_type": [
"Array Int"
]
},
"return_type": "Option Nat"
} | {
"upstream": {
"name": "dafny-synthesis",
"link": "https://github.com/Mondego/dafny-synthesis",
"task_id": "task_id_807",
"student_id": null
}
} | {
"input": [
"{\"a\": \"#[2, 4, 6, 8]\"}",
"{\"a\": \"#[3, 4, 6, 8]\"}",
"{\"a\": \"#[2, 4, 5, 8]\"}",
"{\"a\": \"#[7]\"}",
"{\"a\": \"#[2]\"}",
"{\"a\": \"#[1, 2, 3]\"}"
],
"expected": [
[
"none"
],
[
"some (0)"
],
[
"some (2)"
],
[
"some (0)"
],
[
"none"
],
[
"some (0)"
]
],
"unexpected": [
[
"some (0)"
],
[
"some (1)",
"some (2)",
"none"
],
[
"some (0)",
"some (1)",
"some (3)",
"none"
],
[
"some (1)",
"none"
],
[
"some (0)"
],
[
"some (1)",
"some (2)",
"none"
]
]
} | {
"input": [
"{'a': '#[]'}"
]
} | basic |
verina_basic_8 | -----Description-----
This task requires writing a Lean 4 method that determines the minimum of two integers. The method should return the smaller of the two numbers. When both numbers are equal, either one may be returned.
-----Input-----
The input consists of two integers:
a: The first integer.
b: The second integer.
-----Output-----
The output is an integer:
Returns the smaller value between the input integers, ensuring that the result is less than or equal to both inputs. | -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def myMin_precond (a : Int) (b : Int) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def myMin (a : Int) (b : Int) (h_precond : myMin_precond (a) (b)) : Int :=
-- !benchmark @start code
if a <= b then a else b
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def myMin_postcond (a : Int) (b : Int) (result: Int) (h_precond : myMin_precond (a) (b)) :=
-- !benchmark @start postcond
(result β€ a β§ result β€ b) β§
(result = a β¨ result = b)
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem myMin_spec_satisfied (a: Int) (b: Int) (h_precond : myMin_precond (a) (b)) :
myMin_postcond (a) (b) (myMin (a) (b) h_precond) h_precond := by
-- !benchmark @start proof
unfold myMin myMin_postcond
constructor
Β· split
case left.isTrue h =>
simp
exact h
case left.isFalse h =>
simp
rw [Int.not_le] at h
exact Int.le_of_lt h
Β· split <;> simp
-- !benchmark @end proof
| {
"name": "myMin",
"parameters": {
"param_name": [
"a",
"b"
],
"param_type": [
"Int",
"Int"
]
},
"return_type": "Int"
} | {
"upstream": {
"name": "dafny-synthesis",
"link": "https://github.com/Mondego/dafny-synthesis",
"task_id": "task_id_404",
"student_id": null
}
} | {
"input": [
"{\"a\": 3, \"b\": 5}",
"{\"a\": 10, \"b\": 7}",
"{\"a\": 4, \"b\": 4}",
"{\"a\": -3, \"b\": 5}",
"{\"a\": 3, \"b\": -5}",
"{\"a\": -3, \"b\": -5}",
"{\"a\": 0, \"b\": 10}",
"{\"a\": 0, \"b\": -10}"
],
"expected": [
[
"3"
],
[
"7"
],
[
"4"
],
[
"-3"
],
[
"-5"
],
[
"-5"
],
[
"0"
],
[
"-10"
]
],
"unexpected": [
[
"5",
"4",
"8"
],
[
"10",
"8",
"9"
],
[
"0",
"8",
"2"
],
[
"5",
"0",
"-5"
],
[
"3",
"0",
"-3"
],
[
"-3",
"0",
"-1"
],
[
"10",
"1",
"-1"
],
[
"0",
"-1",
"-5"
]
]
} | {
"input": []
} | basic |
verina_advanced_33 | -----Description-----
This task requires implementing the "Longest Increasing Subsequence" problem in Lean 4.
Given a list of integers, the function should compute the length of the longest strictly increasing
subsequence. A subsequence is formed by deleting zero or more elements without changing the order.
If the list is empty, the function should return 0.
-----Input-----
- nums: A list of integers.
-----Output-----
- A natural number representing the length of the longest strictly increasing subsequence.
- If there is no increasing subsequence, return 0.
| -- !benchmark @start import type=solution
import Mathlib.Data.List.Basic
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def longestIncreasingSubsequence_precond (nums : List Int) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def longestIncreasingSubsequence (nums : List Int) (h_precond : longestIncreasingSubsequence_precond (nums)) : Nat :=
-- !benchmark @start code
let max2 (a : Nat) (b : Nat) : Nat :=
if a > b then a else b
let rec listLength (l : List Int) : Nat :=
match l with
| [] => 0
| _ :: xs => 1 + listLength xs
let rec helper (lst : List Int) (prev : Option Int) : Nat :=
match lst with
| [] => 0
| h :: t =>
let canTake : Bool :=
if prev = none then true
else if prev.get! < h then true else false
let withTake : Nat :=
if canTake then 1 + helper t (some h) else 0
let withoutTake : Nat := helper t prev
max2 withTake withoutTake
let result := helper nums none
result
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def longestIncreasingSubsequence_postcond (nums : List Int) (result: Nat) (h_precond : longestIncreasingSubsequence_precond (nums)) : Prop :=
-- !benchmark @start postcond
let allSubseq := (nums.foldl fun acc x => acc ++ acc.map (fun sub => x :: sub)) [[]] |>.map List.reverse
let increasingSubseqLens := allSubseq.filter (fun l => List.Pairwise (Β· < Β·) l) |>.map (Β·.length)
increasingSubseqLens.contains result β§ increasingSubseqLens.all (Β· β€ result)
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem longestIncreasingSubsequence_spec_satisfied (nums: List Int) (h_precond : longestIncreasingSubsequence_precond (nums)) :
longestIncreasingSubsequence_postcond (nums) (longestIncreasingSubsequence (nums) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "longestIncreasingSubsequence",
"parameters": {
"param_name": [
"nums"
],
"param_type": [
"List Int"
]
},
"return_type": "Nat"
} | {
"upstream": {
"name": "lab_assignment",
"link": "[{\"text_file_id\"=>804930699}]",
"task_id": "lab_longestIncreasingSubsequence_325684656",
"student_id": [
27
]
}
} | {
"input": [
"{\"nums\": \"[10, 9, 2, 5, 3, 7, 101, 18]\"}",
"{\"nums\": \"[0, 1, 0, 3, 2, 3]\"}",
"{\"nums\": \"[7, 7, 7, 7, 7]\"}",
"{\"nums\": \"[]\"}",
"{\"nums\": \"[4, 10, 4, 3, 8, 9]\"}"
],
"expected": [
[
"4"
],
[
"4"
],
[
"1"
],
[
"0"
],
[
"3"
]
],
"unexpected": [
[
"3",
"5"
],
[
"3"
],
[
"0",
"2"
],
[
"1"
],
[
"2",
"4"
]
]
} | {
"input": []
} | advanced |
verina_basic_59 | -----Description-----
Given an integer x, determine a pair (a, b) where the first element is twice the value of x and the second element is four times the value of x.
-----Input-----
The input consists of:
β’ x: An integer.
-----Output-----
The output is a tuple (a, b) where:
β’ a = 2 * x
β’ b = 4 * x
-----Note-----
There are no additional preconditions; the method is defined for all integers. | -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def DoubleQuadruple_precond (x : Int) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def DoubleQuadruple (x : Int) (h_precond : DoubleQuadruple_precond (x)) : (Int Γ Int) :=
-- !benchmark @start code
let a := 2 * x
let b := 2 * a
(a, b)
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def DoubleQuadruple_postcond (x : Int) (result: (Int Γ Int)) (h_precond : DoubleQuadruple_precond (x)) :=
-- !benchmark @start postcond
result.fst = 2 * x β§ result.snd = 2 * result.fst
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem DoubleQuadruple_spec_satisfied (x: Int) (h_precond : DoubleQuadruple_precond (x)) :
DoubleQuadruple_postcond (x) (DoubleQuadruple (x) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "DoubleQuadruple",
"parameters": {
"param_name": [
"x"
],
"param_type": [
"Int"
]
},
"return_type": "(Int Γ Int)"
} | {
"upstream": {
"name": "clover",
"link": "https://github.com/ChuyueSun/Clover",
"task_id": "Clover_double_quadruple",
"student_id": null
}
} | {
"input": [
"{\"x\": 0}",
"{\"x\": 1}",
"{\"x\": -1}",
"{\"x\": 10}",
"{\"x\": -5}"
],
"expected": [
[
"(0, 0)"
],
[
"(2, 4)"
],
[
"(-2, -4)"
],
[
"(20, 40)"
],
[
"(-10, -20)"
]
],
"unexpected": [
[
"(1, 0)",
"(0, 1)",
"(-1, 0)"
],
[
"(2, 2)",
"(1, 4)",
"(3, 4)"
],
[
"(-2, -2)",
"(-1, -4)",
"(-3, -4)"
],
[
"(20, 20)",
"(10, 40)",
"(20, 0)"
],
[
"(-10, -10)",
"(-5, -20)",
"(-15, -20)"
]
]
} | {
"input": []
} | basic |
verina_advanced_56 | -----Description-----
This task requires writing a Lean 4 method that moves all zeroes in a given integer list to the end, while preserving the relative order of the non-zero elements.
The method `moveZeroes` processes the input list by separating the non-zero and zero elements. It then returns a new list formed by appending all non-zero elements followed by all the zero elements.
-----Input-----
The input is a single list of integers:
xs: A list of integers (type: List Int), possibly containing zero and non-zero values.
-----Output-----
The output is a list of integers:
Returns a list (type: List Int) with the same elements as the input, where all zeroes appear at the end, and the non-zero elements maintain their original relative order.
| -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible]
def moveZeroes_precond (xs : List Int) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- Count how many times a specific value appears in the list
-- !benchmark @end code_aux
def moveZeroes (xs : List Int) (h_precond : moveZeroes_precond (xs)) : List Int :=
-- !benchmark @start code
let nonzeros := xs.filter (fun x => x β 0)
let zeros := xs.filter (fun x => x = 0)
nonzeros ++ zeros
-- !benchmark @end code
-- !benchmark @start postcond_aux
def countVal (val : Int) : List Int β Nat
| [] => 0
| x :: xs =>
let rest := countVal val xs
if x = val then rest + 1 else rest
-- Check whether one list is a subsequence of another (preserving relative order)
def isSubsequence (xs ys : List Int) : Bool :=
match xs, ys with
| [], _ => true
| _ :: _, [] => false
| x :: xt, y :: yt =>
if x = y then isSubsequence xt yt else isSubsequence xs yt
-- !benchmark @end postcond_aux
@[reducible]
def moveZeroes_postcond (xs : List Int) (result: List Int) (h_precond : moveZeroes_precond (xs)) : Prop :=
-- !benchmark @start postcond
-- 1. All non-zero elements must maintain their relative order
isSubsequence (xs.filter (fun x => x β 0)) result = true β§
-- 2. All zeroes must be located at the end of the output list
(result.dropWhile (fun x => x β 0)).all (fun x => x = 0) β§
-- 3. The output must contain the same number of elements,
-- and the number of zeroes must remain unchanged
countVal 0 xs = countVal 0 result β§
xs.length = result.length
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem moveZeroes_spec_satisfied (xs: List Int) (h_precond : moveZeroes_precond (xs)) :
moveZeroes_postcond (xs) (moveZeroes (xs) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "moveZeroes",
"parameters": {
"param_name": [
"xs"
],
"param_type": [
"List Int"
]
},
"return_type": "List Int"
} | {
"upstream": {
"name": "lab_assignment",
"link": "https://leetcode.com/problems/move-zeroes/description/",
"task_id": "lab_moveZeroes_324883943",
"student_id": [
41
]
}
} | {
"input": [
"{\"xs\": \"[0, 1, 0, 3, 12]\"}",
"{\"xs\": \"[0, 0, 1]\"}",
"{\"xs\": \"[1, 2, 3]\"}",
"{\"xs\": \"[0, 0, 0]\"}",
"{\"xs\": \"[]\"}",
"{\"xs\": \"[4, 0, 5, 0, 6]\"}",
"{\"xs\": \"[0, 1]\"}",
"{\"xs\": \"[1, 0]\"}",
"{\"xs\": \"[2, 0, 0, 3]\"}"
],
"expected": [
[
"[1, 3, 12, 0, 0]"
],
[
"[1, 0, 0]"
],
[
"[1, 2, 3]"
],
[
"[0, 0, 0]"
],
[
"[]"
],
[
"[4, 5, 6, 0, 0]"
],
[
"[1, 0]"
],
[
"[1, 0]"
],
[
"[2, 3, 0, 0]"
]
],
"unexpected": [
[
"[0, 1, 3, 12, 0]"
],
[
"[0, 1, 0]"
],
[
"[1, 3, 2]",
"[0, 1, 2, 3]"
],
[
"[0, 0]",
"[]",
"[0]"
],
[
"[0]"
],
[
"[0, 4, 5, 6, 0]"
],
[
"[0, 1]"
],
[
"[0, 1]"
],
[
"[0, 0, 2, 3]",
"[2, 0, 3, 0]"
]
]
} | {
"input": []
} | advanced |
verina_basic_60 | -----Description-----
This task requires writing a function that processes an array of integers and produces a new array containing only the even numbers from the input. The order of these even numbers should remain the same as in the original array, ensuring that every even number from the input appears in the output and that every element in the output is even.
-----Input-----
The input consists of one parameter:
β’ arr: An array of integers.
-----Output-----
The output is an array of integers that:
β’ Contains exactly all even numbers from the input array, preserving their original order.
β’ Meets the specified conditions that ensure no extraneous (odd or non-existing) elements are returned.
-----Note-----
There are no additional preconditions. The function must adhere to the provided specification which enforces evenness and order preservation for the elements in the output array. | -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
def isEven (n : Int) : Bool :=
n % 2 = 0
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def FindEvenNumbers_precond (arr : Array Int) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def FindEvenNumbers (arr : Array Int) (h_precond : FindEvenNumbers_precond (arr)) : Array Int :=
-- !benchmark @start code
let rec loop (i : Nat) (acc : Array Int) : Array Int :=
if i < arr.size then
if isEven (arr.getD i 0) then
loop (i + 1) (acc.push (arr.getD i 0))
else
loop (i + 1) acc
else
acc
loop 0 (Array.mkEmpty 0)
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def FindEvenNumbers_postcond (arr : Array Int) (result: Array Int) (h_precond : FindEvenNumbers_precond (arr)) :=
-- !benchmark @start postcond
result.all (fun x => isEven x && x β arr) β§
List.Pairwise (fun (x, i) (y, j) => if i < j then arr.idxOf x β€ arr.idxOf y else true) (result.toList.zipIdx)
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem FindEvenNumbers_spec_satisfied (arr: Array Int) (h_precond : FindEvenNumbers_precond (arr)) :
FindEvenNumbers_postcond (arr) (FindEvenNumbers (arr) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "FindEvenNumbers",
"parameters": {
"param_name": [
"arr"
],
"param_type": [
"Array Int"
]
},
"return_type": "Array Int"
} | {
"upstream": {
"name": "clover",
"link": "https://github.com/ChuyueSun/Clover",
"task_id": "Clover_even_list",
"student_id": null
}
} | {
"input": [
"{\"arr\": \"#[1, 2, 3, 4, 5, 6]\"}",
"{\"arr\": \"#[0, -2, 3, -4, 7]\"}",
"{\"arr\": \"#[1, 3, 5, 7]\"}",
"{\"arr\": \"#[2, 4, 8, 10]\"}",
"{\"arr\": \"#[]\"}"
],
"expected": [
[
"#[2, 4, 6]"
],
[
"#[0, -2, -4]"
],
[
"#[]"
],
[
"#[2, 4, 8, 10]"
],
[
"#[]"
]
],
"unexpected": [
[
"#[2, 4, 5]",
"#[1, 2, 3, 4]",
"#[2, 3, 6]"
],
[
"#[0, 3, -4]",
"#[0, -2, 3]"
],
[
"#[1]",
"#[0, 1]"
],
[
"#[2, 4, 8, 9]",
"#[2, 4, 8, 10, 12]"
],
[
"#[0]",
"#[1, 2]"
]
]
} | {
"input": []
} | basic |
verina_advanced_38 | -----Description-----
This task requires implementing a Lean 4 method that, given a list of intervals, returns the maximum amount that can be spanned after we removed one of the intervals
You may assume you'll receive at least one interval
-----Input-----
The input consists of a list of ordered pairs of intervals.
-----Output-----
The output is an integer:
Return the largest span that is possible after removing one of the intervals.
| -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def maxCoverageAfterRemovingOne_precond (intervals : List (Prod Nat Nat)) : Prop :=
-- !benchmark @start precond
intervals.length > 0
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def maxCoverageAfterRemovingOne (intervals : List (Prod Nat Nat)) (h_precond : maxCoverageAfterRemovingOne_precond (intervals)) : Nat :=
-- !benchmark @start code
let n := intervals.length
if n β€ 1 then 0
else
(List.range n).foldl (fun acc i =>
let remaining := List.eraseIdx intervals i
let sorted := List.mergeSort remaining (fun (a b : Nat Γ Nat) => a.1 β€ b.1)
let merged := sorted.foldl (fun acc curr =>
match acc with
| [] => [curr]
| (s, e) :: rest => if curr.1 β€ e then (s, max e curr.2) :: rest else curr :: acc
) []
let coverage := merged.reverse.foldl (fun acc (s, e) => acc + (e - s)) 0
max acc coverage
) 0
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def maxCoverageAfterRemovingOne_postcond (intervals : List (Prod Nat Nat)) (result: Nat) (h_precond : maxCoverageAfterRemovingOne_precond (intervals)) : Prop :=
-- !benchmark @start postcond
β i < intervals.length,
let remaining := List.eraseIdx intervals i
let sorted := List.mergeSort remaining (fun (a b : Nat Γ Nat) => a.1 β€ b.1)
let merged := sorted.foldl (fun acc curr =>
match acc with
| [] => [curr]
| (s, e) :: rest => if curr.1 β€ e then (s, max e curr.2) :: rest else curr :: acc
) []
let cov := merged.reverse.foldl (fun acc (s, e) => acc + (e - s)) 0
result = cov β§
β j < intervals.length,
let rem_j := List.eraseIdx intervals j
let sort_j := List.mergeSort rem_j (fun (a b : Nat Γ Nat) => a.1 β€ b.1)
let merged_j := sort_j.foldl (fun acc curr =>
match acc with
| [] => [curr]
| (s, e) :: rest => if curr.1 β€ e then (s, max e curr.2) :: rest else curr :: acc
) []
let cov_j := merged_j.reverse.foldl (fun acc (s, e) => acc + (e - s)) 0
cov β₯ cov_j
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem maxCoverageAfterRemovingOne_spec_satisfied (intervals: List (Prod Nat Nat)) (h_precond : maxCoverageAfterRemovingOne_precond (intervals)) :
maxCoverageAfterRemovingOne_postcond (intervals) (maxCoverageAfterRemovingOne (intervals) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "maxCoverageAfterRemovingOne",
"parameters": {
"param_name": [
"intervals"
],
"param_type": [
"List (Prod Nat Nat)"
]
},
"return_type": "Nat"
} | {
"upstream": {
"name": "lab_assignment",
"link": "[{\"text_file_id\"=>804563720}]",
"task_id": "lab_maxCoverageAfterRemovingOne_325587004",
"student_id": [
31
]
}
} | {
"input": [
"{\"intervals\": \"[(1, 3), (2, 5), (6, 8)]\"}",
"{\"intervals\": \"[(1, 4), (2, 6), (8, 10), (9, 12)]\"}",
"{\"intervals\": \"[(1, 2), (2, 3), (3, 4)]\"}",
"{\"intervals\": \"[(1, 10), (2, 3), (4, 5)]\"}",
"{\"intervals\": \"[(5, 6), (1, 2), (3, 4)]\"}"
],
"expected": [
[
"5"
],
[
"8"
],
[
"2"
],
[
"9"
],
[
"2"
]
],
"unexpected": [
[
"4",
"6"
],
[
"7",
"6"
],
[
"3"
],
[
"7",
"10"
],
[
"5",
"3"
]
]
} | {
"input": [
"{'intervals': '[]'}"
]
} | advanced |
verina_basic_68 | -----Description-----
The task is to determine the position of a target integer in a given array. The goal is to return the index corresponding to the first occurrence of the target value. If the target is not present in the array, the result should indicate that by returning the size of the array. This description focuses entirely on understanding the problem without specifying any particular implementation method.
-----Input-----
The input consists of:
β’ a: An array of integers.
β’ e: An integer representing the target to search for in the array.
-----Output-----
The output is a natural number (Nat) which is:
β’ The index of the first occurrence of the target integer if found.
β’ The size of the array if the target integer is not present.
-----Note-----
There are no strict preconditions on the input; the method should work correctly for any array of integers. The specification ensures that the returned index is always valid: it is either within the array bounds with a matching element or equals the arrayβs size if the element is absent. | -- !benchmark @start import type=solution
import Mathlib
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def LinearSearch_precond (a : Array Int) (e : Int) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def LinearSearch (a : Array Int) (e : Int) (h_precond : LinearSearch_precond (a) (e)) : Nat :=
-- !benchmark @start code
let rec loop (n : Nat) : Nat :=
if n < a.size then
if a[n]! = e then n
else loop (n + 1)
else n
loop 0
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def LinearSearch_postcond (a : Array Int) (e : Int) (result: Nat) (h_precond : LinearSearch_precond (a) (e)) :=
-- !benchmark @start postcond
result β€ a.size β§ (result = a.size β¨ a[result]! = e) β§ (β i, i < result β a[i]! β e)
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem LinearSearch_spec_satisfied (a: Array Int) (e: Int) (h_precond : LinearSearch_precond (a) (e)) :
LinearSearch_postcond (a) (e) (LinearSearch (a) (e) h_precond) h_precond := by
-- !benchmark @start proof
unfold LinearSearch_postcond LinearSearch
apply And.intro
. let aux (x : Nat) : (0 β€ x) β (x β€ a.size) β LinearSearch.loop a e x β€ a.size := by
intro hxβ hxβ
let nx := a.size - x
have hnβ : nx = a.size - x := by rfl
have hnβ : x = a.size - nx := by
rw [hnβ, Nat.sub_sub_self]
apply hxβ
rw [hnβ]
induction nx with
| zero =>
unfold LinearSearch.loop
simp
| succ nβ ih =>
by_cases hp : (a.size β€ nβ)
. rw [Nat.sub_eq_zero_of_le hp] at ih
have h_tmp : a.size β€ nβ + 1 := Nat.le_add_right_of_le hp
rw [Nat.sub_eq_zero_of_le h_tmp]
exact ih
. have hq : nβ < a.size := Nat.not_le.mp hp
unfold LinearSearch.loop
simp
split_ifs
. simp
. rw [Nat.sub_add_eq, Nat.sub_add_cancel]
exact ih
apply Nat.zero_lt_sub_of_lt
exact hq
simp
have hβ : 0 β€ a.size := by simp
have h_triv : 0 β€ 0 := by simp
exact aux 0 h_triv hβ
. apply And.intro
. let aux (x : Nat) : (x β₯ 0) β (x β€ a.size) β LinearSearch.loop a e x = a.size β¨ a[LinearSearch.loop a e x]! = e := by
intro hxβ hxβ
let nx := a.size - x
have hnβ : nx = a.size - x := by rfl
have hnβ : x = a.size - nx := by
rw [hnβ, Nat.sub_sub_self]
apply hxβ
rw [hnβ]
induction nx with
| zero =>
unfold LinearSearch.loop
simp
| succ nβ ih =>
-- Ohh boy...
by_cases hp : (a.size β€ nβ)
. rw [Nat.sub_eq_zero_of_le hp] at ih
have h_tmp : a.size β€ nβ + 1 := Nat.le_add_right_of_le hp
rw [Nat.sub_eq_zero_of_le h_tmp]
exact ih
. have hq : nβ < a.size := Nat.not_le.mp hp
apply Or.elim ih -- Didn't find elem, so we're gonna also return a.size...
. intro ihβ
unfold LinearSearch.loop
split_ifs
. rename_i hβ hβ
rw [hβ]
simp
. rename_i haβ haβ
rw [Nat.sub_add_eq, Nat.sub_add_cancel]
rw [ihβ]
simp
apply Nat.zero_lt_sub_of_lt
exact hq
. rename_i hβ
have haβ := Nat.not_lt.mp hβ
have haβ := Nat.sub_le a.size (nβ + 1)
have ha := Nat.eq_iff_le_and_ge.mpr β¨haβ, haββ©
rw [βha]
simp
. intro ihβ
unfold LinearSearch.loop
split_ifs
. rename_i hβ hβ
rw [hβ]
simp
. rename_i haβ haβ
rw [Nat.sub_add_eq, Nat.sub_add_cancel]
rw [ihβ]
simp
apply Nat.zero_lt_sub_of_lt
exact hq
. rename_i hβ
have haβ := Nat.not_lt.mp hβ
have haβ := Nat.sub_le a.size (nβ + 1)
have ha := Nat.eq_iff_le_and_ge.mpr β¨haβ, haββ©
rw [βha]
simp
have hβ : 0 β€ 0 := by simp
have hβ : 0 β€ a.size := by simp
exact aux 0 hβ hβ
. let aux (x : Nat) : (0 β€ x) β (x β€ a.size) β (β i, x β€ i β i < LinearSearch.loop a e x β a[i]! β e) := by
intro hxβ hxβ i
let nx := a.size - x
have hnβ : nx = a.size - x := by rfl
have hnβ : x = a.size - nx := by
rw [hnβ, Nat.sub_sub_self]
apply hxβ
rw [hnβ]
induction nx with
| zero =>
-- There's no such i
unfold LinearSearch.loop
simp
intro hxi hi
have h_contr := Nat.lt_of_le_of_lt hxi hi
have h : a.size β€ a.size := by simp
have h : a.size - a.size < a.size - a.size := Nat.sub_lt_sub_right h h_contr
simp at h
| succ n ih =>
intro hxi
unfold LinearSearch.loop
simp
split_ifs
. rename_i hβ hβ
intro h_contr
have h := Nat.lt_of_le_of_lt hxi h_contr
simp at h
. rename_i hβ hβ
by_cases hp : (a.size β€ n)
. rw [Nat.sub_eq_zero_iff_le.mpr hp] at ih
intro hi
have hpβ : a.size β€ n + 1 := by
have hβ' : n β€ n + 1 := by simp
exact Nat.le_trans hp hβ'
rw [Nat.sub_eq_zero_iff_le.mpr hpβ] at hxi
rw [Nat.sub_eq_zero_iff_le.mpr hpβ] at hi
rw [Nat.sub_eq_zero_iff_le.mpr hpβ] at hβ
have ihβ := ih hxi
simp at hi
unfold LinearSearch.loop at ihβ
split_ifs at ihβ
. rename_i hβ'
simp at ihβ
exact ihβ hi
. rename_i hβ'
contradiction
. have hq : n < a.size := Nat.not_le.mp hp
have hq' : 1 β€ a.size - n := by
have h : 0 < a.size - n := by
exact Nat.sub_pos_of_lt hq
exact Nat.one_le_iff_ne_zero.mpr (Nat.ne_zero_of_lt h)
rw [Nat.sub_add_eq, Nat.sub_add_cancel hq']
intro hi
by_cases h_bounds : (a.size - n β€ i)
. exact ih h_bounds hi
. have h_bounds' : ( i + 1 < a.size - n + 1) := (@Nat.add_lt_add_iff_right 1 i (a.size - n)).mpr (Nat.not_le.mp h_bounds)
have h := Nat.le_of_lt_add_one h_bounds'
apply Nat.le_sub_of_add_le at h
rw [β Nat.sub_add_eq] at h
have hi_fixed := Nat.eq_iff_le_and_ge.mpr β¨hxi, hβ©
rw [hi_fixed] at hβ
exact hβ
. intro h_contr
have h := Nat.lt_of_le_of_lt hxi h_contr
simp at h
have hβ : 0 β€ a.size := by simp
have h_triv : 0 β€ 0 := by simp
intro i
have h_tmp : 0 β€ i := Nat.zero_le i
exact aux 0 h_triv hβ i h_tmp
-- !benchmark @end proof
| {
"name": "LinearSearch",
"parameters": {
"param_name": [
"a",
"e"
],
"param_type": [
"Array Int",
"Int"
]
},
"return_type": "Nat"
} | {
"upstream": {
"name": "clover",
"link": "https://github.com/ChuyueSun/Clover",
"task_id": "Clover_linear_search1",
"student_id": null
}
} | {
"input": [
"{\"a\": \"#[1, 3, 5, 7, 9]\", \"e\": 5}",
"{\"a\": \"#[2, 4, 6, 8]\", \"e\": 5}",
"{\"a\": \"#[5, 5, 5]\", \"e\": 5}",
"{\"a\": \"#[10, 9, 8, 7]\", \"e\": 10}",
"{\"a\": \"#[1, 2, 3, 3, 4]\", \"e\": 3}"
],
"expected": [
[
"2"
],
[
"4"
],
[
"0"
],
[
"0"
],
[
"2"
]
],
"unexpected": [
[
"1",
"3",
"4"
],
[
"1",
"3",
"5"
],
[
"1",
"2",
"3"
],
[
"1",
"2",
"3"
],
[
"1",
"3",
"4"
]
]
} | {
"input": []
} | basic |
verina_advanced_71 | -----Description-----
This task requires writing a Lean 4 method that, given a binary string `s` and an integer `k`, finds the shortest contiguous substring that contains exactly `k` characters `'1'`.
Among all substrings of `s` that contain exactly `k` occurrences of `'1'`, return the one that is shortest in length. If there are multiple such substrings with the same length, return the lexicographically smallest one.
If no such substring exists, return the empty string.
-----Input-----
- s: A binary string (only consisting of characters `'0'` and `'1'`)
- k: A natural number (k β₯ 0)
-----Output-----
A string representing the shortest substring of `s` that contains exactly `k` ones. If no such substring exists, return `""`.
| -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
def countOnes (lst : List Char) : Nat :=
lst.foldl (fun acc c => if c = '1' then acc + 1 else acc) 0
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible]
def shortestBeautifulSubstring_precond (s : String) (k : Nat) : Prop :=
-- !benchmark @start precond
s.toList.all (fun c => c = '0' β¨ c = '1')
-- !benchmark @end precond
-- !benchmark @start code_aux
def listToString (lst : List Char) : String :=
String.mk lst
def isLexSmaller (a b : List Char) : Bool :=
listToString a < listToString b
def allSubstrings (s : List Char) : List (List Char) :=
let n := s.length
(List.range n).flatMap (fun i =>
(List.range (n - i)).map (fun j =>
s.drop i |>.take (j + 1)))
-- !benchmark @end code_aux
def shortestBeautifulSubstring (s : String) (k : Nat) (h_precond : shortestBeautifulSubstring_precond (s) (k)) : String :=
-- !benchmark @start code
let chars := s.data
let candidates := allSubstrings chars |>.filter (fun sub => countOnes sub = k)
let compare (a b : List Char) : Bool :=
a.length < b.length β¨ (a.length = b.length β§ isLexSmaller a b)
let best := candidates.foldl (fun acc cur =>
match acc with
| none => some cur
| some best => if compare cur best then some cur else some best) none
match best with
| some b => listToString b
| none => ""
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible]
def shortestBeautifulSubstring_postcond (s : String) (k : Nat) (result: String) (h_precond : shortestBeautifulSubstring_precond (s) (k)) : Prop :=
-- !benchmark @start postcond
let chars := s.data
let substrings := (List.range chars.length).flatMap (fun i =>
(List.range (chars.length - i + 1)).map (fun len =>
chars.drop i |>.take len))
let isBeautiful := fun sub => countOnes sub = k
let beautiful := substrings.filter (fun sub => isBeautiful sub)
let targets := beautiful.map (Β·.asString) |>.filter (fun s => s β "")
(result = "" β§ targets = []) β¨
(result β targets β§
β r β targets, r.length β₯ result.length β¨ (r.length = result.length β§ result β€ r))
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem shortestBeautifulSubstring_spec_satisfied (s: String) (k: Nat) (h_precond : shortestBeautifulSubstring_precond (s) (k)) :
shortestBeautifulSubstring_postcond (s) (k) (shortestBeautifulSubstring (s) (k) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "shortestBeautifulSubstring",
"parameters": {
"param_name": [
"s",
"k"
],
"param_type": [
"String",
"Nat"
]
},
"return_type": "String"
} | {
"upstream": {
"name": "lab_assignment",
"link": "https://huggingface.co/spaces/livecodebench/code_generation_samples",
"task_id": "lab_shortestBeautifulSubstring_325098964",
"student_id": [
32
]
}
} | {
"input": [
"{\"s\": \"100011001\", \"k\": 3}",
"{\"s\": \"1011\", \"k\": 2}",
"{\"s\": \"000\", \"k\": 1}",
"{\"s\": \"11111\", \"k\": 3}",
"{\"s\": \"10100101\", \"k\": 2}",
"{\"s\": \"1001001\", \"k\": 2}",
"{\"s\": \"10010001\", \"k\": 1}",
"{\"s\": \"1001\", \"k\": 0}"
],
"expected": [
[
"11001"
],
[
"11"
],
[
""
],
[
"111"
],
[
"101"
],
[
"1001"
],
[
"1"
],
[
"0"
]
],
"unexpected": [
[
"00011",
"10001",
""
],
[
"101",
"01",
""
],
[
"0",
"00",
"000"
],
[
"11",
"1111",
""
],
[
"010",
"1001",
"0101"
],
[
"0010",
"0100",
"001"
],
[
"10",
"100",
"000"
],
[
"10",
"100",
"1"
]
]
} | {
"input": [
"{'s': '2', 'k': 1}"
]
} | advanced |
verina_advanced_59 | -----Description-----
This task requires writing a Lean 4 method that determines if a given string is a palindrome, ignoring all
non-alphanumeric characters and case differences. For example, the string "A man, a plan, a canal: Panama" should return
true.
-----Input-----
A single string:
s: The string to check for palindrome property.
-----Output-----
A boolean (Bool):
true if s is a palindrome when ignoring non-alphanumeric characters and case. false otherwise.
| -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible]
def palindromeIgnoreNonAlnum_precond (s : String) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def palindromeIgnoreNonAlnum (s : String) (h_precond : palindromeIgnoreNonAlnum_precond (s)) : Bool :=
-- !benchmark @start code
let cleaned : List Char :=
s.data.filter (fun c => c.isAlpha || c.isDigit)
|>.map Char.toLower
let n := cleaned.length
let startIndex := 0
let endIndex := if n = 0 then 0 else n - 1
let rec check (l r : Nat) : Bool :=
if l >= r then
true
else if cleaned[l]? = cleaned[r]? then
check (l + 1) (r - 1)
else
false
check startIndex endIndex
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible]
def palindromeIgnoreNonAlnum_postcond (s : String) (result: Bool) (h_precond : palindromeIgnoreNonAlnum_precond (s)) : Prop :=
-- !benchmark @start postcond
let cleaned := s.data.filter (fun c => c.isAlpha || c.isDigit) |>.map Char.toLower
let forward := cleaned
let backward := cleaned.reverse
if result then
forward = backward
else
forward β backward
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem palindromeIgnoreNonAlnum_spec_satisfied (s: String) (h_precond : palindromeIgnoreNonAlnum_precond (s)) :
palindromeIgnoreNonAlnum_postcond (s) (palindromeIgnoreNonAlnum (s) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "palindromeIgnoreNonAlnum",
"parameters": {
"param_name": [
"s"
],
"param_type": [
"String"
]
},
"return_type": "Bool"
} | {
"upstream": {
"name": "lab_assignment",
"link": "N/A",
"task_id": "lab_palindromeIgnoreNonAlnum_325057855",
"student_id": [
17
]
}
} | {
"input": [
"{\"s\": \"\"}",
"{\"s\": \"A man, a plan, a canal: Panama\"}",
"{\"s\": \"race a car\"}",
"{\"s\": \"No 'x' in Nixon\"}",
"{\"s\": \"abc!!cba?\"}",
"{\"s\": \"Hello, world!\"}"
],
"expected": [
[
"True"
],
[
"True"
],
[
"False"
],
[
"True"
],
[
"True"
],
[
"False"
]
],
"unexpected": [
[
"False"
],
[
"False"
],
[
"True"
],
[
"False"
],
[
"False"
],
[
"True"
]
]
} | {
"input": []
} | advanced |
verina_advanced_24 | -----Description-----
This task requires writing a Lean 4 method that determines the length of the longest strictly increasing subsequence in a given array of integers.
A subsequence is a sequence that can be derived from the array by deleting some or no elements without changing the order of the remaining elements. The subsequence must be strictly increasing, meaning each element must be greater than the one before it.
The goal is to find the length of the longest such subsequence that can be formed from the input array.
-----Input-----
The input consists of one array:
nums: An array of integers where nums[i] represents the ith element of the input sequence.
-----Output-----
The output is an integer:
Returns the length of the longest strictly increasing subsequence in the input array.
| -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def lengthOfLIS_precond (nums : List Int) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def lengthOfLIS (nums : List Int) (h_precond : lengthOfLIS_precond (nums)) : Int :=
-- !benchmark @start code
let rec lisHelper (dp : List Int) (x : Int) : List Int :=
let rec replace (l : List Int) (acc : List Int) : List Int :=
match l with
| [] => (acc.reverse ++ [x])
| y :: ys => if x β€ y then acc.reverse ++ (x :: ys) else replace ys (y :: acc)
replace dp []
let finalDP := nums.foldl lisHelper []
finalDP.length
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def lengthOfLIS_postcond (nums : List Int) (result: Int) (h_precond : lengthOfLIS_precond (nums)) : Prop :=
-- !benchmark @start postcond
-- Helper function to check strictly increasing
let rec isStrictlyIncreasing (l : List Int) : Bool :=
match l with
| [] | [_] => true
| x :: y :: rest => x < y && isStrictlyIncreasing (y :: rest)
-- Generate all subsequences
let rec subsequences (xs : List Int) : List (List Int) :=
match xs with
| [] => [[]]
| x :: xs' =>
let rest := subsequences xs'
rest ++ rest.map (fun r => x :: r)
let allIncreasing := subsequences nums |>.filter (fun l => isStrictlyIncreasing l)
allIncreasing.any (fun l => l.length = result) β§
allIncreasing.all (fun l => l.length β€ result)
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem lengthOfLIS_spec_satisfied (nums: List Int) (h_precond : lengthOfLIS_precond (nums)) :
lengthOfLIS_postcond (nums) (lengthOfLIS (nums) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "lengthOfLIS",
"parameters": {
"param_name": [
"nums"
],
"param_type": [
"List Int"
]
},
"return_type": "Int"
} | {
"upstream": {
"name": "lab_assignment",
"link": "[{\"text_file_id\"=>804740897}]",
"task_id": "lab_lengthOfLIS_325627981",
"student_id": [
6
]
}
} | {
"input": [
"{\"nums\": \"[10, 9, 2, 5, 3, 7, 101, 18]\"}",
"{\"nums\": \"[0, 1, 0, 3, 2, 3]\"}",
"{\"nums\": \"[7, 7, 7, 7, 7, 7, 7]\"}",
"{\"nums\": \"[4, 10, 4, 3, 8, 9]\"}",
"{\"nums\": \"[1, 3, 6, 7, 9, 4, 10, 5, 6]\"}"
],
"expected": [
[
"4"
],
[
"4"
],
[
"1"
],
[
"3"
],
[
"6"
]
],
"unexpected": [
[
"1",
"2",
"8"
],
[
"1",
"3",
"6"
],
[
"0",
"6",
"7"
],
[
"1",
"2",
"6"
],
[
"1",
"4",
"9"
]
]
} | {
"input": []
} | advanced |
verina_advanced_69 | -----Description-----
Given a sorted list of distinct integers and a target value, return the index if the target is found. If it is not found, return the index where it would be inserted to maintain the sorted order.
This function must preserve the sorted property of the list. The list is assumed to be strictly increasing and contain no duplicates.
-----Input-----
xs : List Int β a sorted list of distinct integers in increasing order
target : Int β the integer to search for
-----Output-----
A natural number (Nat) representing the index at which the target is found, or the index at which it should be inserted to maintain sorted order.
| -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible]
def searchInsert_precond (xs : List Int) (target : Int) : Prop :=
-- !benchmark @start precond
List.Pairwise (Β· < Β·) xs
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def searchInsert (xs : List Int) (target : Int) (h_precond : searchInsert_precond (xs) (target)) : Nat :=
-- !benchmark @start code
match xs with
| [] =>
0
| _ :: _ =>
let rec helper : List Int β Nat β Nat :=
fun ys idx =>
match ys with
| [] =>
idx
| y :: ys' =>
let isCurrent := y
let currentIndex := idx
let targetValue := target
let condition := targetValue β€ isCurrent
if condition then
currentIndex
else
let incrementedIndex := currentIndex + 1
let rest := ys'
helper rest incrementedIndex
let startingIndex := 0
let result := helper xs startingIndex
result
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible]
def searchInsert_postcond (xs : List Int) (target : Int) (result: Nat) (h_precond : searchInsert_precond (xs) (target)) : Prop :=
-- !benchmark @start postcond
let allBeforeLess := (List.range result).all (fun i => xs[i]! < target)
let inBounds := result β€ xs.length
let insertedCorrectly :=
result < xs.length β target β€ xs[result]!
inBounds β§ allBeforeLess β§ insertedCorrectly
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem searchInsert_spec_satisfied (xs: List Int) (target: Int) (h_precond : searchInsert_precond (xs) (target)) :
searchInsert_postcond (xs) (target) (searchInsert (xs) (target) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "searchInsert",
"parameters": {
"param_name": [
"xs",
"target"
],
"param_type": [
"List Int",
"Int"
]
},
"return_type": "Nat"
} | {
"upstream": {
"name": "lab_assignment",
"link": "https://leetcode.com/problems/search-insert-position/",
"task_id": "lab_searchInsert_325772357",
"student_id": [
50
]
}
} | {
"input": [
"{\"xs\": \"[1, 3, 5, 6]\", \"target\": 5}",
"{\"xs\": \"[1, 3, 5, 6]\", \"target\": 2}",
"{\"xs\": \"[1, 3, 5, 6]\", \"target\": 7}",
"{\"xs\": \"[1, 3, 5, 6]\", \"target\": 0}",
"{\"xs\": \"[]\", \"target\": 3}",
"{\"xs\": \"[10]\", \"target\": 5}",
"{\"xs\": \"[10]\", \"target\": 15}"
],
"expected": [
[
"2"
],
[
"1"
],
[
"4"
],
[
"0"
],
[
"0"
],
[
"0"
],
[
"1"
]
],
"unexpected": [
[
"0",
"1",
"3",
"4"
],
[
"0",
"2",
"3"
],
[
"2",
"3"
],
[
"1",
"2"
],
[
"1"
],
[
"1"
],
[
"0"
]
]
} | {
"input": [
"{'xs': '[2, 1]', 'target': 5}",
"{'xs': '[1, 1]', 'target': 2}"
]
} | advanced |
verina_advanced_2 | -----Description-----
This task requires writing a Lean 4 method that finds the length of the logest common subsequence of two input arrays.
-----Input-----
The input consists of two arrays:
a: The first array.
b: The second array.
-----Output-----
The output is an integer:
Returns the length of array a and b's longest common subsequence. | -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def LongestCommonSubsequence_precond (a : Array Int) (b : Array Int) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
def intMax (x y : Int) : Int :=
if x < y then y else x
-- !benchmark @end code_aux
def LongestCommonSubsequence (a : Array Int) (b : Array Int) (h_precond : LongestCommonSubsequence_precond (a) (b)) : Int :=
-- !benchmark @start code
let m := a.size
let n := b.size
let dp := Id.run do
let mut dp := Array.mkArray (m + 1) (Array.mkArray (n + 1) 0)
for i in List.range (m + 1) do
for j in List.range (n + 1) do
if i = 0 β¨ j = 0 then
()
else if a[i - 1]! = b[j - 1]! then
let newVal := ((dp[i - 1]!)[j - 1]!) + 1
dp := dp.set! i (dp[i]!.set! j newVal)
else
let newVal := intMax ((dp[i - 1]!)[j]!) ((dp[i]!)[j - 1]!)
dp := dp.set! i (dp[i]!.set! j newVal)
return dp
(dp[m]!)[n]!
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def LongestCommonSubsequence_postcond (a : Array Int) (b : Array Int) (result: Int) (h_precond : LongestCommonSubsequence_precond (a) (b)) : Prop :=
-- !benchmark @start postcond
let allSubseq (arr : Array Int) := (arr.foldl fun acc x => acc ++ acc.map (fun sub => x :: sub)) [[]] |>.map List.reverse
let subseqA := allSubseq a
let subseqB := allSubseq b
let commonSubseqLens := subseqA.filter (fun l => subseqB.contains l) |>.map (Β·.length)
commonSubseqLens.contains result β§ commonSubseqLens.all (Β· β€ result)
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem LongestCommonSubsequence_spec_satisfied (a: Array Int) (b: Array Int) (h_precond : LongestCommonSubsequence_precond (a) (b)) :
LongestCommonSubsequence_postcond (a) (b) (LongestCommonSubsequence (a) (b) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "LongestCommonSubsequence",
"parameters": {
"param_name": [
"a",
"b"
],
"param_type": [
"Array Int",
"Array Int"
]
},
"return_type": "Int"
} | {
"upstream": {
"name": "lab_assignment",
"link": "",
"task_id": "lab_LongestCommonSubsequence_324999618",
"student_id": [
2
]
}
} | {
"input": [
"{\"a\": \"#[1, 2, 3]\", \"b\": \"#[1, 2, 3]\"}",
"{\"a\": \"#[1, 3, 5, 7]\", \"b\": \"#[1, 2, 3, 4, 5, 6, 7]\"}",
"{\"a\": \"#[1, 2, 3]\", \"b\": \"#[4, 5, 6]\"}",
"{\"a\": \"#[]\", \"b\": \"#[1, 2, 3]\"}",
"{\"a\": \"#[1, 2, 3, 4]\", \"b\": \"#[2, 4, 6, 8]\"}"
],
"expected": [
[
"3"
],
[
"4"
],
[
"0"
],
[
"0"
],
[
"2"
]
],
"unexpected": [
[
"2",
"4"
],
[
"2",
"5"
],
[
"1",
"2"
],
[
"1"
],
[
"1",
"3",
"4"
]
]
} | {
"input": []
} | advanced |
verina_advanced_52 | -----Description-----
This task requires writing a Lean 4 function that finds the minimum number of operations to collect the integers from 1 to k by performing the following removal operation on a list of integers.
A removal operation consists of removing the last element from the list nums and adding it to your collection.
The goal is to determine how many elements must be removed from the end of the list until the set of collected elements (that are less than or equal to k) contains all integers from 1 to k, inclusive.
-----Input-----
The input consists of a list and a positive integer:
nums: A list of positive integers.
k: A positive integer representing the target upper bound for the collection (i.e., we want to collect 1, 2, ..., k).
-----Output-----
The output is an integer:
Return the minimum number of operations (elements removed from the end of nums) required to have collected all integers from 1 to k.
-----Note-----
It is assumed that the input list contains all integers from 1 to k.
| -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def minOperations_precond (nums : List Nat) (k : Nat) : Prop :=
-- !benchmark @start precond
let target_nums := (List.range k).map (Β· + 1)
target_nums.all (fun n => List.elem n nums)
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def minOperations (nums : List Nat) (k : Nat) (h_precond : minOperations_precond (nums) (k)) : Nat :=
-- !benchmark @start code
-- edge case k=0, requires 0 operations
if k == 0 then 0 else
-- recursive helper function
let rec loop (remaining : List Nat) (collected : List Nat) (collected_count : Nat) (ops : Nat) : Nat :=
match remaining with
| [] => ops -- base case
| head :: tail =>
let ops' := ops + 1
-- check if the element is relevant (1 <= head <= k) and not already collected
-- use a list `collected` to keep track of unique numbers found so far
if head > 0 && head <= k && !(List.elem head collected) then
let collected' := head :: collected -- add new unique element to our tracking list
let collected_count' := collected_count + 1
if collected_count' == k then
ops' -- found all k distinct required numbers
else
loop tail collected' collected_count' ops' -- continue searching, count increased
else
-- element is irrelevant (> k), zero/negative, or a duplicate (already in `collected`)
loop tail collected collected_count ops' -- continue searching, count not increased
-- start the loop, initially empty collection, 0 count, 0 operations
loop nums.reverse [] 0 0
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def minOperations_postcond (nums : List Nat) (k : Nat) (result: Nat) (h_precond : minOperations_precond (nums) (k)) : Prop :=
-- !benchmark @start postcond
-- define the list of elements processed after `result` operations
let processed := (nums.reverse).take result
-- define the target numbers to collect (1 to k)
let target_nums := (List.range k).map (Β· + 1)
-- condition 1: All target numbers must be present in the processed elements
let collected_all := target_nums.all (fun n => List.elem n processed)
-- condition 2: `result` must be the minimum number of operations.
-- This means either result is 0 (which implies k must be 0 as target_nums would be empty)
-- or result > 0, and taking one less operation (result - 1) is not sufficient
let is_minimal :=
if result > 0 then
-- if one fewer element is taken, not all target numbers should be present
let processed_minus_one := (nums.reverse).take (result - 1)
Β¬ (target_nums.all (fun n => List.elem n processed_minus_one))
else
-- if result is 0, it can only be minimal if k is 0 (no targets required)
-- So if k=0, `collected_all` is true. If result=0, this condition `k==0` ensures minimality.
k == 0
-- overall specification:
collected_all β§ is_minimal
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem minOperations_spec_satisfied (nums: List Nat) (k: Nat) (h_precond : minOperations_precond (nums) (k)) :
minOperations_postcond (nums) (k) (minOperations (nums) (k) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "minOperations",
"parameters": {
"param_name": [
"nums",
"k"
],
"param_type": [
"List Nat",
"Nat"
]
},
"return_type": "Nat"
} | {
"upstream": {
"name": "lab_assignment",
"link": "",
"task_id": "lab_minOperations_325696322",
"student_id": [
40
]
}
} | {
"input": [
"{\"nums\": \"[3, 1, 5, 4, 2]\", \"k\": 2}",
"{\"nums\": \"[3, 1, 5, 4, 2]\", \"k\": 5}",
"{\"nums\": \"[3, 2, 5, 3, 1]\", \"k\": 3}",
"{\"nums\": \"[5, 4, 3, 2, 1]\", \"k\": 1}",
"{\"nums\": \"[5, 4, 1, 2, 3]\", \"k\": 3}",
"{\"nums\": \"[1, 3, 2, 2, 1]\", \"k\": 2}",
"{\"nums\": \"[10, 1, 20, 2]\", \"k\": 2}",
"{\"nums\": \"[1, 2, 3]\", \"k\": 0}"
],
"expected": [
[
"4"
],
[
"5"
],
[
"4"
],
[
"1"
],
[
"3"
],
[
"2"
],
[
"3"
],
[
"0"
]
],
"unexpected": [
[
"1",
"2",
"5"
],
[
"1",
"2",
"3"
],
[
"1",
"2",
"5"
],
[
"0",
"2",
"5"
],
[
"1",
"4",
"5"
],
[
"1",
"3",
"4"
],
[
"1",
"2",
"4"
],
[
"1",
"2",
"3"
]
]
} | {
"input": [
"{'nums': '[5, 6, 7, 8, 9]', 'k': 3}"
]
} | advanced |
verina_advanced_75 | -----Description-----
Given a sequence of n integers, your task is to find the largest sum obtainable by choosing a contiguous subarray of the sequence. At least one number must be selected.
The algorithm uses dynamic programming (Kadaneβs Algorithm) to solve the problem:
1. Initialize the current maximum (cur) and the overall maximum (maxSoFar) with the first element.
2. For each subsequent element, update:
cur = max(element, cur + element)
maxSoFar = max(maxSoFar, cur)
3. Return maxSoFar as the answer.
-----Input-----
The input is provided as a list of integers:
sequence: A list of n integers.
-----Output-----
The output is a single integer representing the maximum subarray sum.
| -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def task_code_precond (sequence : List Int) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def task_code (sequence : List Int) (h_precond : task_code_precond (sequence)) : Int :=
-- !benchmark @start code
match sequence with
| [] => 0 -- If no elements are provided (should not happen according to the problem)
| x :: xs =>
let (_, maxSoFar) :=
xs.foldl (fun (acc : Int Γ Int) (x : Int) =>
let (cur, maxSoFar) := acc
let newCur := if cur + x >= x then cur + x else x
let newMax := if maxSoFar >= newCur then maxSoFar else newCur
(newCur, newMax)
) (x, x)
maxSoFar
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def task_code_postcond (sequence : List Int) (result: Int) (h_precond : task_code_precond (sequence)) : Prop :=
-- !benchmark @start postcond
let subArrays :=
List.range (sequence.length + 1) |>.flatMap (fun start =>
List.range (sequence.length - start + 1) |>.map (fun len =>
sequence.drop start |>.take len))
let subArraySums := subArrays.filter (Β· β []) |>.map (Β·.sum)
subArraySums.contains result β§ subArraySums.all (Β· β€ result)
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem task_code_spec_satisfied (sequence: List Int) (h_precond : task_code_precond (sequence)) :
task_code_postcond (sequence) (task_code (sequence) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "task_code",
"parameters": {
"param_name": [
"sequence"
],
"param_type": [
"List Int"
]
},
"return_type": "Int"
} | {
"upstream": {
"name": "lab_assignment",
"link": "",
"task_id": "lab_task_code_325773191",
"student_id": [
53
]
}
} | {
"input": [
"{\"sequence\": \"[10, -4, 3, 1, 5, 6, -35, 12, 21, -1]\"}",
"{\"sequence\": \"[2, 1, -4, 3, 4, -4, 6, 5, -5, 1]\"}",
"{\"sequence\": \"[-1, -2, -3, -4, -5]\"}",
"{\"sequence\": \"[7]\"}",
"{\"sequence\": \"[1, 2, 3, 4, 5]\"}"
],
"expected": [
[
"33"
],
[
"14"
],
[
"-1"
],
[
"7"
],
[
"15"
]
],
"unexpected": [
[
"32",
"34",
"0"
],
[
"13",
"15",
"0"
],
[
"-2",
"0",
"1"
],
[
"0",
"1",
"-7"
],
[
"14",
"16",
"0"
]
]
} | {
"input": []
} | advanced |
verina_basic_39 | -----Description-----
This task requires writing a Lean 4 method that rotates a list of integers to the right by a specified number of positions. The method should produce a new list where each element is shifted to the right while preserving the original list's length.
-----Input-----
The input consists of:
β’ l: A list of integers.
β’ n: A non-negative natural number that indicates the number of positions by which to rotate the list.
-----Output-----
The output is a list of integers:
β’ Returns a list with the same length as the input list, where the elements have been rotated to the right by n positions.
-----Note-----
β’ The precondition requires that n is non-negative.
β’ If the input list is empty, it should be returned unchanged. | -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def rotateRight_precond (l : List Int) (n : Nat) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def rotateRight (l : List Int) (n : Nat) (h_precond : rotateRight_precond (l) (n)) : List Int :=
-- !benchmark @start code
let len := l.length
if len = 0 then l
else
(List.range len).map (fun i : Nat =>
let idx_int : Int := ((Int.ofNat i - Int.ofNat n + Int.ofNat len) % Int.ofNat len)
let idx_nat : Nat := Int.toNat idx_int
l.getD idx_nat (l.headD 0)
)
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def rotateRight_postcond (l : List Int) (n : Nat) (result: List Int) (h_precond : rotateRight_precond (l) (n)) :=
-- !benchmark @start postcond
result.length = l.length β§
(β i : Nat, i < l.length β
let len := l.length
let rotated_index := Int.toNat ((Int.ofNat i - Int.ofNat n + Int.ofNat len) % Int.ofNat len)
result[i]? = l[rotated_index]?)
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem rotateRight_spec_satisfied (l: List Int) (n: Nat) (h_precond : rotateRight_precond (l) (n)) :
rotateRight_postcond (l) (n) (rotateRight (l) (n) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "rotateRight",
"parameters": {
"param_name": [
"l",
"n"
],
"param_type": [
"List Int",
"Nat"
]
},
"return_type": "List Int"
} | {
"upstream": {
"name": "dafny-synthesis",
"link": "https://github.com/Mondego/dafny-synthesis",
"task_id": "task_id_743",
"student_id": null
}
} | {
"input": [
"{\"l\": \"[1, 2, 3, 4, 5]\", \"n\": 2}",
"{\"l\": \"[1, 2, 3, 4, 5]\", \"n\": 7}",
"{\"l\": \"[1, 2, 3, 4, 5]\", \"n\": 0}",
"{\"l\": \"[]\", \"n\": 2}"
],
"expected": [
[
"4",
"5",
"1",
"2",
"3"
],
[
"4",
"5",
"1",
"2",
"3"
],
[
"1",
"2",
"3",
"4",
"5"
],
[]
],
"unexpected": [
[
"[5, 1, 2, 3, 4]",
"[3, 4, 5, 1, 2]"
],
[
"[5, 1, 2, 3, 4]",
"[3, 4, 5, 1, 2]"
],
[
"[5, 1, 2, 3, 4]",
"[4, 5, 1, 2, 3]"
],
[
"[0]",
"[42]"
]
]
} | {
"input": []
} | basic |
verina_basic_15 | -----Description-----
This task requires writing a Lean 4 method that determines whether an array of integers contains at least one pair of consecutive numbers. The method should return true if there is any index where an element, when increased by one, equals the next element in the array. If no such consecutive pair exists, the method should return false.
-----Input-----
The input consists of:
a: An array of integers (the array may be empty or non-empty).
-----Output-----
The output is a Boolean value:
Returns true if there is at least one index where an element plus one equals the following element.
Returns false if the array does not contain any consecutive numbers.
-----Note-----
There are no additional preconditions; the method will function correctly regardless of the array's size. | -- !benchmark @start import type=solution
import Mathlib
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def containsConsecutiveNumbers_precond (a : Array Int) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def containsConsecutiveNumbers (a : Array Int) (h_precond : containsConsecutiveNumbers_precond (a)) : Bool :=
-- !benchmark @start code
if a.size β€ 1 then
false
else
let withIndices := a.mapIdx (fun i x => (i, x))
withIndices.any (fun (i, x) =>
i < a.size - 1 && x + 1 == a[i+1]!)
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def containsConsecutiveNumbers_postcond (a : Array Int) (result: Bool) (h_precond : containsConsecutiveNumbers_precond (a)) :=
-- !benchmark @start postcond
(β i, i < a.size - 1 β§ a[i]! + 1 = a[i + 1]!) β result
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem containsConsecutiveNumbers_spec_satisfied (a: Array Int) (h_precond : containsConsecutiveNumbers_precond (a)) :
containsConsecutiveNumbers_postcond (a) (containsConsecutiveNumbers (a) h_precond) h_precond := by
-- !benchmark @start proof
unfold containsConsecutiveNumbers containsConsecutiveNumbers_postcond
constructor
Β· simp_all
intro i hi hconsec
have hi' : 1 + i < a.size := by
rw [Nat.add_comm]
exact Nat.add_lt_of_lt_sub hi
have hi'' : i < a.size := by
have : i < 1 + i := by
simp [Nat.lt_add_of_pos_left]
exact Nat.lt_trans this hi'
constructor
Β· exact Nat.lt_of_add_right_lt hi'
Β· apply Array.any_iff_exists.mpr
simp
exists i
simp [hi, hi'']
have : a[i]! = a[i] := by
exact getElem!_pos a i hi''
rw [βthis]
exact hconsec
Β· simp
intro ha h
have h' := Array.any_iff_exists.mp h
simp at h'
rcases h' with β¨i, hi, β¨hi', hconsecβ©β©
have : a[i]! = a[i] := by
exact getElem!_pos a i hi
exists i
rw [this]
simp_all
-- !benchmark @end proof
| {
"name": "containsConsecutiveNumbers",
"parameters": {
"param_name": [
"a"
],
"param_type": [
"Array Int"
]
},
"return_type": "Bool"
} | {
"upstream": {
"name": "dafny-synthesis",
"link": "https://github.com/Mondego/dafny-synthesis",
"task_id": "task_id_472",
"student_id": null
}
} | {
"input": [
"{\"a\": \"#[1, 2, 3, 5]\"}",
"{\"a\": \"#[1, 3, 5, 7]\"}",
"{\"a\": \"#[]\"}",
"{\"a\": \"#[10]\"}",
"{\"a\": \"#[5, 6]\"}",
"{\"a\": \"#[5, 7, 8, 10]\"}",
"{\"a\": \"#[9, 9, 10]\"}",
"{\"a\": \"#[3, 3, 3]\"}"
],
"expected": [
[
"True"
],
[
"False"
],
[
"False"
],
[
"False"
],
[
"True"
],
[
"True"
],
[
"True"
],
[
"False"
]
],
"unexpected": [
[
"False"
],
[
"True"
],
[
"True"
],
[
"True"
],
[
"False"
],
[
"False"
],
[
"False"
],
[
"True"
]
]
} | {
"input": []
} | basic |
verina_advanced_80 | -----Description-----
This task requires writing a Lean 4 method that finds the indices of two numbers in an array that add up to a target value. Given an array of integers and a target integer, the function should return the indices of the two numbers such that they add up to the target.
You may assume that each input has exactly one solution, and you may not use the same element twice.
-----Input-----
The input consists of:
nums: An array of integers.
target: An integer representing the target sum.
-----Output-----
The output is an array of two integers:
Returns the indices of the two numbers in the array that add up to the target. The indices should be sorted.
| -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible]
def twoSum_precond (nums : Array Int) (target : Int) : Prop :=
-- !benchmark @start precond
-- The array must have at least 2 elements
nums.size β₯ 2 β§
-- There exists exactly one pair of indices whose values sum to the target
(List.range nums.size).any (fun i =>
(List.range i).any (fun j => nums[i]! + nums[j]! = target)) β§
-- No other pair sums to the target (ensuring uniqueness of solution)
((List.range nums.size).flatMap (fun i =>
(List.range i).filter (fun j => nums[i]! + nums[j]! = target))).length = 1
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def twoSum (nums : Array Int) (target : Int) (h_precond : twoSum_precond (nums) (target)) : Array Nat :=
-- !benchmark @start code
let rec findIndices (i : Nat) (j : Nat) (fuel : Nat) : Array Nat :=
match fuel with
| 0 => #[] -- Fuel exhausted, return empty array
| fuel+1 =>
if i >= nums.size then
#[] -- No solution found
else if j >= nums.size then
findIndices (i + 1) (i + 2) fuel -- Move to next i and reset j
else
if nums[i]! + nums[j]! == target then
#[i, j] -- Found solution
else
findIndices i (j + 1) fuel -- Try next j
findIndices 0 1 (nums.size * nums.size)
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible]
def twoSum_postcond (nums : Array Int) (target : Int) (result: Array Nat) (h_precond : twoSum_precond (nums) (target)) : Prop :=
-- !benchmark @start postcond
-- Result contains exactly 2 indices
result.size = 2 β§
-- The indices are valid (within bounds of the nums array)
result[0]! < nums.size β§ result[1]! < nums.size β§
-- The indices are in ascending order (sorted)
result[0]! < result[1]! β§
-- The values at these indices sum to the target
nums[result[0]!]! + nums[result[1]!]! = target
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem twoSum_spec_satisfied (nums: Array Int) (target: Int) (h_precond : twoSum_precond (nums) (target)) :
twoSum_postcond (nums) (target) (twoSum (nums) (target) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "twoSum",
"parameters": {
"param_name": [
"nums",
"target"
],
"param_type": [
"Array Int",
"Int"
]
},
"return_type": "Array Nat"
} | {
"upstream": {
"name": "lab_assignment",
"link": "https://leetcode.com/problems/two-sum/description/",
"task_id": "lab_twoSum_325585735",
"student_id": [
31
]
}
} | {
"input": [
"{\"nums\": \"#[2, 7, 11, 15]\", \"target\": 9}",
"{\"nums\": \"#[3, 2, 4]\", \"target\": 6}",
"{\"nums\": \"#[3, 3]\", \"target\": 6}",
"{\"nums\": \"#[1, 2, 3, 4, 5]\", \"target\": 9}",
"{\"nums\": \"#[0, 4, 3, 0]\", \"target\": 0}"
],
"expected": [
[
"#[0, 1]"
],
[
"#[1, 2]"
],
[
"#[0, 1]"
],
[
"#[3, 4]"
],
[
"#[0, 3]"
]
],
"unexpected": [
[
"#[1, 0]",
"#[2, 3]",
"#[0, 3]"
],
[
"#[0, 1]",
"#[0, 2]",
"#[0, 3]"
],
[
"#[1, 0]",
"#[2, 2]"
],
[
"#[0, 4]",
"#[1, 3]",
"#[2, 2]"
],
[
"#[1, 2]",
"#[2, 1]"
]
]
} | {
"input": [
"{'nums': '#[0]', 'target': 2}"
]
} | advanced |
verina_advanced_77 | -----Description-----
This task requires writing a Lean 4 function that calculates how much water can be trapped between elevations after it rains. The input is a list of non-negative integers representing an elevation map. Each index traps water depending on the min of max heights to its left and right.
-----Input-----
- height: A list of natural numbers representing elevations.
-----Output-----
- A natural number: total units of water that can be trapped.
| -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def trapRainWater_precond (height : List Nat) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def trapRainWater (height : List Nat) (h_precond : trapRainWater_precond (height)) : Nat :=
-- !benchmark @start code
Id.run do
let mut left := 0
let mut right := height.length - 1
let mut leftMax := 0
let mut rightMax := 0
let mut water := 0
while left < right do
let hLeft := height[left]!
let hRight := height[right]!
if hLeft < hRight then
if hLeft >= leftMax then
leftMax := hLeft
else
water := water + (leftMax - hLeft)
left := left + 1
else
if hRight >= rightMax then
rightMax := hRight
else
water := water + (rightMax - hRight)
right := right - 1
return water
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def trapRainWater_postcond (height : List Nat) (result: Nat) (h_precond : trapRainWater_precond (height)) : Prop :=
-- !benchmark @start postcond
let waterAt := List.range height.length |>.map (fun i =>
let lmax := List.take (i+1) height |>.foldl Nat.max 0
let rmax := List.drop i height |>.foldl Nat.max 0
Nat.min lmax rmax - height[i]!)
result - (waterAt.foldl (Β· + Β·) 0) = 0 β§ (waterAt.foldl (Β· + Β·) 0) β€ result
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem trapRainWater_spec_satisfied (height: List Nat) (h_precond : trapRainWater_precond (height)) :
trapRainWater_postcond (height) (trapRainWater (height) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "trapRainWater",
"parameters": {
"param_name": [
"height"
],
"param_type": [
"List Nat"
]
},
"return_type": "Nat"
} | {
"upstream": {
"name": "lab_assignment",
"link": "",
"task_id": "lab_trapRainWater_325601349",
"student_id": [
22
]
}
} | {
"input": [
"{\"height\": \"[0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]\"}",
"{\"height\": \"[4, 2, 0, 3, 2, 5]\"}",
"{\"height\": \"[1, 0, 2]\"}",
"{\"height\": \"[3, 0, 1, 3, 0, 5]\"}",
"{\"height\": \"[0, 1, 2, 3, 4, 5]\"}",
"{\"height\": \"[]\"}"
],
"expected": [
[
"6"
],
[
"9"
],
[
"1"
],
[
"8"
],
[
"0"
],
[
"0"
]
],
"unexpected": [
[
"5",
"7"
],
[
"8"
],
[
"0",
"2"
],
[
"6"
],
[
"1"
],
[
"1"
]
]
} | {
"input": []
} | advanced |
verina_basic_30 | -----Description-----
This task requires writing a Lean 4 method that computes the element-wise modulo between two arrays of integers. The method should produce a new array where each element is the remainder after dividing the corresponding element from the first array by the element from the second array.
-----Input-----
The input consists of:
a: An array of integers.
b: An array of integers.
-----Output-----
The output is an array of integers:
Returns a new array in which each element is the result of taking the modulo of the corresponding elements from the two input arrays.
-----Note-----
Preconditions:
- Both arrays must be non-null.
- Both arrays must have the same length.
- All elements in the second array should be non-zero.
Postconditions:
- The length of the resulting array is the same as the length of the input arrays.
- Each element in the resulting array is the modulo of the corresponding elements in the input arrays. | -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def elementWiseModulo_precond (a : Array Int) (b : Array Int) : Prop :=
-- !benchmark @start precond
a.size = b.size β§ a.size > 0 β§
(β i, i < b.size β b[i]! β 0)
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def elementWiseModulo (a : Array Int) (b : Array Int) (h_precond : elementWiseModulo_precond (a) (b)) : Array Int :=
-- !benchmark @start code
a.mapIdx (fun i x => x % b[i]!)
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def elementWiseModulo_postcond (a : Array Int) (b : Array Int) (result: Array Int) (h_precond : elementWiseModulo_precond (a) (b)) :=
-- !benchmark @start postcond
result.size = a.size β§
(β i, i < result.size β result[i]! = a[i]! % b[i]!)
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem elementWiseModulo_spec_satisfied (a: Array Int) (b: Array Int) (h_precond : elementWiseModulo_precond (a) (b)) :
elementWiseModulo_postcond (a) (b) (elementWiseModulo (a) (b) h_precond) h_precond := by
-- !benchmark @start proof
unfold elementWiseModulo elementWiseModulo_postcond
unfold elementWiseModulo_precond at h_precond
simp_all
intro i hi
have h_maplen : (Array.mapIdx (fun i x => x % b[i]!) a).size = a.size := by
apply Array.size_mapIdx
have h1 : (Array.mapIdx (fun i x => x % b[i]!) a)[i] = (fun i x => x % b[i]!) i a[i] := by
apply Array.getElem_mapIdx
have h_eq : (Array.mapIdx (fun i x => x % b[i]!) a)[i] = (Array.mapIdx (fun i x => x % b[i]!) a)[i]! := by
have hi' : i < (Array.mapIdx (fun i x => x % b[i]!) a).size := by
simp only [h_precond, hi, h_maplen]
rw [Array.getElem!_eq_getD]
unfold Array.getD
simp [hi', hi, h_precond]
rw [β h_eq]
simp only [h1]
have h_eq' : a[i] = a[i]! := by
have hi_a : i < a.size := by
simp only [h_precond, hi]
simp_all [Array.getElem!_eq_getD]
simp only [h_eq']
-- !benchmark @end proof
| {
"name": "elementWiseModulo",
"parameters": {
"param_name": [
"a",
"b"
],
"param_type": [
"Array Int",
"Array Int"
]
},
"return_type": "Array Int"
} | {
"upstream": {
"name": "dafny-synthesis",
"link": "https://github.com/Mondego/dafny-synthesis",
"task_id": "task_id_616",
"student_id": null
}
} | {
"input": [
"{\"a\": \"#[10, 20, 30]\", \"b\": \"#[3, 7, 5]\"}",
"{\"a\": \"#[100, 200, 300, 400]\", \"b\": \"#[10, 20, 30, 50]\"}",
"{\"a\": \"#[-10, -20, 30]\", \"b\": \"#[3, -7, 5]\"}"
],
"expected": [
[
"#[1, 6, 0]"
],
[
"#[0, 0, 0, 0]"
],
[
"#[2, 1, 0]"
]
],
"unexpected": [
[
"#[1, 0, 0]",
"#[0, 6, 0]"
],
[
"#[0, 0, 0, 1]",
"#[1, 0, 0, 0]"
],
[
"#[-1, -5, 0]",
"#[-1, -6, 1]",
"#[0, -6, 0]"
]
]
} | {
"input": [
"{'a': '#[1]', 'b': '#[4, 0]'}"
]
} | basic |
verina_advanced_18 | -----Description-----
This task requires writing a Lean 4 method that determines whether a given number `n` is an Armstrong number (also known as a Narcissistic number). An Armstrong number is a number that is equal to the sum of its own digits raised to the power of the number of digits.
-----Input-----
The input consists of one natural number:
- `n: Nat`: The number to check if it satisfies the Armstrong property.
-----Output-----
The output is a boolean value:
- `Bool`: Return `true` if `n` is an Armstrong number, otherwise return `false`.
| -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
def countDigits (n : Nat) : Nat :=
let rec go (n acc : Nat) : Nat :=
if n = 0 then acc
else go (n / 10) (acc + 1)
go n (if n = 0 then 1 else 0)
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible]
def isArmstrong_precond (n : Nat) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
def sumPowers (n : Nat) (k : Nat) : Nat :=
let rec go (n acc : Nat) : Nat :=
if n = 0 then acc
else
let digit := n % 10
go (n / 10) (acc + digit ^ k)
go n 0
-- !benchmark @end code_aux
def isArmstrong (n : Nat) (h_precond : isArmstrong_precond (n)) : Bool :=
-- !benchmark @start code
let k := countDigits n
sumPowers n k = n
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible]
def isArmstrong_postcond (n : Nat) (result: Bool) (h_precond : isArmstrong_precond (n)) : Prop :=
-- !benchmark @start postcond
let n' := List.foldl (fun acc d => acc + d ^ countDigits n) 0 (List.map (fun c => c.toNat - '0'.toNat) (toString n).toList)
(result β (n = n')) β§
(Β¬ result β (n β n'))
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem isArmstrong_spec_satisfied (n: Nat) (h_precond : isArmstrong_precond (n)) :
isArmstrong_postcond (n) (isArmstrong (n) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "isArmstrong",
"parameters": {
"param_name": [
"n"
],
"param_type": [
"Nat"
]
},
"return_type": "Bool"
} | {
"upstream": {
"name": "lab_assignment",
"link": "leetcode 1134: https://leetcode.ca/all/1134.html",
"task_id": "lab_isArmstrong_325011347",
"student_id": [
7
]
}
} | {
"input": [
"{\"n\": 0}",
"{\"n\": 1}",
"{\"n\": 10}",
"{\"n\": 153}",
"{\"n\": 9474}",
"{\"n\": 9475}"
],
"expected": [
[
"True"
],
[
"True"
],
[
"False"
],
[
"True"
],
[
"True"
],
[
"False"
]
],
"unexpected": [
[
"False"
],
[
"False"
],
[
"True"
],
[
"False"
],
[
"False"
],
[
"True"
]
]
} | {
"input": []
} | advanced |
verina_advanced_21 | -----Description-----
Implement a Lean 4 function that checks if a given string is a palindrome. A string is considered a palindrome
if it reads the same forward and backward.
-----Input-----
The input consists of a single string:
s: A string
-----Output-----
The output is a boolean:
Returns true if s is a palindrome, false otherwise.
| -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def isPalindrome_precond (s : String) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def isPalindrome (s : String) (h_precond : isPalindrome_precond (s)) : Bool :=
-- !benchmark @start code
let length := s.length
if length <= 1 then
true
else
let arr := s.toList
let rec checkIndices (left : Nat) (right : Nat) (chars : List Char) : Bool :=
if left >= right then
true
else
match chars[left]?, chars[right]? with
| some cLeft, some cRight =>
if cLeft == cRight then
checkIndices (left + 1) (right - 1) chars
else
false
| _, _ => false
let approach1 := checkIndices 0 (length - 1) arr
let rec reverseList (acc : List Char) (xs : List Char) : List Char :=
match xs with
| [] => acc
| h :: t => reverseList (h :: acc) t
let reversed := reverseList [] arr
let approach2 := (arr == reversed)
approach1 && approach2
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def isPalindrome_postcond (s : String) (result: Bool) (h_precond : isPalindrome_precond (s)) : Prop :=
-- !benchmark @start postcond
(result β (s.toList == s.toList.reverse)) β§
(Β¬ result β (s.toList β [] β§ s.toList != s.toList.reverse))
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem isPalindrome_spec_satisfied (s: String) (h_precond : isPalindrome_precond (s)) :
isPalindrome_postcond (s) (isPalindrome (s) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "isPalindrome",
"parameters": {
"param_name": [
"s"
],
"param_type": [
"String"
]
},
"return_type": "Bool"
} | {
"upstream": {
"name": "lab_assignment",
"link": "",
"task_id": "lab_isPalindrome_325097530",
"student_id": [
17
]
}
} | {
"input": [
"{\"s\": \"racecar\"}",
"{\"s\": \"abba\"}",
"{\"s\": \"abc\"}",
"{\"s\": \"\"}",
"{\"s\": \"a\"}"
],
"expected": [
[
"True"
],
[
"True"
],
[
"False"
],
[
"True"
],
[
"True"
]
],
"unexpected": [
[
"False"
],
[
"False"
],
[
"True"
],
[
"False"
],
[
"False"
]
]
} | {
"input": []
} | advanced |
verina_basic_52 | -----Description-----
This task requires developing a solution that sorts an array of integers in non-decreasing order. The solution must return an array that is a rearrangement of the input, containing exactly the same elements but ordered from smallest to largest.
-----Input-----
The input consists of:
β’ a: An array of integers. This array can be empty or non-empty.
-----Output-----
The output is an array of integers that:
β’ Is sorted in non-decreasing order (i.e., for any indices i and j with i < j, a[i]! β€ a[j]!).
β’ Has the same size as the input array.
β’ Contains exactly the same elements as the input array, ensuring that the multiset of elements is preserved.
-----Note-----
The implementation uses helper functions for swapping elements and performing inner and outer loops of the bubble sort algorithm. No additional preconditions are required as the function should correctly handle empty and non-empty arrays. | -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def BubbleSort_precond (a : Array Int) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
def swap (a : Array Int) (i j : Nat) : Array Int :=
let temp := a[i]!
let aβ := a.set! i (a[j]!)
aβ.set! j temp
def bubbleInner (j i : Nat) (a : Array Int) : Array Int :=
if j < i then
let a' := if a[j]! > a[j+1]! then swap a j (j+1) else a
bubbleInner (j+1) i a'
else
a
def bubbleOuter (i : Nat) (a : Array Int) : Array Int :=
if i > 0 then
let a' := bubbleInner 0 i a
bubbleOuter (i - 1) a'
else
a
-- !benchmark @end code_aux
def BubbleSort (a : Array Int) (h_precond : BubbleSort_precond (a)) : Array Int :=
-- !benchmark @start code
if a.size = 0 then a else bubbleOuter (a.size - 1) a
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def BubbleSort_postcond (a : Array Int) (result: Array Int) (h_precond : BubbleSort_precond (a)) :=
-- !benchmark @start postcond
List.Pairwise (Β· β€ Β·) result.toList β§ List.isPerm result.toList a.toList
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem BubbleSort_spec_satisfied (a: Array Int) (h_precond : BubbleSort_precond (a)) :
BubbleSort_postcond (a) (BubbleSort (a) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "BubbleSort",
"parameters": {
"param_name": [
"a"
],
"param_type": [
"Array Int"
]
},
"return_type": "Array Int"
} | {
"upstream": {
"name": "clover",
"link": "https://github.com/ChuyueSun/Clover",
"task_id": "Clover_bubble_sort",
"student_id": null
}
} | {
"input": [
"{\"a\": \"#[5, 4, 3, 2, 1]\"}",
"{\"a\": \"#[1, 2, 3, 4, 5]\"}",
"{\"a\": \"#[3, 1, 2, 1, 5]\"}",
"{\"a\": \"#[10]\"}",
"{\"a\": \"#[4, 4, 4, 2, 2, 8]\"}"
],
"expected": [
[
"#[1, 2, 3, 4, 5]"
],
[
"#[1, 2, 3, 4, 5]"
],
[
"#[1, 1, 2, 3, 5]"
],
[
"#[10]"
],
[
"#[2, 2, 4, 4, 4, 8]"
]
],
"unexpected": [
[
"#[5, 4, 3, 2, 1]",
"#[2, 3, 1, 4, 5]"
],
[
"#[5, 4, 3, 2, 1]",
"#[1, 3, 2, 4, 5]"
],
[
"#[1, 2, 3, 1, 5]",
"#[3, 1, 2, 5, 1]"
],
[
"#[0]",
"#[10, 10]"
],
[
"#[2, 4, 4, 2, 4, 8]",
"#[4, 2, 4, 2, 4, 8]",
"#[2, 4, 2, 4, 4, 8]"
]
]
} | {
"input": []
} | basic |
verina_basic_71 | -----Description-----
This problem involves determining the longest common prefix shared by two lists of characters. Given two sequences, the goal is to identify and return the maximal contiguous sequence of characters from the beginning of both lists that are identical.
-----Input-----
The input consists of:
β’ str1: A list of characters.
β’ str2: A list of characters.
-----Output-----
The output is a list of characters representing the longest common prefix of the two input lists. The output list satisfies the following conditions:
β’ Its length is less than or equal to the length of each input list.
β’ It is exactly the prefix of both str1 and str2.
β’ It is empty if the first characters of the inputs differ or if one of the lists is empty.
-----Note-----
It is assumed that both inputs are provided as valid lists of characters. The function always returns the correct longest common prefix based on the inputs. | -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def LongestCommonPrefix_precond (str1 : List Char) (str2 : List Char) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def LongestCommonPrefix (str1 : List Char) (str2 : List Char) (h_precond : LongestCommonPrefix_precond (str1) (str2)) : List Char :=
-- !benchmark @start code
let minLength := Nat.min str1.length str2.length
let rec aux (idx : Nat) (acc : List Char) : List Char :=
if idx < minLength then
match str1[idx]?, str2[idx]? with
| some c1, some c2 =>
if c1 β c2 then acc
else aux (idx + 1) (acc ++ [c1])
| _, _ => acc
else acc
aux 0 []
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def LongestCommonPrefix_postcond (str1 : List Char) (str2 : List Char) (result: List Char) (h_precond : LongestCommonPrefix_precond (str1) (str2)) :=
-- !benchmark @start postcond
(result.length β€ str1.length) β§ (result = str1.take result.length) β§
(result.length β€ str2.length) β§ (result = str2.take result.length) β§
(result.length = str1.length β¨ result.length = str2.length β¨
(str1[result.length]? β str2[result.length]?))
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem LongestCommonPrefix_spec_satisfied (str1: List Char) (str2: List Char) (h_precond : LongestCommonPrefix_precond (str1) (str2)) :
LongestCommonPrefix_postcond (str1) (str2) (LongestCommonPrefix (str1) (str2) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "LongestCommonPrefix",
"parameters": {
"param_name": [
"str1",
"str2"
],
"param_type": [
"List Char",
"List Char"
]
},
"return_type": "List Char"
} | {
"upstream": {
"name": "clover",
"link": "https://github.com/ChuyueSun/Clover",
"task_id": "Clover_longest_prefix",
"student_id": null
}
} | {
"input": [
"{\"str1\": \"['a', 'b', 'c']\", \"str2\": \"['a', 'b', 'd']\"}",
"{\"str1\": \"['x', 'y', 'z']\", \"str2\": \"['x', 'y', 'z']\"}",
"{\"str1\": \"['w', 'o']\", \"str2\": \"['w', 'o', 'w']\"}",
"{\"str1\": \"['a', 'x']\", \"str2\": \"['b', 'y']\"}",
"{\"str1\": \"[]\", \"str2\": \"['h', 'e', 'l', 'l', 'o']\"}"
],
"expected": [
[
"['a', 'b']"
],
[
"['x', 'y', 'z']"
],
[
"['w', 'o']"
],
[
"[]"
],
[
"[]"
]
],
"unexpected": [
[
"['a']",
"['a', 'b', 'c']"
],
[
"['x', 'y']",
"['x', 'z']"
],
[
"['w']",
"['o']",
"['w', 'o', 'w']"
],
[
"['a']",
"['b']"
],
[
"['h']",
"['e']"
]
]
} | {
"input": []
} | basic |
verina_basic_56 | -----Description-----
The problem is to update a destination array by replacing a specific segment with values taken from a source array. Given two arrays, starting positions, and a length, the task is to construct a new array where the segment in the destination from the specified starting index for the given length is replaced by the corresponding segment from the source, while all other elements remain unchanged.
-----Input-----
The input consists of:
β’ src: An array of integers representing the source array.
β’ sStart: A natural number indicating the starting index in src from where to begin copying.
β’ dest: An array of integers representing the destination array.
β’ dStart: A natural number indicating the starting index in dest where the segment will be replaced.
β’ len: A natural number specifying the number of elements to copy.
-----Output-----
The output is an array of integers that:
β’ Has the same size as the destination array (dest).
β’ Preserves the original elements of dest except for the segment starting at index dStart of length len, which is replaced by the corresponding segment from src.
β’ Under the preconditions that src.size β₯ sStart + len and dest.size β₯ dStart + len, guarantees that:
- All elements with indices less than dStart remain as in dest.
- All elements with indices greater than or equal to dStart + len remain as in dest.
- For each index i with 0 β€ i < len, the element at index dStart + i in the output equals the element at index sStart + i in src.
-----Note-----
It is assumed that the input arrays satisfy the preconditions: the source array has enough elements starting from sStart and the destination array has enough space starting from dStart to accommodate the copied segment. | -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def copy_precond (src : Array Int) (sStart : Nat) (dest : Array Int) (dStart : Nat) (len : Nat) : Prop :=
-- !benchmark @start precond
src.size β₯ sStart + len β§
dest.size β₯ dStart + len
-- !benchmark @end precond
-- !benchmark @start code_aux
def updateSegment : Array Int β Array Int β Nat β Nat β Nat β Array Int
| r, src, sStart, dStart, 0 => r
| r, src, sStart, dStart, n+1 =>
let rNew := r.set! (dStart + n) (src[sStart + n]!)
updateSegment rNew src sStart dStart n
-- !benchmark @end code_aux
def copy (src : Array Int) (sStart : Nat) (dest : Array Int) (dStart : Nat) (len : Nat) (h_precond : copy_precond (src) (sStart) (dest) (dStart) (len)) : Array Int :=
-- !benchmark @start code
if len = 0 then dest
else
let r := dest
updateSegment r src sStart dStart len
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def copy_postcond (src : Array Int) (sStart : Nat) (dest : Array Int) (dStart : Nat) (len : Nat) (result: Array Int) (h_precond : copy_precond (src) (sStart) (dest) (dStart) (len)) :=
-- !benchmark @start postcond
result.size = dest.size β§
(β i, i < dStart β result[i]! = dest[i]!) β§
(β i, dStart + len β€ i β i < result.size β result[i]! = dest[i]!) β§
(β i, i < len β result[dStart + i]! = src[sStart + i]!)
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem copy_spec_satisfied (src: Array Int) (sStart: Nat) (dest: Array Int) (dStart: Nat) (len: Nat) (h_precond : copy_precond (src) (sStart) (dest) (dStart) (len)) :
copy_postcond (src) (sStart) (dest) (dStart) (len) (copy (src) (sStart) (dest) (dStart) (len) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "copy",
"parameters": {
"param_name": [
"src",
"sStart",
"dest",
"dStart",
"len"
],
"param_type": [
"Array Int",
"Nat",
"Array Int",
"Nat",
"Nat"
]
},
"return_type": "Array Int"
} | {
"upstream": {
"name": "clover",
"link": "https://github.com/ChuyueSun/Clover",
"task_id": "Clover_copy_part",
"student_id": null
}
} | {
"input": [
"{\"src\": \"#[10, 20, 30, 40, 50]\", \"sStart\": 1, \"dest\": \"#[1, 2, 3, 4, 5, 6]\", \"dStart\": 3, \"len\": 2}",
"{\"src\": \"#[5, 6, 7, 8]\", \"sStart\": 0, \"dest\": \"#[9, 9, 9, 9, 9]\", \"dStart\": 1, \"len\": 3}",
"{\"src\": \"#[100, 200]\", \"sStart\": 0, \"dest\": \"#[1, 2, 3]\", \"dStart\": 1, \"len\": 0}",
"{\"src\": \"#[10, 20, 30, 40, 50]\", \"sStart\": 0, \"dest\": \"#[0, 0, 0, 0, 0]\", \"dStart\": 0, \"len\": 5}",
"{\"src\": \"#[7, 8, 9, 10]\", \"sStart\": 2, \"dest\": \"#[1, 2, 3, 4, 5, 6]\", \"dStart\": 4, \"len\": 2}"
],
"expected": [
[
"#[1, 2, 3, 20, 30, 6]"
],
[
"#[9, 5, 6, 7, 9]"
],
[
"#[1, 2, 3]"
],
[
"#[10, 20, 30, 40, 50]"
],
[
"#[1, 2, 3, 4, 9, 10]"
]
],
"unexpected": [
[
"#[1, 2, 3, 10, 30, 6]",
"#[1, 2, 3, 20, 40, 6]",
"#[1, 2, 20, 30, 6, 0]"
],
[
"#[9, 9, 5, 7, 9]",
"#[9, 5, 7, 6, 9]",
"#[9, 5, 6, 9, 9]"
],
[
"#[1, 0, 3]",
"#[0, 2, 3]",
"#[1, 2, 0]"
],
[
"#[10, 20, 30, 40, 60]",
"#[0, 20, 30, 40, 50]",
"#[10, 20, 30, 40, 0]"
],
[
"#[1, 2, 3, 9, 4, 10]",
"#[1, 2, 9, 4, 3, 10]",
"#[1, 2, 3, 4, 10, 9]"
]
]
} | {
"input": [
"{'src': '#[10, 20, 30]', 'sStart': 1, 'dest': '#[1, 2, 3, 4]', 'dStart': 2, 'len': 3}"
]
} | basic |
verina_basic_34 | -----Description-----
This task requires writing a Lean 4 method that extracts even numbers from an array of integers. The method should return a new array containing only the even numbers found in the input array, while preserving the order in which they appear.
-----Input-----
The input consists of:
arr: An array of integers.
-----Output-----
The output is an array of integers:
Returns an array containing all the even numbers from the input array. Specifically:
- Every element in the output array is an even integer.
- All even integers present in the input array are included in the output array.
- The relative order of the even integers is preserved as in the input array.
-----Note-----
There are no preconditions for this task; the method will work with any array, including empty arrays (which are not null). | -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
def isEven (n : Int) : Bool :=
n % 2 = 0
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def findEvenNumbers_precond (arr : Array Int) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def findEvenNumbers (arr : Array Int) (h_precond : findEvenNumbers_precond (arr)) : Array Int :=
-- !benchmark @start code
arr.foldl (fun acc x => if isEven x then acc.push x else acc) #[]
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def findEvenNumbers_postcond (arr : Array Int) (result: Array Int) (h_precond : findEvenNumbers_precond (arr)) :=
-- !benchmark @start postcond
(β x, x β result β isEven x β§ x β arr.toList) β§
(β x, x β arr.toList β isEven x β x β result) β§
(β x y, x β arr.toList β y β arr.toList β
isEven x β isEven y β
arr.toList.idxOf x β€ arr.toList.idxOf y β
result.toList.idxOf x β€ result.toList.idxOf y)
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem findEvenNumbers_spec_satisfied (arr: Array Int) (h_precond : findEvenNumbers_precond (arr)) :
findEvenNumbers_postcond (arr) (findEvenNumbers (arr) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "findEvenNumbers",
"parameters": {
"param_name": [
"arr"
],
"param_type": [
"Array Int"
]
},
"return_type": "Array Int"
} | {
"upstream": {
"name": "dafny-synthesis",
"link": "https://github.com/Mondego/dafny-synthesis",
"task_id": "task_id_629",
"student_id": null
}
} | {
"input": [
"{\"arr\": \"#[1, 2, 3, 4, 5, 6]\"}",
"{\"arr\": \"#[7, 8, 10, 13, 14]\"}",
"{\"arr\": \"#[1, 3, 5, 7]\"}",
"{\"arr\": \"#[]\"}",
"{\"arr\": \"#[0, -2, -3, -4, 5]\"}"
],
"expected": [
[
"#[2, 4, 6]"
],
[
"#[8, 10, 14]"
],
[
"#[]"
],
[
"#[]"
],
[
"#[0, -2, -4]"
]
],
"unexpected": [
[
"#[1, 2, 3]",
"#[2, 3, 4, 6]"
],
[
"#[7, 8, 10]",
"#[8, 14]"
],
[
"#[1]",
"#[1, 3]"
],
[
"#[0]",
"#[1]"
],
[
"#[0, -3, -4]",
"#[-2, -4]"
]
]
} | {
"input": []
} | basic |
verina_basic_65 | -----Description-----
This task involves computing the integer square root of a given natural number. The goal is to determine the largest natural number r that satisfies r * r β€ N and N < (r + 1) * (r + 1).
-----Input-----
The input consists of:
β’ N: A natural number.
-----Output-----
The output is a natural number r that meets the following conditions:
β’ r * r β€ N
β’ N < (r + 1) * (r + 1)
-----Note-----
The implementation relies on a recursive strategy to iteratively increment r until (r + 1)*(r + 1) exceeds N. Edge cases, such as N = 0, should be handled correctly. | -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def SquareRoot_precond (N : Nat) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def SquareRoot (N : Nat) (h_precond : SquareRoot_precond (N)) : Nat :=
-- !benchmark @start code
let rec boundedLoop : Nat β Nat β Nat
| 0, r => r
| bound+1, r =>
if (r + 1) * (r + 1) β€ N then
boundedLoop bound (r + 1)
else
r
boundedLoop (N+1) 0
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def SquareRoot_postcond (N : Nat) (result: Nat) (h_precond : SquareRoot_precond (N)) :=
-- !benchmark @start postcond
result * result β€ N β§ N < (result + 1) * (result + 1)
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem SquareRoot_spec_satisfied (N: Nat) (h_precond : SquareRoot_precond (N)) :
SquareRoot_postcond (N) (SquareRoot (N) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "SquareRoot",
"parameters": {
"param_name": [
"N"
],
"param_type": [
"Nat"
]
},
"return_type": "Nat"
} | {
"upstream": {
"name": "clover",
"link": "https://github.com/ChuyueSun/Clover",
"task_id": "Clover_integer_square_root",
"student_id": null
}
} | {
"input": [
"{\"N\": 0}",
"{\"N\": 1}",
"{\"N\": 15}",
"{\"N\": 16}",
"{\"N\": 26}"
],
"expected": [
[
"0"
],
[
"1"
],
[
"3"
],
[
"4"
],
[
"5"
]
],
"unexpected": [
[
"1",
"2"
],
[
"0",
"2"
],
[
"2",
"4",
"5"
],
[
"3",
"5",
"6"
],
[
"4",
"6",
"7"
]
]
} | {
"input": []
} | basic |
verina_advanced_35 | -----Description-----
This task requires writing a Lean 4 function that finds the majority element in a list of integers. The majority element is the element that appears more than βn/2β times, where n is the listβs length. You may assume that a majority element always exists in the input.
-----Input-----
- nums: A list of integers of length β₯ 1, containing a majority element.
-----Output-----
- An integer: the element that appears more than βn/2β times.
| -- !benchmark @start import type=solution
import Std.Data.HashMap
open Std
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible]
def majorityElement_precond (nums : List Int) : Prop :=
-- !benchmark @start precond
nums.length > 0 β§ nums.any (fun x => nums.count x > nums.length / 2)
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def majorityElement (nums : List Int) (h_precond : majorityElement_precond (nums)) : Int :=
-- !benchmark @start code
Id.run do
let mut counts : HashMap Int Nat := {}
let n := nums.length
for x in nums do
let count := counts.getD x 0
counts := counts.insert x (count + 1)
match counts.toList.find? (fun (_, c) => c > n / 2) with
| some (k, _) => k
| none => 0
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible]
def majorityElement_postcond (nums : List Int) (result: Int) (h_precond : majorityElement_precond (nums)) : Prop :=
-- !benchmark @start postcond
let n := nums.length
(nums.count result) > n / 2
β§ β x, x β result β nums.count x β€ n / 2
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem majorityElement_spec_satisfied (nums: List Int) (h_precond : majorityElement_precond (nums)) :
majorityElement_postcond (nums) (majorityElement (nums) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "majorityElement",
"parameters": {
"param_name": [
"nums"
],
"param_type": [
"List Int"
]
},
"return_type": "Int"
} | {
"upstream": {
"name": "lab_assignment",
"link": "https://leetcode.com/problems/majority-element/description/",
"task_id": "lab_majorityElement_324976035",
"student_id": [
22
]
}
} | {
"input": [
"{\"nums\": \"[3, 2, 3]\"}",
"{\"nums\": \"[2, 2, 1, 1, 1, 2, 2]\"}",
"{\"nums\": \"[1, 1, 1, 2, 3, 1]\"}",
"{\"nums\": \"[0, 0, 0, 0]\"}",
"{\"nums\": \"[7]\"}"
],
"expected": [
[
"3"
],
[
"2"
],
[
"1"
],
[
"0"
],
[
"7"
]
],
"unexpected": [
[
"2"
],
[
"1"
],
[
"2",
"3"
],
[
"1"
],
[]
]
} | {
"input": [
"{'nums': '[1, 2, 3]'}",
"{'nums': '[]'}"
]
} | advanced |
verina_advanced_8 | -----Description-----
This task requires writing a Lean 4 method that determines whether it is possible to complete a circular journey around a set of gas stations. Each gas station provides a certain amount of gas, and traveling from one station to the next consumes a certain amount of gas.
You start the journey at one of the gas stations with an empty tank. The goal is to find the starting station's index that allows completing the entire circuit once in the clockwise direction without running out of gas. If such a station exists, return its index. Otherwise, return -1.
If multiple solutions exist, return the one with the smallest starting gas station index.
-----Input-----
The input consists of two arrays:
gas: An array of integers where gas[i] represents the amount of gas available at the ith station.
cost: An array of integers where cost[i] is the amount of gas required to travel from station i to station i + 1.
-----Output-----
The output is an integer:
Returns the index of the starting gas station that allows a complete trip around the circuit. If it is not possible to complete the circuit, return -1.
| -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible]
def canCompleteCircuit_precond (gas : List Int) (cost : List Int) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def canCompleteCircuit (gas : List Int) (cost : List Int) (h_precond : canCompleteCircuit_precond (gas) (cost)) : Int :=
-- !benchmark @start code
let totalGas := gas.foldl (Β· + Β·) 0
let totalCost := cost.foldl (Β· + Β·) 0
if totalGas < totalCost then
-1
else
let rec loop (g c : List Int) (idx : Nat) (tank : Int) (start : Nat) : Int :=
match g, c with
| [], [] => start
| gi :: gs, ci :: cs =>
let tank' := tank + gi - ci
if tank' < 0 then
loop gs cs (idx + 1) 0 (idx + 1)
else
loop gs cs (idx + 1) tank' start
| _, _ => -1 -- lengths donβt match
let zipped := List.zip gas cost
let rec walk (pairs : List (Int Γ Int)) (i : Nat) (tank : Int) (start : Nat) : Int :=
match pairs with
| [] => start
| (g, c) :: rest =>
let newTank := tank + g - c
if newTank < 0 then
walk rest (i + 1) 0 (i + 1)
else
walk rest (i + 1) newTank start
walk zipped 0 0 0
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible]
def canCompleteCircuit_postcond (gas : List Int) (cost : List Int) (result: Int) (h_precond : canCompleteCircuit_precond (gas) (cost)) : Prop :=
-- !benchmark @start postcond
let valid (start : Nat) := List.range gas.length |>.all (fun i =>
let acc := List.range (i + 1) |>.foldl (fun t j =>
let jdx := (start + j) % gas.length
t + gas[jdx]! - cost[jdx]!) 0
acc β₯ 0)
-- For result = -1: It's impossible to complete the circuit starting from any index
-- In other words, there's no starting point from which we can always maintain a non-negative gas tank
(result = -1 β (List.range gas.length).all (fun start => Β¬ valid start)) β§
-- For result β₯ 0: This is the valid starting point
-- When starting from this index, the gas tank never becomes negative during the entire circuit
(result β₯ 0 β result < gas.length β§ valid result.toNat β§ (List.range result.toNat).all (fun start => Β¬ valid start))
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem canCompleteCircuit_spec_satisfied (gas: List Int) (cost: List Int) (h_precond : canCompleteCircuit_precond (gas) (cost)) :
canCompleteCircuit_postcond (gas) (cost) (canCompleteCircuit (gas) (cost) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "canCompleteCircuit",
"parameters": {
"param_name": [
"gas",
"cost"
],
"param_type": [
"List Int",
"List Int"
]
},
"return_type": "Int"
} | {
"upstream": {
"name": "lab_assignment",
"link": "https://leetcode.com/problems/gas-station",
"task_id": "lab_canCompleteCircuit_324678911",
"student_id": [
6
]
}
} | {
"input": [
"{\"gas\": \"[1, 2, 3, 4, 5]\", \"cost\": \"[3, 4, 5, 1, 2]\"}",
"{\"gas\": \"[2, 3, 4]\", \"cost\": \"[3, 4, 3]\"}",
"{\"gas\": \"[5, 1, 2, 3, 4]\", \"cost\": \"[4, 4, 1, 5, 1]\"}",
"{\"gas\": \"[3, 3, 4]\", \"cost\": \"[3, 4, 4]\"}",
"{\"gas\": \"[1, 2, 3]\", \"cost\": \"[1, 2, 3]\"}",
"{\"gas\": \"[1, 2, 3, 4]\", \"cost\": \"[2, 2, 2, 2]\"}",
"{\"gas\": \"[0, 0, 0]\", \"cost\": \"[1, 1, 1]\"}"
],
"expected": [
[
"3"
],
[
"-1"
],
[
"4"
],
[
"-1"
],
[
"0"
],
[
"1"
],
[
"-1"
]
],
"unexpected": [
[
"-1",
"0",
"1",
"2",
"4"
],
[
"0",
"1",
"2",
"3"
],
[
"-1",
"0",
"1",
"2",
"3"
],
[
"0",
"1",
"2"
],
[
"-1",
"1",
"2"
],
[
"-1",
"0",
"2",
"3"
],
[
"0",
"1",
"2"
]
]
} | {
"input": []
} | advanced |
verina_advanced_12 | -----Description-----
Write a Lean 4 function that returns the first duplicate integer found in a list. The function should return the value of the first duplicate it encounters, scanning from left to right. If no duplicates exist, return -1.
-----Input-----
lst: A list of integers.
-----Output-----
An integer representing the first duplicated value if any exists, otherwise -1.
| -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible]
def firstDuplicate_precond (lst : List Int) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def firstDuplicate (lst : List Int) (h_precond : firstDuplicate_precond (lst)) : Int :=
-- !benchmark @start code
let rec helper (seen : List Int) (rem : List Int) : Int :=
match rem with
| [] => -1
| h :: t => if seen.contains h then h else helper (h :: seen) t
helper [] lst
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible]
def firstDuplicate_postcond (lst : List Int) (result: Int) (h_precond : firstDuplicate_precond (lst)) : Prop :=
-- !benchmark @start postcond
-- if result = -1, then lst does not contain any duplicates
(result = -1 β List.Nodup lst) β§
-- if result is not -1, then it is the first duplicate in lst
(result β -1 β
lst.count result > 1 β§
(lst.filter (fun x => lst.count x > 1)).head? = some result
)
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem firstDuplicate_spec_satisfied (lst: List Int) (h_precond : firstDuplicate_precond (lst)) :
firstDuplicate_postcond (lst) (firstDuplicate (lst) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "firstDuplicate",
"parameters": {
"param_name": [
"lst"
],
"param_type": [
"List Int"
]
},
"return_type": "Int"
} | {
"upstream": {
"name": "lab_assignment",
"link": "NA-These are typical problems we solve, im sure their links exist, I just didn't copy directly from any link",
"task_id": "lab_firstDuplicate_325276763",
"student_id": [
10
]
}
} | {
"input": [
"{\"lst\": \"[1, 2, 3, 2, 4]\"}",
"{\"lst\": \"[5, 1, 2, 3, 4, 5]\"}",
"{\"lst\": \"[1, 2, 3, 4, 5]\"}",
"{\"lst\": \"[7, 7, 7, 7]\"}",
"{\"lst\": \"[]\"}"
],
"expected": [
[
"2"
],
[
"5"
],
[
"-1"
],
[
"7"
],
[
"-1"
]
],
"unexpected": [
[
"1",
"3",
"-1"
],
[
"1",
"0"
],
[
"1",
"2",
"3"
],
[
"-1"
],
[
"0",
"1",
"2"
]
]
} | {
"input": []
} | advanced |
verina_advanced_11 | -----Description-----
This task requires writing a Lean 4 method that finds the **majority element** in a list of integers. A majority element is defined as an element that appears **strictly more than half** the number of times in the list.
If such an element exists, the method should return that element. Otherwise, it should return `-1`. The implementation must ensure that the result is either the majority element (if one exists) or `-1` (when no such element appears more than βn/2β times).
-----Input-----
The input consists of a list of integers:
- lst: A list of integers, which may include duplicates and negative numbers. The list may also be empty.
-----Output-----
The output is a single integer:
- If a majority element exists in the input list, return that element.
- If no majority element exists, return `-1`.
| -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def findMajorityElement_precond (lst : List Int) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
def countOccurrences (n : Int) (lst : List Int) : Nat :=
lst.foldl (fun acc x => if x = n then acc + 1 else acc) 0
-- !benchmark @end code_aux
def findMajorityElement (lst : List Int) (h_precond : findMajorityElement_precond (lst)) : Int :=
-- !benchmark @start code
let n := lst.length
let majority := lst.find? (fun x => countOccurrences x lst > n / 2)
match majority with
| some x => x
| none => -1
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def findMajorityElement_postcond (lst : List Int) (result: Int) (h_precond : findMajorityElement_precond (lst)) : Prop :=
-- !benchmark @start postcond
let count := fun x => (lst.filter (fun y => y = x)).length
let n := lst.length
let majority := count result > n / 2 β§ lst.all (fun x => count x β€ n / 2 β¨ x = result)
(result = -1 β lst.all (count Β· β€ n / 2) β¨ majority) β§
(result β -1 β majority)
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem findMajorityElement_spec_satisfied (lst: List Int) (h_precond : findMajorityElement_precond (lst)) :
findMajorityElement_postcond (lst) (findMajorityElement (lst) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "findMajorityElement",
"parameters": {
"param_name": [
"lst"
],
"param_type": [
"List Int"
]
},
"return_type": "Int"
} | {
"upstream": {
"name": "lab_assignment",
"link": "[{\"text_file_id\"=>803067667}]",
"task_id": "lab_findMajorityElement_325106966",
"student_id": [
9
]
}
} | {
"input": [
"{\"lst\": \"[1, 2, 1, 1]\"}",
"{\"lst\": \"[1, 2, 3, 4]\"}",
"{\"lst\": \"[2, 2, 2, 2, 3, 3]\"}",
"{\"lst\": \"[]\"}",
"{\"lst\": \"[5, 5, 5, 5, 5, 5]\"}",
"{\"lst\": \"[-1, -1, -1, 2, 2]\"}",
"{\"lst\": \"[-3, -3, -3, -3, 1]\"}"
],
"expected": [
[
"1"
],
[
"-1"
],
[
"2"
],
[
"-1"
],
[
"5"
],
[
"-1"
],
[
"-3"
]
],
"unexpected": [
[
"2",
"-1"
],
[
"1",
"2",
"3",
"4"
],
[
"3",
"-1"
],
[
"0",
"1"
],
[
"0",
"-1"
],
[
"2"
],
[
"1",
"-1"
]
]
} | {
"input": []
} | advanced |
verina_basic_66 | -----Description-----
This task focuses on determining if a given integer is even. The problem requires checking whether the integer can be represented as twice another integer, meaning it is divisible by 2 without any remainder.
-----Input-----
The input consists of a single integer:
β’ x: An integer to be evaluated.
-----Output-----
The output is a boolean value:
β’ true if x is even (x mod 2 equals 0).
β’ false if x is odd.
-----Note-----
No additional preconditions are required. The method should work correctly for any integer value. | -- !benchmark @start import type=solution
import Mathlib
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def ComputeIsEven_precond (x : Int) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def ComputeIsEven (x : Int) (h_precond : ComputeIsEven_precond (x)) : Bool :=
-- !benchmark @start code
if x % 2 = 0 then true else false
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def ComputeIsEven_postcond (x : Int) (result: Bool) (h_precond : ComputeIsEven_precond (x)) :=
-- !benchmark @start postcond
result = true β β k : Int, x = 2 * k
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem ComputeIsEven_spec_satisfied (x: Int) (h_precond : ComputeIsEven_precond (x)) :
ComputeIsEven_postcond (x) (ComputeIsEven (x) h_precond) h_precond := by
-- !benchmark @start proof
unfold ComputeIsEven ComputeIsEven_postcond
simp
rfl
-- !benchmark @end proof
| {
"name": "ComputeIsEven",
"parameters": {
"param_name": [
"x"
],
"param_type": [
"Int"
]
},
"return_type": "Bool"
} | {
"upstream": {
"name": "clover",
"link": "https://github.com/ChuyueSun/Clover",
"task_id": "Clover_is_even",
"student_id": null
}
} | {
"input": [
"{\"x\": 4}",
"{\"x\": 7}",
"{\"x\": 0}",
"{\"x\": -2}",
"{\"x\": -3}"
],
"expected": [
[
"True"
],
[
"False"
],
[
"True"
],
[
"True"
],
[
"False"
]
],
"unexpected": [
[
"False"
],
[
"True"
],
[
"False"
],
[
"False"
],
[
"True"
]
]
} | {
"input": []
} | basic |
verina_advanced_32 | -----Description-----
This test implements a function in Lean 4 that finds the length of the longest increasing subsequence in a list of integers. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements. An increasing subsequence is one in which the elements are in strictly increasing order.
-----Input-----
numbers: A list of integers.
-----Output-----
A natural number representing the length of the longest increasing subsequence in the input list. If the list is empty, the function returns 0.
| -- !benchmark @start import type=solution
import Mathlib
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def longestIncreasingSubsequence_precond (numbers : List Int) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def longestIncreasingSubsequence (numbers : List Int) (h_precond : longestIncreasingSubsequence_precond (numbers)) : Nat :=
-- !benchmark @start code
let rec buildTables : List Int β List Int β List Nat β Nat β Nat
| [], _, lengths, _ =>
let rec findMaxLength : List Nat β Nat
| [] => 0
| x :: xs =>
let maxRest := findMaxLength xs
if x > maxRest then x else maxRest
findMaxLength lengths
| currNum :: restNums, prevNums, lengths, idx =>
let rec findLengthEndingAtCurr : List Int β List Nat β Nat β Nat
| [], _, best => best
| prevVal :: restVals, prevLen :: restLens, best =>
if prevVal < currNum then
findLengthEndingAtCurr restVals restLens (max best prevLen)
else
findLengthEndingAtCurr restVals restLens best
| _, _, best => best
let bestPrevLen := findLengthEndingAtCurr prevNums lengths 0
let currLength := bestPrevLen + 1
buildTables restNums (prevNums ++ [currNum]) (lengths ++ [currLength]) (idx + 1)
match numbers with
| [] => 0
| [x] => 1
| first :: rest => buildTables rest [first] [1] 1
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def longestIncreasingSubsequence_postcond (numbers : List Int) (result: Nat) (h_precond : longestIncreasingSubsequence_precond (numbers)) : Prop :=
-- !benchmark @start postcond
let allSubseq := (numbers.foldl fun acc x => acc ++ acc.map (fun sub => x :: sub)) [[]] |>.map List.reverse
let increasingSubseqLens := allSubseq.filter (fun l => List.Pairwise (Β· < Β·) l) |>.map (Β·.length)
increasingSubseqLens.contains result β§ increasingSubseqLens.all (Β· β€ result)
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem longestIncreasingSubsequence_spec_satisfied (numbers: List Int) (h_precond : longestIncreasingSubsequence_precond (numbers)) :
longestIncreasingSubsequence_postcond (numbers) (longestIncreasingSubsequence (numbers) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "longestIncreasingSubsequence",
"parameters": {
"param_name": [
"numbers"
],
"param_type": [
"List Int"
]
},
"return_type": "Nat"
} | {
"upstream": {
"name": "lab_assignment",
"link": "",
"task_id": "lab_longestIncreasingSubsequence_324969521",
"student_id": [
26
]
}
} | {
"input": [
"{\"numbers\": \"[10, 22, 9, 33, 21, 50, 41, 60]\"}",
"{\"numbers\": \"[3, 10, 2, 1, 20]\"}",
"{\"numbers\": \"[50, 3, 10, 7, 40, 80]\"}",
"{\"numbers\": \"[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]\"}",
"{\"numbers\": \"[1, 2, 3, 4, 5]\"}",
"{\"numbers\": \"[]\"}",
"{\"numbers\": \"[5]\"}",
"{\"numbers\": \"[5, 5, 5, 5]\"}"
],
"expected": [
[
"5"
],
[
"3"
],
[
"4"
],
[
"1"
],
[
"5"
],
[
"0"
],
[
"1"
],
[
"1"
]
],
"unexpected": [
[
"4",
"6",
"8"
],
[
"2",
"4",
"5"
],
[
"3",
"5",
"6"
],
[
"0",
"2",
"10"
],
[
"1",
"4",
"6"
],
[
"1",
"2",
"3"
],
[
"0",
"2"
],
[
"0",
"4"
]
]
} | {
"input": []
} | advanced |
verina_basic_84 | -----Description-----
You are given an array of integers and a threshold value k. The problem is to create a new array where every element greater than k is replaced with -1 while every other element remains unchanged.
-----Input-----
The input consists of:
β’ arr: An array of integers.
β’ k: An integer used as the threshold for replacement.
-----Output-----
The output is an array of integers that satisfies the following conditions:
β’ For every index i, if arr[i] is greater than k, then the returned array at index i is -1.
β’ For every index i, if arr[i] is less than or equal to k, then the returned array at index i remains unchanged.
-----Note-----
It is assumed that the input array may be empty or non-empty, and that k can be any integer. There are no additional preconditions. | -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def replace_precond (arr : Array Int) (k : Int) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
def replace_loop (oldArr : Array Int) (k : Int) : Nat β Array Int β Array Int
| i, acc =>
if i < oldArr.size then
if (oldArr[i]!) > k then
replace_loop oldArr k (i+1) (acc.set! i (-1))
else
replace_loop oldArr k (i+1) acc
else
acc
-- !benchmark @end code_aux
def replace (arr : Array Int) (k : Int) (h_precond : replace_precond (arr) (k)) : Array Int :=
-- !benchmark @start code
replace_loop arr k 0 arr
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def replace_postcond (arr : Array Int) (k : Int) (result: Array Int) (h_precond : replace_precond (arr) (k)) :=
-- !benchmark @start postcond
(β i : Nat, i < arr.size β (arr[i]! > k β result[i]! = -1)) β§
(β i : Nat, i < arr.size β (arr[i]! β€ k β result[i]! = arr[i]!))
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem replace_spec_satisfied (arr: Array Int) (k: Int) (h_precond : replace_precond (arr) (k)) :
replace_postcond (arr) (k) (replace (arr) (k) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "replace",
"parameters": {
"param_name": [
"arr",
"k"
],
"param_type": [
"Array Int",
"Int"
]
},
"return_type": "Array Int"
} | {
"upstream": {
"name": "clover",
"link": "https://github.com/ChuyueSun/Clover",
"task_id": "Clover_replace",
"student_id": null
}
} | {
"input": [
"{\"arr\": \"#[1, 5, 3, 10]\", \"k\": 4}",
"{\"arr\": \"#[-1, 0, 1, 2]\", \"k\": 2}",
"{\"arr\": \"#[100, 50, 100]\", \"k\": 100}",
"{\"arr\": \"#[-5, -2, 0, 3]\", \"k\": -3}",
"{\"arr\": \"#[1, 2, 3]\", \"k\": 5}"
],
"expected": [
[
"#[1, -1, 3, -1]"
],
[
"#[-1, 0, 1, 2]"
],
[
"#[100, 50, 100]"
],
[
"#[-5, -1, -1, -1]"
],
[
"#[1, 2, 3]"
]
],
"unexpected": [
[
"#[1, 5, 3, 10]",
"#[1, -1, 3, 10]"
],
[
"#[0, 0, 1, 2]",
"#[-1, 0, 1, 1]"
],
[
"#[100, 50, -1]",
"#[100, 50, 50]"
],
[
"#[-5, -2, -1, -1]",
"#[-5, -1, 0, -1]"
],
[
"#[1, 3, 3]",
"#[1, 2, -1]"
]
]
} | {
"input": []
} | basic |
verina_basic_35 | -----Description-----
This task requires writing a Lean 4 method that rearranges an array of integers by moving all zero values to the end of the array. The method should ensure that the relative order of the non-zero elements remains the same, the overall size of the array is unchanged, and the number of zeroes in the array stays constant.
-----Input-----
The input consists of:
arr: An array of integers.
-----Output-----
The output is an array of integers:
Returns an array where:
- The length is the same as that of the input array.
- All zero values are positioned at the end.
- The relative order of non-zero elements is preserved.
- The count of zero values remains the same as in the input array.
-----Note-----
There are no preconditions; the method will always work for any array of integers. | -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def MoveZeroesToEnd_precond (arr : Array Int) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def MoveZeroesToEnd (arr : Array Int) (h_precond : MoveZeroesToEnd_precond (arr)) : Array Int :=
-- !benchmark @start code
let nonZeros := arr.toList.filter (Β· β 0)
let zeros := arr.toList.filter (Β· = 0)
Array.mk (nonZeros ++ zeros)
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def MoveZeroesToEnd_postcond (arr : Array Int) (result: Array Int) (h_precond : MoveZeroesToEnd_precond (arr)) :=
-- !benchmark @start postcond
let firstResZeroIdx := result.toList.idxOf 0
List.isPerm result.toList arr.toList β§
result.toList.take firstResZeroIdx = arr.toList.filter (Β· β 0) β§
result.toList.drop firstResZeroIdx = arr.toList.filter (Β· = 0)
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem MoveZeroesToEnd_spec_satisfied (arr: Array Int) (h_precond : MoveZeroesToEnd_precond (arr)) :
MoveZeroesToEnd_postcond (arr) (MoveZeroesToEnd (arr) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "MoveZeroesToEnd",
"parameters": {
"param_name": [
"arr"
],
"param_type": [
"Array Int"
]
},
"return_type": "Array Int"
} | {
"upstream": {
"name": "dafny-synthesis",
"link": "https://github.com/Mondego/dafny-synthesis",
"task_id": "task_id_632",
"student_id": null
}
} | {
"input": [
"{\"arr\": \"#[0, 1, 0, 3, 12]\"}",
"{\"arr\": \"#[0, 0, 1]\"}",
"{\"arr\": \"#[1, 2, 3]\"}",
"{\"arr\": \"#[0, 0, 0]\"}",
"{\"arr\": \"#[]\"}"
],
"expected": [
[
"#[1, 3, 12, 0, 0]"
],
[
"#[1, 0, 0]"
],
[
"#[1, 2, 3]"
],
[
"#[0, 0, 0]"
],
[
"#[]"
]
],
"unexpected": [
[
"#[0, 1, 0, 3, 12]",
"#[1, 0, 3, 12, 0]"
],
[
"#[0, 0, 1]",
"#[0, 1, 0]"
],
[
"#[1, 3, 2]",
"#[2, 1, 3]"
],
[
"#[1, 0, 0]",
"#[0, 1, 0]"
],
[
"#[0]",
"#[1]"
]
]
} | {
"input": []
} | basic |
verina_basic_93 | -----Description-----
This task requires swapping two 8-bit unsigned integers. Given two unsigned integer inputs, the goal is to produce an output pair where the first element is the original second input and the second element is the original first input. The problem focuses solely on exchanging the values without specifying any particular method to achieve the swap.
-----Input-----
The input consists of:
β’ X: A UInt8 value.
β’ Y: A UInt8 value.
-----Output-----
The output is a pair of UInt8 values (newX, newY) where:
β’ newX is equal to the original Y.
β’ newY is equal to the original X.
-----Note-----
There are no additional preconditions; the function is meant to work correctly for any pair of UInt8 values by leveraging bitwise xor operations. | -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def SwapBitvectors_precond (X : UInt8) (Y : UInt8) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def SwapBitvectors (X : UInt8) (Y : UInt8) (h_precond : SwapBitvectors_precond (X) (Y)) : UInt8 Γ UInt8 :=
-- !benchmark @start code
let temp := X.xor Y
let newY := temp.xor Y
let newX := temp.xor newY
(newX, newY)
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def SwapBitvectors_postcond (X : UInt8) (Y : UInt8) (result: UInt8 Γ UInt8) (h_precond : SwapBitvectors_precond (X) (Y)) :=
-- !benchmark @start postcond
result.fst = Y β§ result.snd = X β§
(X β Y β result.fst β X β§ result.snd β Y)
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem SwapBitvectors_spec_satisfied (X: UInt8) (Y: UInt8) (h_precond : SwapBitvectors_precond (X) (Y)) :
SwapBitvectors_postcond (X) (Y) (SwapBitvectors (X) (Y) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "SwapBitvectors",
"parameters": {
"param_name": [
"X",
"Y"
],
"param_type": [
"UInt8",
"UInt8"
]
},
"return_type": "UInt8 Γ UInt8"
} | {
"upstream": {
"name": "clover",
"link": "https://github.com/ChuyueSun/Clover",
"task_id": "Clover_swap_bitvector",
"student_id": null
}
} | {
"input": [
"{\"X\": 0, \"Y\": 0}",
"{\"X\": 5, \"Y\": 10}",
"{\"X\": 255, \"Y\": 1}",
"{\"X\": 128, \"Y\": 64}",
"{\"X\": 15, \"Y\": 15}"
],
"expected": [
[
"(0, 0)"
],
[
"(10, 5)"
],
[
"(1, 255)"
],
[
"(64, 128)"
],
[
"(15, 15)"
]
],
"unexpected": [
[
"(0, 1)",
"(1, 0)"
],
[
"(5, 10)",
"(10, 10)",
"(5, 5)"
],
[
"(255, 1)",
"(1, 254)",
"(0, 255)"
],
[
"(128, 64)",
"(64, 64)",
"(0, 128)"
],
[
"(15, 16)",
"(16, 15)",
"(14, 15)"
]
]
} | {
"input": []
} | basic |
verina_advanced_7 | -----Description-----
This task requires writing a Lean 4 function that converts a binary number represented as a list of digits (0 or 1) into its corresponding decimal value. The list is ordered in big-endian format, meaning the most significant digit comes first.
The function should interpret the list as a binary number and return its decimal representation as a natural number.
-----Input-----
The input is a list of natural numbers:
digits: A list of digits, each of which is either 0 or 1, representing a binary number in big-endian order.
-----Output-----
The output is a natural number:
Returns the decimal value of the binary number represented by the input list.
| -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible]
def binaryToDecimal_precond (digits : List Nat) : Prop :=
-- !benchmark @start precond
digits.all (fun d => d = 0 β¨ d = 1)
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def binaryToDecimal (digits : List Nat) (h_precond : binaryToDecimal_precond (digits)) : Nat :=
-- !benchmark @start code
let rec helper (digits : List Nat) : Nat :=
match digits with
| [] => 0
| first :: rest => first * Nat.pow 2 rest.length + helper rest
helper digits
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible]
def binaryToDecimal_postcond (digits : List Nat) (result: Nat) (h_precond : binaryToDecimal_precond (digits)) : Prop :=
-- !benchmark @start postcond
result - List.foldl (Ξ» acc bit => acc * 2 + bit) 0 digits = 0 β§
List.foldl (Ξ» acc bit => acc * 2 + bit) 0 digits - result = 0
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem binaryToDecimal_spec_satisfied (digits: List Nat) (h_precond : binaryToDecimal_precond (digits)) :
binaryToDecimal_postcond (digits) (binaryToDecimal (digits) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "binaryToDecimal",
"parameters": {
"param_name": [
"digits"
],
"param_type": [
"List Nat"
]
},
"return_type": "Nat"
} | {
"upstream": {
"name": "lab_assignment",
"link": "https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/description/",
"task_id": "lab_binaryToDecimal_325751702",
"student_id": [
5
]
}
} | {
"input": [
"{\"digits\": \"[1, 0, 1]\"}",
"{\"digits\": \"[1, 1, 1, 1]\"}",
"{\"digits\": \"[0, 0, 0]\"}",
"{\"digits\": \"[1, 0, 0, 0, 0]\"}",
"{\"digits\": \"[]\"}",
"{\"digits\": \"[1]\"}"
],
"expected": [
[
"5"
],
[
"15"
],
[
"0"
],
[
"16"
],
[
"0"
],
[
"1"
]
],
"unexpected": [
[
"3",
"4",
"6"
],
[
"14",
"16"
],
[
"1",
"2"
],
[
"8",
"0"
],
[
"1"
],
[
"0"
]
]
} | {
"input": [
"{'digits': '[2]'}"
]
} | advanced |
verina_advanced_54 | -----Description-----
This task requires writing a Lean 4 function that finds the one missing number in a list of distinct natural numbers from 0 to n. The list contains exactly n numbers and all numbers are in the range [0, n], but one number in that range is missing.
Your function must return the missing number. You may assume the input list contains no duplicates and only one number is missing.
-----Input-----
- nums: A list of natural numbers of length n, each in the range [0, n] with exactly one number missing.
-----Output-----
- A natural number: the missing number in the range [0, n] not present in the list.
| -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible]
def missingNumber_precond (nums : List Nat) : Prop :=
-- !benchmark @start precond
nums.all (fun x => x β€ nums.length) β§ List.Nodup nums
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def missingNumber (nums : List Nat) (h_precond : missingNumber_precond (nums)) : Nat :=
-- !benchmark @start code
let n := nums.length
let expectedSum := (n * (n + 1)) / 2
let actualSum := nums.foldl (Β· + Β·) 0
expectedSum - actualSum
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible]
def missingNumber_postcond (nums : List Nat) (result: Nat) (h_precond : missingNumber_precond (nums)) : Prop :=
-- !benchmark @start postcond
let n := nums.length
(result β List.range (n + 1)) β§
Β¬(result β nums) β§
β x, (x β List.range (n + 1)) β x β result β x β nums
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem missingNumber_spec_satisfied (nums: List Nat) (h_precond : missingNumber_precond (nums)) :
missingNumber_postcond (nums) (missingNumber (nums) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "missingNumber",
"parameters": {
"param_name": [
"nums"
],
"param_type": [
"List Nat"
]
},
"return_type": "Nat"
} | {
"upstream": {
"name": "lab_assignment",
"link": "https://leetcode.com/problems/missing-number/description/",
"task_id": "lab_missingNumber_324976035",
"student_id": [
22
]
}
} | {
"input": [
"{\"nums\": \"[3, 0, 1]\"}",
"{\"nums\": \"[0, 1]\"}",
"{\"nums\": \"[9, 6, 4, 2, 3, 5, 7, 0, 1]\"}",
"{\"nums\": \"[0]\"}",
"{\"nums\": \"[1]\"}"
],
"expected": [
[
"2"
],
[
"2"
],
[
"8"
],
[
"1"
],
[
"0"
]
],
"unexpected": [
[
"0",
"1",
"3"
],
[
"0",
"1"
],
[
"1",
"9"
],
[
"0"
],
[
"1"
]
]
} | {
"input": [
"{'nums': '[0, 0, 1]'}"
]
} | advanced |
verina_basic_22 | -----Description-----
This task requires writing a Lean 4 method that identifies the dissimilar elements between two arrays of integers. In other words, the method should return an array containing all elements that appear in one input array but not in the other. The output array must contain no duplicate elements and the order of elements does not matter.
-----Input-----
The input consists of:
a: An array of integers.
b: An array of integers.
-----Output-----
The output is an array of integers:
Returns an array containing all distinct elements from both input arrays that are not present in the other array and should be sorted | -- !benchmark @start import type=solution
import Std.Data.HashSet
-- !benchmark @end import
-- !benchmark @start solution_aux
def inArray (a : Array Int) (x : Int) : Bool :=
a.any (fun y => y = x)
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def dissimilarElements_precond (a : Array Int) (b : Array Int) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def dissimilarElements (a : Array Int) (b : Array Int) (h_precond : dissimilarElements_precond (a) (b)) : Array Int :=
-- !benchmark @start code
let res := a.foldl (fun acc x => if !inArray b x then acc.insert x else acc) Std.HashSet.empty
let res := b.foldl (fun acc x => if !inArray a x then acc.insert x else acc) res
res.toArray.insertionSort
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def dissimilarElements_postcond (a : Array Int) (b : Array Int) (result: Array Int) (h_precond : dissimilarElements_precond (a) (b)) :=
-- !benchmark @start postcond
result.all (fun x => inArray a x β inArray b x)β§
result.toList.Pairwise (Β· β€ Β·) β§
a.all (fun x => if x β b then x β result else x β result) β§
b.all (fun x => if x β a then x β result else x β result)
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem dissimilarElements_spec_satisfied (a: Array Int) (b: Array Int) (h_precond : dissimilarElements_precond (a) (b)) :
dissimilarElements_postcond (a) (b) (dissimilarElements (a) (b) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "dissimilarElements",
"parameters": {
"param_name": [
"a",
"b"
],
"param_type": [
"Array Int",
"Array Int"
]
},
"return_type": "Array Int"
} | {
"upstream": {
"name": "dafny-synthesis",
"link": "https://github.com/Mondego/dafny-synthesis",
"task_id": "task_id_579",
"student_id": null
}
} | {
"input": [
"{\"a\": \"#[1, 2, 3, 4]\", \"b\": \"#[3, 4, 5, 6]\"}",
"{\"a\": \"#[1, 1, 2]\", \"b\": \"#[2, 3]\"}",
"{\"a\": \"#[]\", \"b\": \"#[4, 5]\"}",
"{\"a\": \"#[7, 8]\", \"b\": \"#[]\"}",
"{\"a\": \"#[1, 2, 3]\", \"b\": \"#[1, 2, 3]\"}",
"{\"a\": \"#[1, 2, 3]\", \"b\": \"#[4, 5, 6]\"}",
"{\"a\": \"#[-1, 0, 1]\", \"b\": \"#[0]\"}"
],
"expected": [
[
"#[1, 2, 5, 6]"
],
[
"#[1, 3]"
],
[
"#[4, 5]"
],
[
"#[7, 8]"
],
[
"#[]"
],
[
"#[1, 2, 3, 4, 5, 6]"
],
[
"#[-1, 1]"
]
],
"unexpected": [
[
"#[1,2,3,4,5,6]",
"#[3,4]",
"#[1,3,5]"
],
[
"#[1]",
"#[3]",
"#[1,2,3]"
],
[
"#[4]",
"#[5]",
"#[]"
],
[
"#[7]",
"#[8]",
"#[7, 8, 9]"
],
[
"#[1]",
"#[1,2]",
"#[1,2,3]"
],
[
"#[1,2,3,4]",
"#[4,5,6]",
"#[1,2,3]"
],
[
"#[0]",
"#[-1,0,1]",
"#[-1]"
]
]
} | {
"input": []
} | basic |
verina_advanced_66 | -----Description-----
Given an input string "words_str", this task requires writing a Lean 4 function that reverses the order of its words. A word is defined as a contiguous sequence of non-space characters. The function must remove any extra spaces so that the output string contains words separated by a single space and has no leading or trailing spaces. The characters within each word must stay the same as the original input.
-----Input-----
words_str: A string that may contain leading, trailing, or multiple spaces between words.
-----Output-----
A string with the words from the input reversed, where words are separated by a single space, with no extra spaces at the beginning or end.
| -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible]
def reverseWords_precond (words_str : String) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def reverseWords (words_str : String) (h_precond : reverseWords_precond (words_str)) : String :=
-- !benchmark @start code
let rawWords : List String := words_str.splitOn " "
let rec filterNonEmpty (words : List String) : List String :=
match words with
| [] => []
| h :: t =>
if h = "" then
filterNonEmpty t
else
h :: filterNonEmpty t
let filteredWords : List String := filterNonEmpty rawWords
let revWords : List String := filteredWords.reverse
let rec joinWithSpace (words : List String) : String :=
match words with
| [] => ""
| [w] => w
| h :: t =>
-- Append the current word with a space and continue joining the rest.
h ++ " " ++ joinWithSpace t
let result : String := joinWithSpace revWords
result
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible]
def reverseWords_postcond (words_str : String) (result: String) (h_precond : reverseWords_precond (words_str)) : Prop :=
-- !benchmark @start postcond
β words : List String,
(words = (words_str.splitOn " ").filter (fun w => w β "")) β§
result = String.intercalate " " (words.reverse)
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem reverseWords_spec_satisfied (words_str: String) (h_precond : reverseWords_precond (words_str)) :
reverseWords_postcond (words_str) (reverseWords (words_str) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "reverseWords",
"parameters": {
"param_name": [
"words_str"
],
"param_type": [
"String"
]
},
"return_type": "String"
} | {
"upstream": {
"name": "lab_assignment",
"link": "https://leetcode.com/problems/reverse-words-in-a-string/description/",
"task_id": "lab_reverseWords_324895224",
"student_id": [
48
]
}
} | {
"input": [
"{\"words_str\": \"the sky is blue\"}",
"{\"words_str\": \" hello world \"}",
"{\"words_str\": \"a good example\"}",
"{\"words_str\": \" Bob Loves Alice \"}",
"{\"words_str\": \"this lab is interesting\"}"
],
"expected": [
[
"blue is sky the"
],
[
"world hello"
],
[
"example good a"
],
[
"Alice Loves Bob"
],
[
"interesting is lab this"
]
],
"unexpected": [
[
"the sky is blue",
"sky the blue is"
],
[
"hello world",
"worldhello"
],
[
"a good example",
"example a good"
],
[
"Bob Loves Alice",
"Alice Loves Bob "
],
[
"gnitseretni si bal siht"
]
]
} | {
"input": []
} | advanced |
verina_basic_69 | -----Description-----
This problem involves determining the index of the first occurrence of a specified element within an array of integers. The objective is to identify the correct position where the target element appears for the first time, ensuring that all elements prior to that index are different from the target.
-----Input-----
The input consists of:
β’ a: An array of integers.
β’ e: An integer representing the element to search for.
-----Output-----
The output is a natural number (Nat) representing the index of the first occurrence of e in the array.
β’ If the element e exists in the array, the index n will satisfy the conditions specified above.
-----Note-----
It is assumed that the input satisfies the precondition where at least one index i in a exists such that a[i]! = e.
The implementation uses a helper function to iterate through the array recursively. | -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def LinearSearch_precond (a : Array Int) (e : Int) : Prop :=
-- !benchmark @start precond
β i, i < a.size β§ a[i]! = e
-- !benchmark @end precond
-- !benchmark @start code_aux
def linearSearchAux (a : Array Int) (e : Int) (n : Nat) : Nat :=
if n < a.size then
if a[n]! = e then n else linearSearchAux a e (n + 1)
else
0
-- !benchmark @end code_aux
def LinearSearch (a : Array Int) (e : Int) (h_precond : LinearSearch_precond (a) (e)) : Nat :=
-- !benchmark @start code
linearSearchAux a e 0
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def LinearSearch_postcond (a : Array Int) (e : Int) (result: Nat) (h_precond : LinearSearch_precond (a) (e)) :=
-- !benchmark @start postcond
(result < a.size) β§ (a[result]! = e) β§ (β k : Nat, k < result β a[k]! β e)
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem LinearSearch_spec_satisfied (a: Array Int) (e: Int) (h_precond : LinearSearch_precond (a) (e)) :
LinearSearch_postcond (a) (e) (LinearSearch (a) (e) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "LinearSearch",
"parameters": {
"param_name": [
"a",
"e"
],
"param_type": [
"Array Int",
"Int"
]
},
"return_type": "Nat"
} | {
"upstream": {
"name": "clover",
"link": "https://github.com/ChuyueSun/Clover",
"task_id": "Clover_linear_search2",
"student_id": null
}
} | {
"input": [
"{\"a\": \"#[1, 2, 3, 4, 5]\", \"e\": 3}",
"{\"a\": \"#[10, 20, 30, 40, 50]\", \"e\": 10}",
"{\"a\": \"#[5, 4, 3, 2, 1]\", \"e\": 1}",
"{\"a\": \"#[-1, 0, 1, 2]\", \"e\": -1}",
"{\"a\": \"#[7, 8, 7, 9, 7]\", \"e\": 7}"
],
"expected": [
[
"2"
],
[
"0"
],
[
"4"
],
[
"0"
],
[
"0"
]
],
"unexpected": [
[
"0",
"1",
"3"
],
[
"1",
"2",
"3"
],
[
"0",
"1",
"2"
],
[
"1",
"2",
"3"
],
[
"2",
"3",
"4"
]
]
} | {
"input": [
"{'a': '#[1, 2, 3, 4, 5]', 'e': 6}"
]
} | basic |
verina_basic_7 | -----Description-----
This task requires writing a Lean 4 method that computes the sum of the squares of the first n odd natural numbers. The result should match the formula: (n * (2 * n - 1) * (2 * n + 1)) / 3.
-----Input-----
The input consists of:
n: A natural number representing the count of odd natural numbers to consider (n should be non-negative).
-----Output-----
The output is a natural number:
Returns the sum of the squares of the first n odd natural numbers, as defined by the formula: (n * (2 * n - 1) * (2 * n + 1)) / 3. | -- !benchmark @start import type=solution
-- !benchmark @end import
-- !benchmark @start solution_aux
-- !benchmark @end solution_aux
-- !benchmark @start precond_aux
-- !benchmark @end precond_aux
@[reducible, simp]
def sumOfSquaresOfFirstNOddNumbers_precond (n : Nat) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond
-- !benchmark @start code_aux
-- !benchmark @end code_aux
def sumOfSquaresOfFirstNOddNumbers (n : Nat) (h_precond : sumOfSquaresOfFirstNOddNumbers_precond (n)) : Nat :=
-- !benchmark @start code
let rec loop (k : Nat) (sum : Nat) : Nat :=
if k = 0 then
sum
else
loop (k - 1) (sum + (2 * k - 1) * (2 * k - 1))
loop n 0
-- !benchmark @end code
-- !benchmark @start postcond_aux
-- !benchmark @end postcond_aux
@[reducible, simp]
def sumOfSquaresOfFirstNOddNumbers_postcond (n : Nat) (result: Nat) (h_precond : sumOfSquaresOfFirstNOddNumbers_precond (n)) :=
-- !benchmark @start postcond
result - (n * (2 * n - 1) * (2 * n + 1)) / 3 = 0 β§
(n * (2 * n - 1) * (2 * n + 1)) / 3 - result = 0
-- !benchmark @end postcond
-- !benchmark @start proof_aux
-- !benchmark @end proof_aux
theorem sumOfSquaresOfFirstNOddNumbers_spec_satisfied (n: Nat) (h_precond : sumOfSquaresOfFirstNOddNumbers_precond (n)) :
sumOfSquaresOfFirstNOddNumbers_postcond (n) (sumOfSquaresOfFirstNOddNumbers (n) h_precond) h_precond := by
-- !benchmark @start proof
sorry
-- !benchmark @end proof
| {
"name": "sumOfSquaresOfFirstNOddNumbers",
"parameters": {
"param_name": [
"n"
],
"param_type": [
"Nat"
]
},
"return_type": "Nat"
} | {
"upstream": {
"name": "dafny-synthesis",
"link": "https://github.com/Mondego/dafny-synthesis",
"task_id": "task_id_267",
"student_id": null
}
} | {
"input": [
"{\"n\": 0}",
"{\"n\": 1}",
"{\"n\": 2}",
"{\"n\": 3}",
"{\"n\": 4}",
"{\"n\": 5}",
"{\"n\": 10}"
],
"expected": [
[
"0"
],
[
"1"
],
[
"10"
],
[
"35"
],
[
"84"
],
[
"165"
],
[
"1330"
]
],
"unexpected": [
[
"1",
"2"
],
[
"0",
"2",
"3"
],
[
"9",
"11",
"12"
],
[
"30",
"34",
"36"
],
[
"80",
"85",
"90"
],
[
"160",
"166",
"170"
],
[
"1320",
"1331",
"1340"
]
]
} | {
"input": []
} | basic |
End of preview. Expand
in Data Studio
Verina: Benchmarking Verifiable Code Generation
Check out GitHub for more information.
- Downloads last month
- 140