Решение на Логически изрази от Виктор Веселинов
Към профила на Виктор Веселинов
Резултати
- 5 точки от тестове
- 0 бонус точки
- 5 точки общо
- 5 успешни тест(а)
- 15 неуспешни тест(а)
Код
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Expr {
Atom(char),
Not(Box<Expr>),
And(Vec<Expr>),
Or(Vec<Expr>),
}
#[derive(Debug, PartialEq, Eq)]
pub enum ParseError {
UnexpectedExpr,
UnexpectedUnaryOp,
UnexpectedBinOp,
UnexpectedParen,
UnexpectedEnd,
}
/// Парсър за прост израз, който не съдържа скоби
pub struct SimpleExprParser {
}
impl SimpleExprParser {
pub fn new() -> SimpleExprParser {
todo!()
}
pub fn push_atom(&mut self, c: char) -> Result<(), ParseError> {
todo!()
}
pub fn push_op(&mut self, op: char) -> Result<(), ParseError> {
todo!()
}
pub fn finish(self) -> Result<Expr, ParseError> {
todo!()
}
}
/// Парсър за пълния израз
pub struct ExprParser {
}
impl ExprParser {
pub fn new() -> ExprParser {
todo!();
}
/// Приема атом.
///
/// `c` ще бъде валиден символ за атом.
/// В противен случай можете да panic-нете (няма да се тества)
pub fn push_atom(&mut self, c: char) -> Result<(), ParseError> {
todo!()
}
/// Приема символ за операция.
///
/// `op` ще бъде едно от '&', '|', '!'.
/// В противен случай можете да panic-нете (няма да се тества)
pub fn push_op(&mut self, op: char) -> Result<(), ParseError> {
todo!()
}
/// Приема отваряща скоба.
pub fn open_paren(&mut self) -> Result<(), ParseError> {
todo!()
}
/// Приема затваряща скоба.
pub fn close_paren(&mut self) -> Result<(), ParseError> {
todo!()
}
/// Завършва парсването и връща построения израз.
pub fn finish(self) -> Result<Expr, ParseError> {
todo!()
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Value {
True,
False,
Expr(Expr),
}
pub fn eval(expr: &Expr, truthy: &[char], falsy: &[char]) -> Value {
match expr {
Expr::Atom(atom) => {
if truthy.contains(atom) {
Value::True
} else if falsy.contains(atom) {
Value::False
} else {
Value::Expr(expr.clone())
}
}
Expr::Not(sub_expr) => match eval(sub_expr, truthy, falsy) {
Value::True => Value::False,
Value::False => Value::True,
Value::Expr(inner_expr) => Value::Expr(Expr::Not(Box::new(inner_expr))),
},
Expr::And(sub_expr) => {
let mut simplified = Vec::new();
for sub_expr in sub_expr {
match eval(sub_expr, truthy, falsy) {
Value::False => return Value::False, // Ако има False, целият And е False
Value::True => continue, // Пропускаме True
Value::Expr(e) => simplified.push(e),
}
}
if simplified.is_empty() {
Value::True // Ако няма останали елементи, връщаме True
} else if simplified.len() == 1 {
eval(&simplified.pop().unwrap(), truthy, falsy) // Ако е останал само един елемент, опростяваме до него
} else {
Value::Expr(Expr::And(simplified))
}
}
Expr::Or(sub_expr) => {
let mut simplified = Vec::new();
for sub_expr in sub_expr {
match eval(sub_expr, truthy, falsy) {
Value::True => return Value::True, // Ако има True, целият Or е True
Value::False => continue, // Пропускаме False
Value::Expr(e) => simplified.push(e),
}
}
if simplified.is_empty() {
Value::False // Ако няма останали елементи, връщаме False
} else if simplified.len() == 1 {
eval(&simplified.pop().unwrap(), truthy, falsy) // Ако е останал само един елемент, опростяваме до него
} else {
Value::Expr(Expr::Or(simplified))
}
}
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20241224-258381-1li00cf/solution) warning: unused variable: `c` --> src/lib.rs:27:33 | 27 | pub fn push_atom(&mut self, c: char) -> Result<(), ParseError> { | ^ help: if this is intentional, prefix it with an underscore: `_c` | = note: `#[warn(unused_variables)]` on by default warning: unused variable: `op` --> src/lib.rs:31:31 | 31 | pub fn push_op(&mut self, op: char) -> Result<(), ParseError> { | ^^ help: if this is intentional, prefix it with an underscore: `_op` warning: unused variable: `c` --> src/lib.rs:54:33 | 54 | pub fn push_atom(&mut self, c: char) -> Result<(), ParseError> { | ^ help: if this is intentional, prefix it with an underscore: `_c` warning: unused variable: `op` --> src/lib.rs:62:31 | 62 | pub fn push_op(&mut self, op: char) -> Result<(), ParseError> { | ^^ help: if this is intentional, prefix it with an underscore: `_op` warning: `solution` (lib) generated 4 warnings warning: variable does not need to be mutable --> tests/solution_test.rs:306:13 | 306 | let mut parser = SimpleExprParser::new(); | ----^^^^^^ | | | help: remove this `mut` | = note: `#[warn(unused_mut)]` on by default warning: `solution` (test "solution_test") generated 1 warning Finished test [unoptimized + debuginfo] target(s) in 1.65s Running tests/solution_test.rs (target/debug/deps/solution_test-1428e1090729d165) running 20 tests test solution_test::test_eval_full ... ok test solution_test::test_error_paren_mismatched ... FAILED test solution_test::test_eval_not_and_or ... ok test solution_test::test_eval_partial ... ok test solution_test::test_eval_unwrap_and_or ... ok test solution_test::test_eval_unwrap_nested ... ok test solution_test::test_paren_around_expr ... FAILED test solution_test::test_paren_expr_priority ... FAILED test solution_test::test_paren_nested ... FAILED test solution_test::test_paren_not ... FAILED test solution_test::test_paren_surrounded ... FAILED test solution_test::test_parser_alternating_ops ... FAILED test solution_test::test_parser_and_or ... FAILED test solution_test::test_parser_atom ... FAILED test solution_test::test_parser_error_unexpected_end ... FAILED test solution_test::test_parser_errors_basic ... FAILED test solution_test::test_parser_expr_and_not ... FAILED test solution_test::test_parser_multiple_atoms_same_op ... FAILED test solution_test::test_parser_multiple_not ... FAILED test solution_test::test_parser_not ... FAILED failures: ---- solution_test::test_error_paren_mismatched stdout ---- thread '<unnamed>' panicked at 'not yet implemented', src/lib.rs:47:9 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace thread 'solution_test::test_error_paren_mismatched' panicked at 'called `Option::unwrap()` on a `None` value', tests/solution_test.rs:325:5 ---- solution_test::test_paren_around_expr stdout ---- thread '<unnamed>' panicked at 'not yet implemented', src/lib.rs:47:9 thread 'solution_test::test_paren_around_expr' panicked at 'called `Option::unwrap()` on a `None` value', tests/solution_test.rs:176:5 ---- solution_test::test_paren_expr_priority stdout ---- thread '<unnamed>' panicked at 'not yet implemented', src/lib.rs:47:9 thread 'solution_test::test_paren_expr_priority' panicked at 'called `Option::unwrap()` on a `None` value', tests/solution_test.rs:197:5 ---- solution_test::test_paren_nested stdout ---- thread '<unnamed>' panicked at 'not yet implemented', src/lib.rs:47:9 thread 'solution_test::test_paren_nested' panicked at 'called `Option::unwrap()` on a `None` value', tests/solution_test.rs:252:5 ---- solution_test::test_paren_not stdout ---- thread '<unnamed>' panicked at 'not yet implemented', src/lib.rs:47:9 thread 'solution_test::test_paren_not' panicked at 'called `Option::unwrap()` on a `None` value', tests/solution_test.rs:216:5 ---- solution_test::test_paren_surrounded stdout ---- thread '<unnamed>' panicked at 'not yet implemented', src/lib.rs:47:9 thread 'solution_test::test_paren_surrounded' panicked at 'called `Option::unwrap()` on a `None` value', tests/solution_test.rs:235:5 ---- solution_test::test_parser_alternating_ops stdout ---- thread '<unnamed>' panicked at 'not yet implemented', src/lib.rs:24:9 thread 'solution_test::test_parser_alternating_ops' panicked at 'called `Option::unwrap()` on a `None` value', tests/solution_test.rs:157:5 ---- solution_test::test_parser_and_or stdout ---- thread '<unnamed>' panicked at 'not yet implemented', src/lib.rs:24:9 thread 'solution_test::test_parser_and_or' panicked at 'called `Option::unwrap()` on a `None` value', tests/solution_test.rs:84:5 ---- solution_test::test_parser_atom stdout ---- thread '<unnamed>' panicked at 'not yet implemented', src/lib.rs:24:9 thread 'solution_test::test_parser_atom' panicked at 'called `Option::unwrap()` on a `None` value', tests/solution_test.rs:74:5 ---- solution_test::test_parser_error_unexpected_end stdout ---- thread '<unnamed>' panicked at 'not yet implemented', src/lib.rs:24:9 thread 'solution_test::test_parser_error_unexpected_end' panicked at 'called `Option::unwrap()` on a `None` value', tests/solution_test.rs:305:5 ---- solution_test::test_parser_errors_basic stdout ---- thread '<unnamed>' panicked at 'not yet implemented', src/lib.rs:24:9 thread 'solution_test::test_parser_errors_basic' panicked at 'called `Option::unwrap()` on a `None` value', tests/solution_test.rs:265:5 ---- solution_test::test_parser_expr_and_not stdout ---- thread '<unnamed>' panicked at 'not yet implemented', src/lib.rs:24:9 thread 'solution_test::test_parser_expr_and_not' panicked at 'called `Option::unwrap()` on a `None` value', tests/solution_test.rs:107:5 ---- solution_test::test_parser_multiple_atoms_same_op stdout ---- thread '<unnamed>' panicked at 'not yet implemented', src/lib.rs:24:9 thread 'solution_test::test_parser_multiple_atoms_same_op' panicked at 'called `Option::unwrap()` on a `None` value', tests/solution_test.rs:124:5 ---- solution_test::test_parser_multiple_not stdout ---- thread '<unnamed>' panicked at 'not yet implemented', src/lib.rs:24:9 thread 'solution_test::test_parser_multiple_not' panicked at 'called `Option::unwrap()` on a `None` value', tests/solution_test.rs:140:5 ---- solution_test::test_parser_not stdout ---- thread '<unnamed>' panicked at 'not yet implemented', src/lib.rs:24:9 thread 'solution_test::test_parser_not' panicked at 'called `Option::unwrap()` on a `None` value', tests/solution_test.rs:98:5 failures: solution_test::test_error_paren_mismatched solution_test::test_paren_around_expr solution_test::test_paren_expr_priority solution_test::test_paren_nested solution_test::test_paren_not solution_test::test_paren_surrounded solution_test::test_parser_alternating_ops solution_test::test_parser_and_or solution_test::test_parser_atom solution_test::test_parser_error_unexpected_end solution_test::test_parser_errors_basic solution_test::test_parser_expr_and_not solution_test::test_parser_multiple_atoms_same_op solution_test::test_parser_multiple_not solution_test::test_parser_not test result: FAILED. 5 passed; 15 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s error: test failed, to rerun pass `--test solution_test`