I'm of the understand that there are a few Technology forum aficionados that are programmers, or are at least interested in programming, so I thought I'd start a thread -- a sort of game. The game was inspired by
this thread. It's pretty simple: just implement the following program in whatever language you desire. If someone's already posted a program in your favorite language, see if you can do better! You get bragging rights if you implement the program in a weird/unusual/rare language.
Here are the guidelines for the program:
- Write a method named matchesPattern1() that accepts a String as input. This method determines whether or not the input String has the following property:
The string has a number of c's equal to the total number of a's and b's.
In other words, every word in this language is of the form:
anbmc(n + m), where n ≥ 0 and m ≥ 0.
n and m may be equal, but this is not required.
Thus, bc, abccac, aabbcccc and abbbbccccc are all valid words in this language.
Your method should return a boolean value: true if the input string satisfies these requirements, and false if it does not.
For example, given a String such as aabbcc, your method should return false. - Write a method named matchesPattern2() that accepts a String as input. This method determines whether or not the input String has the following properties:
The string has a number of c's equal to the total number of a's and b's.
All a's come before any b's, and all b's come before any c's. (HINT: Can you test for this using one or more boolean variable "flags"?)
In other words, every word in this language is of the form:
anbmc(n + m), where n ≥ 0 and m ≥ 0.
n and m may be equal, but this is not required.
Thus, aabbcccc and abbbbccccc are valid words in this language, but abaccc and abc are not.
Your method should return a boolean value: true if the input string satisfies these requirements, and false if it does not.
For example, given a String such as aabbcc, your method should return false.
This pattern is identical to the one above, except it imposes a specific ordering requirement on the input string. - Write a method named matchesPattern3() that accepts a String as input. This method determines whether or not the input String has the following properties:
The string has an equal (nonzero) number of a's and c's, and any number of (zero or more) b's.
All a's come before any b's, and all b's come before any c's.
In other words, every word in this language is of the form:
anb*cn, where n > 0.
Thus, aabbcc and ac are valid words in this language, but abaccc and aabc are not.
Your method should return a boolean value: true if the input string satisfies these requirements, and false if it does not.
For example, given a String such as aabbaa, your method should return false.