Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- /*
- * The animals are conspiring again. The cows are mooing very strangely and
- * you’re certain of it. Your theory is that the length of each “MM” sound
- * and the length of the “OO” sound can be translated to hexadecimal values
- * and then converted to ascii. Create a program to translate cowspeak to
- * English so you can find out what they’re up to.
- *
- * Input:
- * The first line will contain a single integer n that indicates the number
- * of data sets that follow. Each data set will consist of one line
- * containing many space separated strings representing the cows’ moos.
- *
- * Output:
- * For each test case, output the decoded cowspeak
- */
- public class Cowspeak {
- public static void main(String[] args) throws FileNotFoundException {
- // For testing:
- File f = new File("C:\\Users\\chris\\Desktop\\Cowspeak1.txt");
- Scanner input = new Scanner(f);
- // For submission:
- //Scanner input = new Scanner(System.in);
- // The first line will contain a single integer n that indicates the
- // number of data sets that follow.
- int n = input.nextInt();
- // Right now I am just before the end of the first line, so I need to
- // move the scanner down to the second line by finishing reading this
- // line.
- input.nextLine();
- for (int i = 0; i < n; i++) {
- // Each data set will consist of one line containing many space
- // separated strings representing the cows’ moos.
- String line = input.nextLine();
- for (String token : line.split(" ")) {
- // count Ms and Os
- //System.out.println(token);
- int ms = 0, os = 0;
- for (int j = 0; j < token.length(); j++)
- if (token.charAt(j) == 'M')
- ms++;
- else if (token.charAt(j) == 'O')
- os++;
- //System.out.println("Ms: " + ms + ", Os: " + os);
- // I think the number of Ms is the 16s digit (the left-most digit)
- // and the number of Os is the 1s digit (right most) of a char.
- char c = (char)(ms*16 + os);
- System.out.print(c);
- }
- System.out.println();
- }
- }
- private static String reverseCowSpeak(String words) {
- String cowspeak = "";
- for (int i = 0; i < words.length(); i++) {
- char c = words.charAt(i);
- int ms = c / 16;
- int os = c % 16;
- for (int j = 0; j < ms; j++)
- cowspeak += "M";
- for (int j = 0; j < os; j++)
- cowspeak += "O";
- cowspeak += " ";
- }
- return cowspeak;
- }
- }
Add Comment
Please, Sign In to add comment