Guest User

Untitled

a guest
Feb 19th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.Scanner;
  4.  
  5. /*
  6. * The animals are conspiring again. The cows are mooing very strangely and
  7. * you’re certain of it. Your theory is that the length of each “MM” sound
  8. * and the length of the “OO” sound can be translated to hexadecimal values
  9. * and then converted to ascii. Create a program to translate cowspeak to
  10. * English so you can find out what they’re up to.
  11. *
  12. * Input:
  13. * The first line will contain a single integer n that indicates the number
  14. * of data sets that follow. Each data set will consist of one line
  15. * containing many space separated strings representing the cows’ moos.
  16. *
  17. * Output:
  18. * For each test case, output the decoded cowspeak
  19. */
  20. public class Cowspeak {
  21.  
  22. public static void main(String[] args) throws FileNotFoundException {
  23. // For testing:
  24. File f = new File("C:\\Users\\chris\\Desktop\\Cowspeak1.txt");
  25. Scanner input = new Scanner(f);
  26.  
  27. // For submission:
  28. //Scanner input = new Scanner(System.in);
  29.  
  30. // The first line will contain a single integer n that indicates the
  31. // number of data sets that follow.
  32. int n = input.nextInt();
  33.  
  34. // Right now I am just before the end of the first line, so I need to
  35. // move the scanner down to the second line by finishing reading this
  36. // line.
  37. input.nextLine();
  38.  
  39. for (int i = 0; i < n; i++) {
  40. // Each data set will consist of one line containing many space
  41. // separated strings representing the cows’ moos.
  42. String line = input.nextLine();
  43. for (String token : line.split(" ")) {
  44. // count Ms and Os
  45. //System.out.println(token);
  46. int ms = 0, os = 0;
  47. for (int j = 0; j < token.length(); j++)
  48. if (token.charAt(j) == 'M')
  49. ms++;
  50. else if (token.charAt(j) == 'O')
  51. os++;
  52.  
  53. //System.out.println("Ms: " + ms + ", Os: " + os);
  54.  
  55. // I think the number of Ms is the 16s digit (the left-most digit)
  56. // and the number of Os is the 1s digit (right most) of a char.
  57. char c = (char)(ms*16 + os);
  58. System.out.print(c);
  59. }
  60.  
  61. System.out.println();
  62. }
  63. }
  64.  
  65. private static String reverseCowSpeak(String words) {
  66. String cowspeak = "";
  67. for (int i = 0; i < words.length(); i++) {
  68. char c = words.charAt(i);
  69. int ms = c / 16;
  70. int os = c % 16;
  71.  
  72. for (int j = 0; j < ms; j++)
  73. cowspeak += "M";
  74.  
  75. for (int j = 0; j < os; j++)
  76. cowspeak += "O";
  77.  
  78. cowspeak += " ";
  79. }
  80.  
  81. return cowspeak;
  82. }
  83. }
Add Comment
Please, Sign In to add comment