Skip to content

Commit 47fe203

Browse files
committed
Add proof solution
1 parent a63769f commit 47fe203

File tree

3 files changed

+136
-67
lines changed

3 files changed

+136
-67
lines changed
Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,82 @@
1+
import java.util.ArrayList;
12
import java.util.List;
23

34
public class IntergalacticTransmission {
45

5-
public static List<String> getTransmitSequence(List<String> message) {
6-
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
7-
}
6+
private static final byte INIT_UPPER_MASK = (byte) 0xFE;
87

9-
public static List<String> decodeSequence(List<String> sequence) {
10-
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
11-
}
8+
public static List<Integer> getTransmitSequence(List<Integer> message) {
9+
List<Integer> transmitSeq = new ArrayList<>();
10+
byte carry = 0;
11+
byte upperMask = INIT_UPPER_MASK;
1212

13+
for (int i = 0; i < message.size(); i++) {
14+
byte currentByte = message.get(i).byteValue();
15+
16+
if (upperMask == 0) {
17+
transmitSeq.add((int) addParity(carry) & 0xFF);
18+
carry = 0;
19+
upperMask = (byte) 0xFE;
20+
}
21+
22+
int shiftPlaces = Integer.numberOfTrailingZeros(upperMask & 0xFF);
23+
int current = ((carry & 0xFF) << (8 - shiftPlaces)) | ((currentByte & 0xFF) >>> shiftPlaces);
24+
transmitSeq.add((int) addParity((byte) current) & 0xFF);
25+
26+
carry = (byte) (currentByte & ~upperMask);
27+
upperMask = (byte) (upperMask << 1);
28+
}
29+
30+
if (upperMask != INIT_UPPER_MASK) {
31+
byte lastGroup = (byte) ((carry & 0xFF) << Integer.bitCount(upperMask & 0xFF));
32+
transmitSeq.add((int) addParity(lastGroup) & 0xFF);
33+
}
34+
35+
return transmitSeq;
36+
}
37+
38+
private static byte addParity(byte source) {
39+
if (Integer.bitCount(source & 0x7F) % 2 == 0) {
40+
return (byte) (source << 1);
41+
} else {
42+
return (byte) ((source << 1) | 1);
43+
}
44+
}
45+
46+
public static List<Integer> decodeSequence(List<Integer> receivedSeq) {
47+
if (receivedSeq.isEmpty()) {
48+
return new ArrayList<>();
49+
}
50+
51+
List<Integer> decodedMessage = new ArrayList<>();
52+
byte byteToAdd = 0x00;
53+
byte upperMask = (byte) 0xFF;
54+
55+
for (int i = 0; i < receivedSeq.size(); i++) {
56+
byte currentByte = receivedSeq.get(i).byteValue();
57+
58+
if (upperMask == (byte) 0xFF) {
59+
byteToAdd = getByteData(currentByte);
60+
upperMask = (byte) 0x80;
61+
continue;
62+
}
63+
64+
byte currentByteData = getByteData(currentByte);
65+
int shiftPlaces = Integer.numberOfTrailingZeros(upperMask & 0xFF);
66+
byte contribution = (byte) ((currentByteData & 0xFF) >>> shiftPlaces);
67+
decodedMessage.add((byteToAdd | contribution) & 0xFF);
68+
69+
byteToAdd = (byte) (((currentByteData & ~(upperMask | 0x01)) & 0xFF) << Integer.bitCount(upperMask & 0xFF));
70+
upperMask = (byte) (((upperMask & 0xFF) >>> 1) | 0x80);
71+
}
72+
73+
return decodedMessage;
74+
}
75+
76+
private static byte getByteData(byte data) {
77+
if (Integer.bitCount(data & 0xFF) % 2 != 0) {
78+
throw new IllegalArgumentException("Byte has incorrect parity");
79+
}
80+
return (byte) (data & 0xFE);
81+
}
1382
}

exercises/practice/intergalactic-transmission/src/main/java/IntergalacticTransmission.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
public class IntergalacticTransmission {
44

5-
public static List<String> getTransmitSequence(List<String> message) {
5+
public static List<Integer> getTransmitSequence(List<Integer> message) {
66
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
77
}
88

9-
public static List<String> decodeSequence(List<String> sequence) {
9+
public static List<Integer> decodeSequence(List<Integer> sequence) {
1010
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
1111
}
1212

0 commit comments

Comments
 (0)