Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CodingProblems {
- public static void main(String[] args) {
- System.out.println("TESTING SOLUTION TO PROBLEM #1");
- System.out.println(accum("abcd"));
- System.out.println("TESTING SOLUTION TO PROBLEM #2");
- // 00000000 10101010 11111111
- //expected:
- // 11111111 10101010 00000000
- 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};
- printByteArray(testdata);
- System.out.println();
- printByteArray(reverseData(testdata));
- }
- //problem 1 attempt successful!
- public static String accum(String mumble) {
- String mumbled = "";
- //outer 4 loop to loop ever single letter in the string input
- for (int i = 0; i < mumble.length(); i++) {
- //inner 4 loop: each letter has to be repeated 1 more than the precious
- for (int j = 0; j <= i; j++) {
- if (j == 0) {
- //concactinate a capital letter if its the first letter in a repeat series
- mumbled += mumble.toUpperCase().charAt(i);
- }
- else {
- mumbled += mumble.toLowerCase().charAt(i);
- }
- }
- if (i != mumble.length() - 1) {
- mumbled += "-";
- }
- }
- return mumbled;
- }
- //problem 2 attempt successful
- public static int[] reverseData(int[] data) {
- int[] reversedData = new int[data.length];
- int nextPos = 0;
- //data validation
- if (data.length % 8 != 0) {
- System.out.println("invalid data");
- return new int[]{-1};
- }
- // loop through every group of 8 bits, starting from the end of the array
- // 00000000 11111111 00000000
- for (int i = data.length - 1; i >= 0; i = i - 8) {
- // inner 4 loop that inserts a new bit/byte of that group, starting from the left
- //10101010 10101010
- for (int j = i-7; j <= i; j++) {
- //add in the next bit in the reversed data array
- //loop begins j = i-7
- //loop ends j = i
- reversedData[nextPos] = data[j];
- nextPos++;
- }
- }
- return reversedData;
- }
- //print int arrays
- public static void printByteArray(int[] x) {
- for (int i = 0; i < x.length; i++) {
- System.out.print(x[i]);
- //every 8th value, print a space to separate the groups of 8 bits
- if ((i+1) % 8 == 0) {
- System.out.print(" ");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement