Skip to content

Instantly share code, notes, and snippets.

@cbilson
Created February 6, 2018 12:22
Show Gist options
  • Save cbilson/a01a2fd84385e42dc91a56aa1e958c94 to your computer and use it in GitHub Desktop.
Save cbilson/a01a2fd84385e42dc91a56aa1e958c94 to your computer and use it in GitHub Desktop.
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 cowspe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MMMMMMOOOOOOOOOOOOO MMMMMMO MMMMMMOOOOOOOOO MMMMMMOOOOOOOOOOOOOO MMOOOOOOOO MMMMMOOO MMMMMMMOOOO MMMMMMMOO MMMMMMOOOOOOOOO MMMMMMOOOOOOOOOOOOOO MMMMMMOOOOOOO MMMMMOOOOOOOOOOO MMMMMOOOOOOOOOOOOO MM MMMMMMO MMMMMMMOO MMMMMMOOOOOOO MMMMMMMOOO MMOOOOOOOOO MM MMMMMMMOOOOOOOOOOO MM MMMMMOOO MMMMMMMOOOOOOOOO MMMMMMMOOO MMMMMMMOOOO MMMMMMOOOOO MMMMMMOOOOOOOOOOOOO MMOOOOOOOOOOOOOO MMMMMMOOOOOOOOOOOOOOO MMMMMMMOOOOO MMMMMMMOOOO MMOOOOOOOOOOOOOO MMMMMMM MMMMMMMOO MMMMMMOOOOOOOOO MMMMMMOOOOOOOOOOOOOO MMMMMMMOOOO MMMMMMOOOOOOOOOOOO MMMMMMOOOOOOOOOOOOOO MMOOOOOOOO MMOO MMMMOOOOOOOOOOOOO MMMMMMOOOOOOOOOOOOOOO MMMMMMOOOOOOOOOOOOOOO MMO MMOO MMOOOOOOOOO MMMOOOOOOOOOOO MM MMMMMMMOOOOOOOOOOOOO MM MMMMMMMOOOOOOOOOOOOO
MMMMOOOOOOOOOOOOOO MMMMMMOOOOOOOOOOOOOOO MMMMMMMOOOOOOO MM MMMMMMMOOOOOOOOO MMMMMMOOOOOOOOOOOOOOO MMMMMMMOOOOO MM MMMMMMOOOOOOOO MMMMMMO MMMMMMMOOOOOO MMMMMMOOOOO MMOOOOOOOOOOOOOO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment