I actually wrote this contest and I wrote it in VB. Here is a link to my raw solution.
An artist wants to construct a sign whose letters will rotate freely in the breeze. In order to do this, she must only use letters that are not changed by rotation of 180 degrees: I, O, S, H, Z, X, and N.
Write a program that reads a word and determines whether the word can be used on the sign.
Input Specification
The input will consist of one word, all in uppercase letters, with no spaces. The maximum length
of the word will be 30 letters, and the word will have at least one letter in it.
Output Specification
Output YES if the input word can be used on the sign; otherwise, output NO.
Sample Input 1
SHINS
Output for Sample Input 1
YES
Sample Input 2
NOISE
Output for Sample Input 2
NO
import java.util.Scanner; import java.io.File; public class CCC_Problem_J2_Rotating_Letters { public static void main(String[] args) { try { Scanner file = new Scanner(new File("C:\\Users\\Mike\\Desktop\\ccc_2013\\stage_1\\test_data\\j2\\j2.1-1.in")); String word = file.nextLine(); // use a regular expression as a delimiter if (word.replaceAll("[IOSHZXN]", "").equals("")) { // replace all valid letters with blank space. If the entire string is blank, all characters were valid // if (word.split("[IOSHZXN]").length == 0) { // another way of doing it, just with an array System.out.println("YES"); } else { System.out.println("NO"); } } catch (Exception e) { e.printStackTrace(); } } }
In the above solution, I used regular expressions (regex) to determine if the string was valid. Look at the following code for that:
// use a regular expression as a delimiter if (word.replaceAll("[IOSHZXN]", "").equals("")) { // replace all valid letters with blank space. If the entire string is blank, all characters were valid // if (word.split("[IOSHZXN]").length == 0) { // another way of doing it, just with an array System.out.println("YES"); } else { System.out.println("NO"); }
But if that scares you or you don't understand regex, you could change up the algorithm and have it loop through each letter. On each
letter, test if it is valid or not with a verbose if statement, or you could even use regex with the .match
method on the character.
Look at the follwing code for that:
boolean valid = true; for (int i = 0; i < word.length(); i++) { // iterate through every letter in the word char c = word.charAt(i); // current character if (c != 'I' && c != 'O' && c != 'S' && c != 'H' && c != 'Z' && c != 'X' && c != 'N') { // if none of the letters // if (String.valueOf(c).matches("[^IOSHZXN]")) { // another way of doing it valid = false; // any letter is not valid, word is invalid } } // print result if (valid) System.out.println("YES"); else System.out.println("NO");
Using their test cases:
All the following outputs are exactly the expected outputs.
Input:
TEST
Output:
NO
DOWNLOAD as .in
Input:
HOISIN
Output:
YES
DOWNLOAD as .in
Input:
NOZ
Output:
YES
DOWNLOAD as .in
Input:
ZZZABC
Output:
NO
DOWNLOAD as .in
Input:
ABCDE
Output:
NO
DOWNLOAD as .in
Input:
IOI
Output:
YES
DOWNLOAD as .in
Input:
HI
Output:
YES
DOWNLOAD as .in
Input:
AXE
Output:
NO
DOWNLOAD as .in
Input:
IIOOSSHHZZXXNN
Output:
YES
DOWNLOAD as .in
Input:
IOSHZXNQ
Output:
NO
DOWNLOAD as .in
Created: April 18, 2014
Completed in full by: Michael Yaworski