Datasets:
Download level_3/11.rs from CodeWalk/verus-proof-completion: direct link, hf CLI and curl.
- Browser
- Download file 2.72 kB
-
https://huggingface.co/datasets/CodeWalk/verus-proof-completion/resolve/main/level_3/11.rs
- Command line
-
hf download hf://datasets/CodeWalk/verus-proof-completion/level_3/11.rs
-
curl -L -o 11.rs https://huggingface.co/datasets/CodeWalk/verus-proof-completion/resolve/main/level_3/11.rs
2.72 kB
| use vstd::prelude::*; | |
| fn main() {} | |
| verus!{ | |
| fn f1_remove_kth_element(list: &Vec<i32>, k: usize) -> (new_list: Vec<i32>) | |
| requires | |
| list.len() > 0, | |
| 0 < k < list@.len(), | |
| { | |
| let mut new_list = Vec::new(); | |
| let mut index = 0; | |
| while index < (k - 1) | |
| { | |
| new_list.push(list[index]); | |
| index += 1; | |
| } | |
| let mut index = k; | |
| while index < list.len() | |
| { | |
| new_list.push(list[index]); | |
| index += 1; | |
| } | |
| new_list | |
| } | |
| fn f2_find_negative_numbers(arr: &Vec<i32>) -> (negative_list: Vec<i32>) | |
| { | |
| let mut negative_list: Vec<i32> = Vec::new(); | |
| let input_len = arr.len(); | |
| let mut index = 0; | |
| while index < arr.len() | |
| { | |
| if (arr[index] < 0) { | |
| negative_list.push(arr[index]); | |
| } | |
| index += 1; | |
| } | |
| negative_list | |
| } | |
| pub open spec fn count_frequency_rcr(seq: Seq<i32>, key: i32) -> int | |
| decreases seq.len(), | |
| { | |
| if seq.len() == 0 { | |
| 0 | |
| } else { | |
| count_frequency_rcr(seq.drop_last(), key) + if (seq.last() == key) { | |
| 1 as int | |
| } else { | |
| 0 as int | |
| } | |
| } | |
| } | |
| fn f3_count_frequency(arr: &Vec<i32>, key: i32) -> (frequency: usize) | |
| { | |
| let mut index = 0; | |
| let mut counter = 0; | |
| while index < arr.len() | |
| { | |
| if (arr[index] == key) { | |
| counter += 1; | |
| } | |
| index += 1; | |
| } | |
| counter | |
| } | |
| fn f3_remove_duplicates(arr: &Vec<i32>) -> (unique_arr: Vec<i32>) | |
| { | |
| let mut unique_arr: Vec<i32> = Vec::new(); | |
| let input_len = arr.len(); | |
| let mut index = 0; | |
| while index < arr.len() | |
| { | |
| if f3_count_frequency(&arr, arr[index]) == 1 { | |
| unique_arr.push(arr[index]); | |
| } | |
| index += 1; | |
| } | |
| unique_arr | |
| } | |
| spec fn step1(__hp0: Seq<i32>, __hp1: usize, outp: Seq<i32>) -> bool { | |
| (outp == __hp0.subrange(0, __hp1 - 1 as int).add( | |
| __hp0.subrange(__hp1 as int, __hp0.len() as int), | |
| )) | |
| } | |
| spec fn step2(inp: Seq<i32>, outp: Seq<i32>) -> bool { | |
| (outp == inp.filter(|x: i32| x < 0)) | |
| } | |
| spec fn step3(inp: Seq<i32>, outp: Seq<i32>) -> bool { | |
| (outp == inp.filter(|x: i32| count_frequency_rcr(inp, x) == 1)) | |
| } | |
| fn chain_driver(list: &Vec<i32>, k: usize) -> (r_final: Vec<i32>) | |
| requires | |
| list.len() > 0, | |
| 0 < k < list@.len(), | |
| ensures | |
| exists|v1: Seq<i32>, v2: Seq<i32>| step1(list@, k, v1) && step2(v1, v2) && step3(v2, r_final@), | |
| { | |
| let x1 = f1_remove_kth_element(list, k); | |
| let x2 = f2_find_negative_numbers(&x1); | |
| let x3 = f3_remove_duplicates(&x2); | |
| proof { | |
| assert(step1(list@, k, x1@)); | |
| assert(step2(x1@, x2@)); | |
| assert(step3(x2@, x3@)); | |
| } | |
| x3 | |
| } | |
| } // verus! | |