Assume that Rust is given a sorted integer vector with no duplication.
Are there any pairs of integers with the same absolute value?I would like to write a function called .
Let's call it the is_tautology function.
If you write without worrying about efficiency in ruby, it looks like the following.
defis_tautology(clause)
return claim.any?{|literal|close.include?(-literal)}
end
v=
[
[1,2,3],
[-2,-1,1,3],
[-4,-3,-1,2],
[-10,-5,1,2,3,4,5,6,7]
]
v.each {|close|
print("#{clause}#{is_tautology(clause)}\n")
}
When I write in C++ for efficiency, it looks like this
#include<iostream>
# include <vector>
bool is_tautology (std::vector<int>&clause)
{
std::vector<int>::const_iterator begin, end;
begin=clause.begin();
end = close.end();
if(begin==end)
{
return false;
}
--end;
while (*begin<0&&*end>0)
{
if (-*begin<*end)
{
--end;
}
else if (-*begin>*end)
{
++ begin;
}
else
{
return true;
}
}
return false;
}
int main()
{
std::vector<std::vector<int>>v=
{
{1,2,3},
{-2,-1,1,3},
{-4,-3,-1,2},
{-10,-5,1,2,3,4,5,6,7}
};
for (autoclause:v)
{
std::cout<<is_tautology(clause)<<std::endl;
}
}
I tried to write an efficient is_tautology function in Rust, but
Prepare an iterator, take the next value, and judge None...
I tried to do it, but it was going to be very complicated and I stopped working.
Is it possible to write the is_tautology function in a neat way with Rust?
trust
I tried to write an efficient is_tautology function in Rust, but
Prepare an iterator, take the next value, and judge None...
I tried to do it, but it was going to be very complicated and I stopped working.
Is it possible to write the is_tautology function in a neat way with Rust?
When I tried to do so, it turned out to be as follows.Personally, I think I was able to write it in a neat enough way, but what do you think?
fnis_tautology(clause:&[i32])->bool{
use std::cmp::Ordering;
let mut begin_iter=close.iter();
let mut end_iter=close.iter().rev();
let mut begin = begin_iter.next();
let mut end = end_iter.next();
if begin == end {
return false;
}
whilelet (Some(&x), Some(&y))=(begin,end){
match(-x).cmp(&y){
Ordering::Equal=>return true,
Ordering::Less=>end=end_iter.next(),
Ordering::Greater=>begin=begin_iter.next(),
}
}
false
}
US>fnmain(){
letv = [
vec! [1,2,3],
vec! [-2, -1, 1, 3],
vec! [-4,-3, -1,2],
vec! [-10,-5,1,2,3,4,5,6,7],
];
v.into_iter().for_each(|close|{
println!("{:?}{}, cause, is_tautology(&clause));
});
}
We added the if
expression because the part corresponding to while(*begin<0&*end>0)
of the C++ code was leaked.
fnis_tautology(clause:&[i32])->bool{
use std::cmp::Ordering;
let mut begin_iter=close.iter();
let mut end_iter=close.iter().rev();
let mut begin = begin_iter.next();
let mut end = end_iter.next();
if begin == end {
return false;
}
whilelet (Some(&x), Some(&y))=(begin,end){
if x>=0||y<=0{
break;
}
match(-x).cmp(&y){
Ordering::Equal=>return true,
Ordering::Less=>end=end_iter.next(),
Ordering::Greater=>begin=begin_iter.next(),
}
}
false
}
How to use HashSet
.
use std::collections::HashSet;
fnis_tautology(v:&Vec<i32>)->bool{
if v.is_empty() {return false};
let uniq —HashSet<i32>=v.into_iter().map(|i|i.abs()) .collect();
return v.len()!=uniq.len();
}
© 2024 OneMinuteCode. All rights reserved.