Datasets:
Tasks:
Text Generation
Modalities:
Text
Formats:
json
Languages:
English
Size:
< 1K
ArXiv:
Tags:
competitive-programming
code-generation
specification-generation
proof-generation
verification
rust
License:
Download leetcode/lc1664/code_spec.rs from Gax-c/VeriContest_Lite: direct link, hf CLI and curl.
- Browser
- Download file 2.62 kB
-
https://huggingface.co/datasets/Gax-c/VeriContest_Lite/resolve/main/leetcode/lc1664/code_spec.rs
- Command line
-
hf download hf://datasets/Gax-c/VeriContest_Lite/leetcode/lc1664/code_spec.rs
-
curl -L -o code_spec.rs https://huggingface.co/datasets/Gax-c/VeriContest_Lite/resolve/main/leetcode/lc1664/code_spec.rs
2.62 kB
| use vstd::prelude::*; | |
| fn main() {} | |
| verus! { | |
| pub struct Solution; | |
| pub open spec fn sum_even(s: Seq<i32>, start: int, end: int) -> int | |
| decreases end - start, | |
| { | |
| if start >= end { | |
| 0 | |
| } else if start % 2 == 0 { | |
| s[start] as int + sum_even(s, start + 1, end) | |
| } else { | |
| sum_even(s, start + 1, end) | |
| } | |
| } | |
| pub open spec fn sum_odd(s: Seq<i32>, start: int, end: int) -> int | |
| decreases end - start, | |
| { | |
| if start >= end { | |
| 0 | |
| } else if start % 2 == 1 { | |
| s[start] as int + sum_odd(s, start + 1, end) | |
| } else { | |
| sum_odd(s, start + 1, end) | |
| } | |
| } | |
| pub open spec fn even_sum_after_removal(s: Seq<i32>, i: int) -> int { | |
| sum_even(s, 0, i) + sum_odd(s, i + 1, s.len() as int) | |
| } | |
| pub open spec fn odd_sum_after_removal(s: Seq<i32>, i: int) -> int { | |
| sum_odd(s, 0, i) + sum_even(s, i + 1, s.len() as int) | |
| } | |
| pub open spec fn is_fair_after_removal(s: Seq<i32>, i: int) -> bool { | |
| even_sum_after_removal(s, i) == odd_sum_after_removal(s, i) | |
| } | |
| pub open spec fn count_fair(s: Seq<i32>, start: int, end: int) -> int | |
| decreases end - start, | |
| { | |
| if start >= end { | |
| 0 | |
| } else if is_fair_after_removal(s, start) { | |
| 1 + count_fair(s, start + 1, end) | |
| } else { | |
| count_fair(s, start + 1, end) | |
| } | |
| } | |
| impl Solution { | |
| pub fn ways_to_make_fair(nums: Vec<i32>) -> (res: i32) | |
| requires | |
| 1 <= nums.len() <= 100_000, | |
| forall |i: int| 0 <= i < nums.len() ==> 1 <= nums[i] <= 10_000, | |
| ensures | |
| res == count_fair(nums@, 0, nums@.len() as int), | |
| { | |
| let n = nums.len(); | |
| let mut right_even: i64 = 0; | |
| let mut right_odd: i64 = 0; | |
| let mut i: usize = 0; | |
| while i < n { | |
| if i % 2 == 0 { | |
| right_even = right_even + nums[i] as i64; | |
| } else { | |
| right_odd = right_odd + nums[i] as i64; | |
| } | |
| i = i + 1; | |
| } | |
| let mut left_even: i64 = 0; | |
| let mut left_odd: i64 = 0; | |
| let mut result: i32 = 0; | |
| i = 0; | |
| while i < n { | |
| if i % 2 == 0 { | |
| right_even = right_even - nums[i] as i64; | |
| } else { | |
| right_odd = right_odd - nums[i] as i64; | |
| } | |
| if left_even + right_odd == left_odd + right_even { | |
| result = result + 1; | |
| } | |
| if i % 2 == 0 { | |
| left_even = left_even + nums[i] as i64; | |
| } else { | |
| left_odd = left_odd + nums[i] as i64; | |
| } | |
| i = i + 1; | |
| } | |
| result | |
| } | |
| } | |
| } | |