In this challenge, you will implement a finite state automaton (FSA) to recognize a specific pattern in a sequence of characters.
A finite state automaton is a mathematical model of computation used to design both computer programs and sequential logic circuits. It is a powerful tool for pattern matching as it consists of a finite number of states and transitions between these states based on input symbols.
Finite state automatons are widely used in text processing, lexical analysis, and many other areas where pattern recognition is essential.
You need to create an FSA that can recognize the pattern "ab*c", where:
You will implement a function recognize_pattern
that takes a string slice as input and returns a boolean indicating whether the input string matches the pattern.
match
statement.match
statement.Did You Know?
Finite state automatons have a wide range of applications outside computer science as well. For example, they are used in the design of digital circuits. In digital circuit design, an FSA can be used to create sequential circuits such as counters and communication protocol controllers. FSAs are also used in the field of linguistics to model the morphology of languages and in robotics to control the behavior of autonomous robots.
mbergkvist
#[derive(Debug, PartialEq)]enum State { Start, A, B, C,}pub fn recognize_pattern(input: &str) -> bool { let mut state = State::Start; input.chars().all(|c| match (&mut state, c) { (State::Start, 'a') => { state = State::A; true } (State::A | State::B, 'b') => { state = State::B; true } (State::A | State::B, 'c') => { state = State::C; true } _ => false, }) && state == State::C}
jimlawton
#[derive(Debug, PartialEq)]enum States { Init, GotA, GotB, GotC,}fn fsm(state: States, c: char) -> States { println!("{state:?} {c}"); match (state, c) { (States::Init, 'a') => States::GotA, (States::GotA, 'a') => States::GotA, (States::GotA, 'b') => States::GotB, (States::GotA, 'c') => States::GotC, (States::GotB, 'b') => States::GotB, (States::GotB, 'c') => States::GotC, (States::GotC, _) => if c != 'c' { States::GotC } else { States::Init }, (_, _) => States::Init, }}pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here println!("pattern: {input}"); if input.len() > 0 { let mut state = States::Init; for c in input.chars() { state = fsm(state, c); } if state == States::GotC { return true; } } false}
broswen
pub fn recognize_pattern(input: &str) -> bool { let mut matcher = Matcher::new(); matcher.matches(input)}#[derive(Debug, Eq, PartialEq)]enum FSA { Start, A, Bs, CDone, Invalid,}struct Matcher { state: FSA,}impl Matcher { fn new() -> Self { Self { state: FSA::Start, } } fn matches(&mut self, s: &str) -> bool { for c in s.chars() { self.next(c); } self.state == FSA::CDone } fn next(&mut self, c: char) { match self.state { FSA::Start => { match c { 'a' => self.state = FSA::A, _ => self.state = FSA::Start, } }, FSA::A => { match c { 'b' => self.state = FSA::Bs, 'c' => self.state = FSA::CDone, _ => self.state = FSA::Invalid, } }, FSA::Bs => { match c { 'b' => self.state = FSA::Bs, 'c' => self.state = FSA::CDone, _ => self.state = FSA::Invalid, } }, FSA::CDone => { match c { _ => self.state = FSA::Invalid, } }, FSA::Invalid => {} } }}
DivineGod
enum State { Invalid, A, B, C,}pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let final_state = input.chars().fold(State::Invalid, |state, c| { match (state, c) { (_, 'a') => State::A, (State::A, 'b') => State::B, (State::A, 'c') => State::C, (State::B, 'b') => State::B, (State::B, 'c') => State::C, _ => State::Invalid, } }); matches!(final_state, State::C)}
sroas
enum State { Invalid, A, B, C,}pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let final_state = input.chars().fold(State::Invalid, |state, c| { match (state, c) { (_, 'a') => State::A, (State::A, 'b') => State::B, (State::A, 'c') => State::C, (State::B, 'b') => State::B, (State::B, 'c') => State::C, _ => State::Invalid, } }); matches!(final_state, State::C)}
qiyuan711
enum State { S, B, T, Invalid,}pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let mut state = State::S; for x in input.chars() { state = match (state, x) { (State::S, 'a') => State::B, (State::B, 'b') => State::B, (State::B, 'c') => State::T, (_, _) => State::Invalid, }; } match state { State::T => true, _ => false, }}
diegocmsantos
enum State { S, B, T, Invalid,}pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let mut state = State::S; for x in input.chars() { state = match (state, x) { (State::S, 'a') => State::B, (State::B, 'b') => State::B, (State::B, 'c') => State::T, (_, _) => State::Invalid, }; } matches!(state, State::T)}
leemars
enum State { S, B, T, Invalid,}pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let mut state = State::S; for x in input.chars() { state = match (state, x) { (State::S, 'a') => State::B, (State::B, 'b') => State::B, (State::B, 'c') => State::T, (_, _) => State::Invalid, }; } match state { State::T => true, _ => false, }}
tamanishi
pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here println!("{}", input); #[derive(PartialEq)] enum States {S, A, B, C} let mut state = States::S; for c in input.chars() { match state { States::S => {if c == 'a' {state = States::A;} else {return false;}}, States::A => {if c == 'b' {state = States::B;} else if c == 'c' {state = States::C;} else {return false;}}, States::B => {if c == 'c' {state = States::C;} else if c == 'b' {continue;} else {return false;}}, States::C => {return false;}, } } if state == States::C { return true; } false}
Thymelizabeth
pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let mut state = State::A; for c in input.chars() { match state { State::A => if c == 'a' { state = State::B; } else { break; } State::B => if c == 'b' { continue; } else if c == 'c' { state = State::C; } else { break; } State::C => return false, } } state == State::C}#[derive(PartialEq, Eq)]enum State { A, B, C,}
aynugek
enum State { Start, Running, End, Invalid,}pub fn recognize_pattern(input: &str) -> bool { let mut state = State::Start; for c in input.chars() { match (state, c) { (State::Start, 'a') => state = State::Running, (State::Running, 'b') => state = State::Running, (State::Running, 'c') => state = State::End, (_, _) => { state = State::Invalid; break; } } } match state { State::End => true, _ => false, }}
0xsmarter
pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let mut fsa = FSA { state: State::Start, }; for c in input.chars() { match (fsa.state, c) { (State::Start, 'a') => fsa.state = State::A, (State::A, 'b') => fsa.state = State::B, (State::A, 'c') => fsa.state = State::C, (State::B, 'b') => fsa.state = State::B, (State::B, 'c') => fsa.state = State::C, _ => fsa.state = State::Reject, } } if fsa.state == State::C { return true; } false}#[derive(PartialEq)]enum State { Start, A, B, C, Reject,}struct FSA { state: State,}
shinobu52
pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let mut fsa = FSA { state: State::Start, }; for c in input.chars() { match (fsa.state, c) { (State::Start, 'a') => fsa.state = State::A, (State::A, 'b') => fsa.state = State::B, (State::A, 'c') => fsa.state = State::C, (State::B, 'b') => fsa.state = State::B, (State::B, 'c') => fsa.state = State::C, _ => fsa.state = State::Reject, } } if fsa.state == State::C { return true; } false}#[derive(PartialEq)]enum State { Start, A, B, C, Reject,}struct FSA { state: State,}
shinobu52
pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let mut fsa = FSA { state: State::Start, }; for c in input.chars() { match c { 'a' => match fsa.state { State::Start => fsa.state = State::A, _ => fsa.state = State::Reject, }, 'b' => match fsa.state { State::A => fsa.state = State::B, State::B => fsa.state = State::B, _ => fsa.state = State::Reject, }, 'c' => match fsa.state { State::A => fsa.state = State::C, State::B => fsa.state = State::C, _ => fsa.state = State::Reject, }, _ => fsa.state = State::Reject, } } if fsa.state == State::C { return true; } false}#[derive(PartialEq)]enum State { Start, A, B, C, Reject,}struct FSA { state: State,}
tsucchinoko
#[derive(PartialEq)]enum State { Empty, A, ABs, Valid, Invalid,}pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let mut cur = State::Empty; for x in input.chars() { cur = match (cur, x) { (State::Empty, 'a') => State::A, (State::A, 'b') => State::ABs, (State::A, 'c') => State::Valid, (State::ABs, 'b') => State::ABs, (State::ABs, 'c') => State::Valid, _ => State::Invalid, } } cur == State::Valid}
Rolando1994
#[derive(PartialEq)]enum State { Empty, A, ABs, Valid, Invalid}pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let mut cur = State::Empty; for x in input.chars() { cur = match (cur, x) { (State::Empty, 'a') => State::A, (State::A, 'b') => State::ABs, (State::A, 'c') => State::Valid, (State::ABs, 'b') => State::ABs, (State::ABs, 'c') => State::Valid, _ => State::Invalid } } cur == State::Valid}
ankeetparikh
#[derive(PartialEq)]enum State { Empty, A, ABs, Valid, Invalid}pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let mut cur = State::Empty; for x in input.chars() { cur = match (cur, x) { (State::Empty, 'a') => State::A, (State::A, 'b') => State::ABs, (State::A, 'c') => State::Valid, (State::ABs, 'b') => State::ABs, (State::ABs, 'c') => State::Valid, _ => State::Invalid } } cur == State::Valid}
senft
#[derive(PartialEq)]enum State { A, BoC, Valid, Invalid,}pub fn recognize_pattern(input: &str) -> bool { let mut state = State::A; for c in input.chars() { state = match (state, c) { (State::A, 'a') => State::BoC, (State::BoC, 'b') => State::BoC, (State::BoC, 'c') => State::Valid, _ => State::Invalid, } } state == State::Valid}
senft
#[derive(PartialEq)]enum State { Start, A, BoC, End, Invalid,}pub fn recognize_pattern(input: &str) -> bool { let mut state = State::Start; for c in input.chars() { state = match state { State::Start => match c { 'a' => State::A, _ => State::Invalid, }, State::A => match c { 'b' => State::BoC, 'c' => State::End, _ => State::Invalid, }, State::BoC => match c { 'b' => State::BoC, 'c' => State::End, _ => State::Invalid, }, _ => State::Invalid, } } state == State::End}
whitwulf
#[derive(Debug)]enum State { Start, A, B, C, Invalid,}pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let mut state = State::Start; for ch in input.chars() { state = match (state, ch) { (State::Start, 'a') => State::A, (State::A, 'b') => State::B, (State::A, 'c') => State::C, (State::B, 'b') => State::B, (State::B, 'c') => State::C, _ => State::Invalid, } } if let State::Invalid = state { return false; } if let State::C = state { return true } false}
cloki0610
#[derive(PartialEq)]enum State { Init, A, B, C}pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let mut state = State::Init; for c in input.chars() { state = match (state, c) { (State::Init, 'a') => State::A, (State::A | State::B, 'b') => State::B, (State::A | State::B, 'c') => State::C, _ => return false, } } state == State::C}
Bubbet
#[derive(PartialEq)]enum State { Init, A, B, C}pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let mut state = State::Init; for c in input.chars() { state = match (state, c) { (State::Init, 'a') => State::A, (State::A | State::B, 'b') => State::B, (State::A | State::B, 'c') => State::C, _ => return false, } } state == State::C}
maxvi
#[derive(Debug)]enum State { Start, A, B, C, Invalid,}pub fn recognize_pattern(input: &str) -> bool { let mut state = State::Start; for ch in input.chars() { state = match (state, ch) { (State::Start, 'a') => State::A, (State::A, 'b') => State::B, (State::A, 'c') => State::C, (State::B, 'b') => State::B, (State::B, 'c') => State::C, _ => State::Invalid, }; if let State::Invalid = state { return false; } } matches!(state, State::C)}
MichaelScofield111
enum ParseState { ExpectA, ExpectBorC, End}fn recognize_pattern_inner(inp: &mut impl Iterator<Item=char>, state: ParseState) -> bool { match (inp.next(), state) { (Some('a'), ParseState::ExpectA) => recognize_pattern_inner(inp, ParseState::ExpectBorC), (Some('b'), ParseState::ExpectBorC) => recognize_pattern_inner(inp, ParseState::ExpectBorC), (Some('c'), ParseState::ExpectBorC) => recognize_pattern_inner(inp, ParseState::End), (None, ParseState::End) => true, (_x, _y) => false, }}pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here recognize_pattern_inner(&mut input.chars(), ParseState::ExpectA)}
majesticalcreature
type Arg<'a> = &'a str;pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let mut auto = Automaton::new(); input.to_string().chars().for_each(|ch| auto.insertNext(ch.to_string().as_str()) ); auto.accepts()}struct Automaton { state: State}impl Automaton { fn new() -> Self { Automaton{ state: State(ElState::A) } } fn insertNext(&mut self, input: Arg) { self.state.transition(input); } fn accepts(&self) -> bool { match self.state.0 { ElState::S => true, _ => false } }}struct State(ElState);enum ElState { A, B, S, F,}impl State { fn transition(&mut self, arg: Arg) { use ElState::*; self.0 = match self.0 { A => match arg { "a" => B, _ => F }, B => match arg { "b" => B, "c" => S, _ => F }, S => F, F => F }; }}
StimhackSoftware
#[derive(Debug, Clone, PartialEq, Eq)]enum ParseState { ExpectA, ExpectBorC, End}fn recognize_pattern_inner(inp : &mut impl Iterator<Item=char>, state : ParseState) -> bool { match (inp.next(), state) { (Some('a'), ParseState::ExpectA) => recognize_pattern_inner(inp, ParseState::ExpectBorC), (Some('b'), ParseState::ExpectBorC) => recognize_pattern_inner(inp, ParseState::ExpectBorC), (Some('c'), ParseState::ExpectBorC) => recognize_pattern_inner(inp, ParseState::End), (None, ParseState::End) => true, (_x, _y) => false, }}pub fn recognize_pattern(input: &str) -> bool { recognize_pattern_inner(&mut input.chars(), ParseState::ExpectA)}
armed
#[derive(PartialEq)]enum State { Start, A, B, Invalid, End}pub fn recognize_pattern(input: &str) -> bool { let mut state = State::Start; let mut cx = input.chars(); loop { let c = cx.next(); match (c, &state) { (Some('a'), State::Start) => { state = State::A; }, (Some('b'), State::A) | (Some('b'), State::B) => { state = State::B; }, (Some('c'), State::A) | (Some('c'), State::B) => { state = State::End; }, (None, State::End) => break, _ => { state = State::Invalid; break; } } } state == State::End}
miscer
use std::cmp::PartialEq;enum Action { Input(char), End,}#[derive(PartialEq, Debug)]enum State { Start, CharA, CharB, CharC, Valid, Invalid,}fn run_action(state: State, action: Action) -> State { match state { State::Start => match action { Action::Input('a') => State::CharA, _ => State::Invalid, }, State::CharA => match action { Action::Input('b') => State::CharB, Action::Input('c') => State::CharC, _ => State::Invalid, }, State::CharB => match action { Action::Input('b') => State::CharB, Action::Input('c') => State::CharC, _ => State::Invalid, }, State::CharC => match action { Action::End => State::Valid, _ => State::Invalid, }, _ => State::Invalid, }}pub fn recognize_pattern(input: &str) -> bool { let mut state = State::Start; for c in input.chars() { state = run_action(state, Action::Input(c)) }; state = run_action(state, Action::End); state == State::Valid}
cip999
pub enum State { New, A, B, Invalid, Done,}impl State { pub fn new() -> Self { Self::New } pub fn ok(&self) -> bool { match self { &Self::Done => true, _ => false, } } pub fn transition(&mut self, c: char) { *self = match *self { Self::New => self.transition_new(c), Self::A | Self::B => self.transition_a_b(c), Self::Invalid | Self::Done => Self::Invalid, } } fn transition_new(&self, c: char) -> Self { match c { 'a' => Self::A, _ => Self::Invalid, } } fn transition_a_b(&self, c: char) -> Self { match c { 'b' => Self::B, 'c' => Self::Done, _ => Self::Invalid, } }}pub fn recognize_pattern(input: &str) -> bool { let mut state = State::new(); input.chars().for_each(|c| state.transition(c)); state.ok()}
uggla
pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let mut state = State::new(); if input.is_empty() { return false; } for c in input.chars(){ if !c.is_alphabetic(){ return false; } state = state.next_state(c); } if state == State::C{ true }else { false }}#[derive(Eq, PartialEq, Clone, Copy)]enum State { None, A, B, C, Invalid,}impl State{ fn new() -> Self{ State::None } fn next_state(&self, c: char) -> State{ match (c, &self) { ('a', State::None) => State::A, //('a', _) => *self, ('b', State::A) => State::B, ('b', State::B) => State::B, ('c', State::B) => State::C, ('c', State::A) => State::C, (_, _) => State::Invalid, } }}
its-me-sv
#[derive(PartialEq)]enum State { Init, PostA, PostC,}pub fn recognize_pattern(input: &str) -> bool { let mut state = State::Init; for char in input.chars() { state = match (state, char) { (State::Init | State::PostA, 'a' | 'b') => State::PostA, (State::PostA, 'c') => State::PostC, _ => return false, } } state == State::PostC}
its-me-sv
#[derive(PartialEq)]enum State { Init, PostA, PostC,}pub fn recognize_pattern(input: &str) -> bool { let mut state = State::Init; for char in input.chars() { state = match (state, char) { (State::Init | State::PostA, 'a' | 'b') => State::PostA, (State::PostA, 'c') => State::PostC, _ => return false, } } state == State::PostC}
its-me-sv
pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here match !input.starts_with("a") || !input.ends_with("c") { true => false, false => { input[1..input.len()] .chars() .filter(|&ch| ch == 'b') .count() == input.len() - 2 } }}
raneid
enum States { Init, PostA, PostC,}pub fn recognize_pattern(input: &str) -> bool { let mut state = States::Init; for c in input.chars() { state = match (state, c) { (States::Init, 'a') => States::PostA, (States::PostA, 'b') => States::PostA, (States::PostA, 'c') => States::PostC, _ => return false, } } match state { States::PostC => true, _ => false, }}
kawanoyou
pub fn recognize_pattern(input: &str) -> bool { #[derive(PartialEq)] enum State { Start, MatchA, MatchB, MatchC, NoMatch, } let mut state: State = State::Start; // Implement your finite state automaton here for c in input.chars() { state = match (state, c) { (State::Start, 'a') => State::MatchA, (State::MatchA, 'b') => State::MatchB, (State::MatchA, 'c') => State::MatchC, (State::MatchB, 'b') => State::MatchB, (State::MatchB, 'c') => State::MatchC, _ => State::NoMatch, } } return state == State::MatchC;}
ludovicknecht
#[derive(PartialEq)]enum State { Initial, A, B, C}pub fn recognize_pattern(input: &str) -> bool { let mut state = State::Initial; for ch in input.chars() { state = match (state, ch) { (State::Initial, 'a') => State::A, (State::A, 'b') => State::B, (State::A, 'c') => State::C, (State::B, 'b') => State::B, (State::B, 'c') => State::C, _ => return false }; } state == State::C}
mrsalo
use std::error::Error;#[derive(Debug, PartialEq)]enum State { ExpectingA, InBetween, ExpectingNothing,}struct Automaton { state: State}impl Automaton { pub fn new() -> Automaton { Self{state: State::ExpectingA} } pub fn feed(&mut self, c: char) -> Result<(), Box<dyn Error>> { println!("{:?}", self.state); match self.state { State::ExpectingA => { if c != 'a' { Err(format!("Expected a, got: {}", c))?; } self.state = State::InBetween; Ok(()) }, State::InBetween => { if c == 'b' { Ok(()) } else if c == 'c' { self.state = State::ExpectingNothing; Ok(()) } else { Err(format!("Expected b | c, got: {}", c))? } }, State::ExpectingNothing => { Err(format!("Expected nothing, got: {}", c))? } } }}pub fn recognize_pattern(input: &str) -> bool { let mut automaton = Automaton::new(); for c in input.chars() { match automaton.feed(c) { Ok(_) => (), Err(e) => { println!("{:?}", e); return false; } } } automaton.state == State::ExpectingNothing }
saukmn
pub fn recognize_pattern(input: &str) -> bool { enum State { Started, StartsWithA, EndsWithC, Matched, Unmatched } impl State { fn matches(input: &str) -> bool { let state = State::Started.next(input); match state { State::Matched => true, _ => false, } } fn next(self, input: &str) -> State { match self { State::Started if input.starts_with("a") => State::StartsWithA.next(&input[1..]), State::StartsWithA if input.ends_with("c") => State::EndsWithC.next(&input[..input.len() - 1]), State::EndsWithC if input == &"b".repeat(input.len()) => State::Matched, State::Matched | State::Unmatched=> self, _ => State::Unmatched, } } } State::matches(input)}
XiaoPengYouCode
pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here if input.is_empty() { return false; } let mut state = State::Start; for ch in input.chars() { state = transition(state, ch); if state == State::Error { return false; } } // 检查最终状态是否是 C,即模式匹配成功 state == State::C}#[derive(PartialEq)]enum State { Start, A, B, C, Error,}// 定义 FSA 的状态转换函数fn transition(current_state: State, input: char) -> State { match (current_state, input) { (State::Start, 'a') => State::A, (State::A, 'b') => State::B, (State::A, 'c') => State::C, (State::B, 'b') => State::B, (State::B, 'c') => State::C, // 如果输入不符合预期,进入错误状态 _ => State::Error, }}
kalley
pub fn recognize_pattern(input: &str) -> bool { if input.len() < 2 { return false; } let mut chars = input.chars(); let first = chars.next(); let last = chars.next_back(); if first != Some('a') || last != Some('c') { return false; } chars.all(|c| c == 'b')}
querywatson
#[derive(Debug, PartialEq)]enum MatchingState { Start, FoundA, FoundB, MatchFound, NoMatch,}pub fn recognize_pattern(input: &str) -> bool { let mut state = MatchingState::Start; for ch in input.chars() { state = match state { MatchingState::Start => { if ch == 'a' { MatchingState::FoundA } else { MatchingState::NoMatch } } MatchingState::FoundA => match ch { 'b' => MatchingState::FoundB, 'c' => MatchingState::MatchFound, _ => MatchingState::NoMatch, }, MatchingState::FoundB => match ch { 'b' => MatchingState::FoundB, 'c' => MatchingState::MatchFound, _ => MatchingState::NoMatch, }, MatchingState::MatchFound => { MatchingState::NoMatch } MatchingState::NoMatch => { MatchingState::NoMatch } }; if state == MatchingState::NoMatch { return false; } } state == MatchingState::MatchFound}
querywatson
pub fn recognize_pattern(input: &str) -> bool { if input.len() < 2 { return false; } let first_char = input.chars().next(); let last_char = input.chars().last(); if first_char != Some('a') || last_char != Some('c') { return false; } let chars: Vec<char> = input.chars().collect(); if chars.len() == 0 { return true; } for ch in chars[1..chars.len()-1].iter() { if *ch != 'b' { return false; } } true}
querywatson
#[derive(Debug, PartialEq)]enum MatchingState { Start, FoundA, FoundB, MatchFound, NoMatch,}pub fn recognize_pattern(input: &str) -> bool { let mut state = MatchingState::Start; for ch in input.chars() { state = match state { MatchingState::Start => { if ch == 'a' { MatchingState::FoundA } else { MatchingState::NoMatch } } MatchingState::FoundA => match ch { 'b' => MatchingState::FoundB, 'c' => MatchingState::MatchFound, _ => MatchingState::NoMatch, }, MatchingState::FoundB => match ch { 'b' => MatchingState::FoundB, 'c' => MatchingState::MatchFound, _ => MatchingState::NoMatch, }, MatchingState::MatchFound => { MatchingState::NoMatch } MatchingState::NoMatch => { MatchingState::NoMatch } }; if state == MatchingState::NoMatch { return false; } } state == MatchingState::MatchFound}
querywatson
#[derive(PartialEq)]enum MatchingState { Initialized, AFound, BContinue, NoMatch, FoundMatch,}pub fn recognize_pattern(input: &str) -> bool { let mut state = MatchingState::Initialized; for ch in input.chars() { state = match state { MatchingState::Initialized => match ch { 'a' => MatchingState::AFound, _ => MatchingState::NoMatch }, MatchingState::AFound => match ch { 'a' => MatchingState::NoMatch, 'b' => MatchingState::BContinue, 'c' => MatchingState::FoundMatch, _ => MatchingState::NoMatch }, MatchingState::BContinue => match ch { 'a' => MatchingState::NoMatch, 'b' => MatchingState::BContinue, 'c' => MatchingState::FoundMatch, _ => MatchingState::NoMatch }, _ => MatchingState::NoMatch, }; if state == MatchingState::NoMatch { return false; } } if state == MatchingState::FoundMatch { true }else { false }}
alemack17
pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let mut state=State::Start; let mut ret=false; for c in input.chars(){ match c{ 'a'=> if state==State::Start{state=State::FoundA}else{ret=false;break;}, 'b'=> if state!=State::FoundA{ret=false;break;}, 'c'=> if state==State::FoundA{ret=true;state=State::FoundC}else{ret=false;break;}, _ => {ret=false;break;} } } return ret;} #[derive(PartialEq)]enum State{ Start, FoundA, FoundC}
mcgrizzz
enum State { Init, Character(char), Complete, Invalid}pub fn recognize_pattern(input: &str) -> bool { let mut state: State = State::Init; for c in input.chars() { state = match state { State::Init | State::Invalid //If we error state, we should be open to matching later ex: "ab abc" if c == 'a' => State::Character('a'), State::Character(previous) if (previous == 'a' || previous == 'b') && c == 'b' => State::Character(c), State::Character(_) if c == 'c' => State::Complete, _ => State::Invalid } } match state { State::Complete => true, _ => false }}
daves003
pub fn recognize_pattern(input: &str) -> bool { enum State { Initial, GotA, GotB, GotC, Invalid } let mut state = State::Initial; for c in input.chars() { state = match (state, c) { (State::Initial, 'a') => State::GotA, (State::GotA, 'b') => State::GotB, (State::GotA, 'c') => State::GotC, (State::GotB, 'b') => State::GotB, (State::GotB, 'c') => State::GotC, _ => State::Invalid }; } match state { State::GotC => true, _ => false }}
paulosuzart
enum MatchState { Start, Received(char), End, Error,}pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let mut state = MatchState::Start; for current_char in input.chars() { state = match state { MatchState::Start if current_char == 'a' => MatchState::Received('a'), MatchState::Received(prev_char) if (prev_char == 'a' || prev_char == 'b') && current_char == 'b' => { MatchState::Received(current_char) } MatchState::Received(_) if current_char == 'c' => MatchState::End, _ => MatchState::Error, }; } match state { MatchState::End => true, _ => false, }}
habu1010
#[derive(PartialEq, Eq)]enum Fsa { A, ZeroOrMoreB, C, End,}#[derive(PartialEq, Eq)]enum ParseResult { CurrentChar(Fsa), NextChar(Fsa), Terminate,}fn parse_ch(fsa: Fsa, ch: char) -> ParseResult { match fsa { Fsa::A => match ch { 'a' => ParseResult::NextChar(Fsa::ZeroOrMoreB), _ => ParseResult::Terminate, }, Fsa::ZeroOrMoreB => match ch { 'b' => ParseResult::NextChar(Fsa::ZeroOrMoreB), _ => ParseResult::CurrentChar(Fsa::C), }, Fsa::C => match ch { 'c' => ParseResult::NextChar(Fsa::End), _ => ParseResult::Terminate, }, Fsa::End => ParseResult::Terminate, }}pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let mut fsa = Fsa::A; for ch in input.chars() { let mut continue_loop = true; while continue_loop { fsa = match parse_ch(fsa, ch) { ParseResult::CurrentChar(next_fsa) => next_fsa, ParseResult::NextChar(next_fsa) => { continue_loop = false; next_fsa }, ParseResult::Terminate => return false, } } } fsa == Fsa::End}
FranklinChen
enum State { // Need to see 'a'. Initial, // Need to see 'b' or 'c'. SawA, // Saw 'c', need to end. SawC,}pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here use State::*; let mut state = Initial; for c in input.chars() { match state { Initial => { match c { 'a' => state = SawA, _ => return false, } }, SawA => { match c { 'b' => {}, 'c' => state = SawC, _ => return false, } }, SawC => { // Junk after the 'c'. return false; } } } matches!(state, SawC)}