Problem Statement
NOTE: This problem statement contains images that may not display properly if viewed outside of the applet.
Taro shows a magic trick to Hanako.
Taro: Hello Hanako. I'll show you a magic trick. Please imagine a positive integer less than or equal to 16.
Hanako: OK. I imagined it.
Taro: (Taro shows card 1 to Hanako.) Does this card contain your number?
Hanako: Yes.
Taro: (Taro shows card 2 to Hanako.) Does this card contain your number?
Hanako: No.
Taro: (Taro shows card 3 to Hanako.) Does this card contain your number?
Hanako: Yes.
Taro: (Taro shows card 4 to Hanako.) Does this card contain your number?
Hanako: Yes.
Taro: Your number is 5!
(Card 1 contains 1, 2, 3, 4, 5, 6, 7 and 8. Card 2 contains 1, 2, 3, 4, 9, 10, 11 and 12. Card 3 contains 1, 2, 5, 6, 9, 10, 13 and 14. Card 4 contains 1, 3, 5, 7, 9, 11, 13 and 15.)
Your task is to write a program that simulates this magic trick. You are given Hanako's answers in theString answer. The i-th character is 'Y' if she answered "yes" to the i-th question, and 'N' if she answered "no" to the i-th question. Return the integer Hanako imagined.
Taro shows a magic trick to Hanako.
Taro: Hello Hanako. I'll show you a magic trick. Please imagine a positive integer less than or equal to 16.
Hanako: OK. I imagined it.
Taro: (Taro shows card 1 to Hanako.) Does this card contain your number?
Hanako: Yes.
Taro: (Taro shows card 2 to Hanako.) Does this card contain your number?
Hanako: No.
Taro: (Taro shows card 3 to Hanako.) Does this card contain your number?
Hanako: Yes.
Taro: (Taro shows card 4 to Hanako.) Does this card contain your number?
Hanako: Yes.
Taro: Your number is 5!

(Card 1 contains 1, 2, 3, 4, 5, 6, 7 and 8. Card 2 contains 1, 2, 3, 4, 9, 10, 11 and 12. Card 3 contains 1, 2, 5, 6, 9, 10, 13 and 14. Card 4 contains 1, 3, 5, 7, 9, 11, 13 and 15.)
Your task is to write a program that simulates this magic trick. You are given Hanako's answers in the
Definition
- Class:
- NumberMagicEasy
- Method:
- theNumber
- Parameters:
- String
- Returns:
- int
- Method signature:
- int theNumber(String answer)
- (be sure your method is public)
Constraints
- answer will contain exactly 4 characters.
- Each character in answer will be 'Y' or 'N'.
Examples
"YNYY"
Returns: 5
The example from the statement.
"YNNN"
Returns: 8
8 is the only number that exists on the first card and does not exist on any other cards.
"NNNN"
Returns: 16
"YYYY"
Returns: 1
"NYNY"
Returns: 11
"NNNY"
Returns: 15
"NNYN"
Returns: 14
"NNYY"
Returns: 13
"NYNN"
Returns: 12
"NYYN"
Returns: 10
"NYYY"
Returns: 9
"YNNY"
Returns: 7
"YNYN"
Returns: 6
"YYNN"
Returns: 4
"YYNY"
Returns: 3
"YYYN"
Returns: 2