Any pets in your house? I have a 10gal fish tank. You may not want to keep more
ID: 3882869 • Letter: A
Question
Any pets in your house? I have a 10gal fish tank. You may not want to keep more than three fishes in such a small tank, so I choose to maintain a population of exactly 3 (no more, no less, and don't ask me why). Unfortunately, every day, exactly one fish dies and I immediately buy a new one from a local fish store and put it in the tank by the end of the day. This happens until someday later no fish dies and I have always the same three fish in my tank. Here are some rules about the tank: (1). At most these two kinds of fish in the tank: damsels and clowns, (2). Since damsels are pretty aggressive, if the tank has at least one clown, then there is no more than one damsel in the tank. At the end of each day, my tank is in one of the following possible configurations: A: three damsels, B: three clowns, C: 2 clowns and 1 damsel. From day one to day n, I may observe a sequence (with length n) of these configurations, which is a word on alphabet {A, B, C}. Write a regular expression that represents all the possible observed sequences for all n.Explanation / Answer
Solution=======================
Observation:
Hence a java based Regex pattern will look like: (^A+$) | (^[BC]+$)
You can test out your sequence here:
public class TestSimple {
public static void main(String[] args) {
String s="BCCBBBC"; //Create sequence here
//Checking for either AAAAAA or BCCBCC
System.out.println(s.matches("(^A+$)|(^[BC]+$)"));
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.