Advertisement
Guest User

ASMR Coding #9 source code

a guest
Jul 30th, 2021
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. public class CodingProblems {
  2.  
  3.  
  4. public static void main(String[] args) {
  5.  
  6.  
  7.  
  8. System.out.println("TESTING SOLUTION TO PROBLEM #1");
  9.  
  10. System.out.println(accum("abcd"));
  11.  
  12. System.out.println("TESTING SOLUTION TO PROBLEM #2");
  13. // 00000000 10101010 11111111
  14.  
  15. //expected:
  16. // 11111111 10101010 00000000
  17. int[] testdata = new int[]{1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,1,0,1,0,1,0};
  18.  
  19. printByteArray(testdata);
  20. System.out.println();
  21. printByteArray(reverseData(testdata));
  22.  
  23. }
  24.  
  25.  
  26. //problem 1 attempt successful!
  27.  
  28. public static String accum(String mumble) {
  29.  
  30. String mumbled = "";
  31.  
  32. //outer 4 loop to loop ever single letter in the string input
  33. for (int i = 0; i < mumble.length(); i++) {
  34.  
  35. //inner 4 loop: each letter has to be repeated 1 more than the precious
  36. for (int j = 0; j <= i; j++) {
  37.  
  38. if (j == 0) {
  39. //concactinate a capital letter if its the first letter in a repeat series
  40. mumbled += mumble.toUpperCase().charAt(i);
  41. }
  42. else {
  43. mumbled += mumble.toLowerCase().charAt(i);
  44. }
  45. }
  46.  
  47. if (i != mumble.length() - 1) {
  48. mumbled += "-";
  49. }
  50. }
  51.  
  52. return mumbled;
  53. }
  54.  
  55.  
  56.  
  57. //problem 2 attempt successful
  58. public static int[] reverseData(int[] data) {
  59.  
  60. int[] reversedData = new int[data.length];
  61. int nextPos = 0;
  62.  
  63. //data validation
  64. if (data.length % 8 != 0) {
  65. System.out.println("invalid data");
  66. return new int[]{-1};
  67. }
  68. // loop through every group of 8 bits, starting from the end of the array
  69.  
  70. // 00000000 11111111 00000000
  71. for (int i = data.length - 1; i >= 0; i = i - 8) {
  72.  
  73. // inner 4 loop that inserts a new bit/byte of that group, starting from the left
  74.  
  75. //10101010 10101010
  76. for (int j = i-7; j <= i; j++) {
  77.  
  78. //add in the next bit in the reversed data array
  79. //loop begins j = i-7
  80. //loop ends j = i
  81. reversedData[nextPos] = data[j];
  82. nextPos++;
  83. }
  84.  
  85. }
  86.  
  87. return reversedData;
  88. }
  89.  
  90. //print int arrays
  91. public static void printByteArray(int[] x) {
  92. for (int i = 0; i < x.length; i++) {
  93. System.out.print(x[i]);
  94.  
  95. //every 8th value, print a space to separate the groups of 8 bits
  96. if ((i+1) % 8 == 0) {
  97. System.out.print(" ");
  98. }
  99. }
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement