Skip to content

Commit 1b1dbc3

Browse files
committed
Sync intergalactic transmission with problem specs
1 parent c1e3bd3 commit 1b1dbc3

File tree

8 files changed

+230
-74
lines changed

8 files changed

+230
-74
lines changed
Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,54 @@
11
# Instructions
22

3-
Your job is to help implement the message sequencer to add the parity bit to the messages and the decoder to receive messages.
3+
Your job is to help implement
44

5-
The entire message, itself, is sequence of a number of bytes.
6-
The transmitters and receivers can only transmit and receive one byte at a time, so parity bit needs to be added every eighth bit.
7-
The algorithm for adding the bits is as follows:
8-
1. Divide the message bits into groups of 7, starting from the left (the message is transmitted from left to right).
9-
2. If the last group has less than 7 bits, append some 0s to pad the group to 7 bits.
10-
3. For each group, determine if there are an odd or even number of 1s.
11-
4. If the group has even number of 1s or none at all, append a 0 to the group. Otherwise, append 1 if there is odd number.
5+
- the transmitter, which calculates the transmission sequence, and
6+
- the receiver, which decodes it.
127

13-
For example, consider the message 0xC0_01_C0_DE.
14-
Writing this out in binary and locating every 7th bit:
8+
A parity bit is simple way of detecting transmission errors.
9+
The transmitters and receivers can only transmit and receive _exactly_ eight bits at a time (including the parity bit).
10+
The parity bit is set so that there is an _even_ number 1s in each transmission and is always the first bit from the right.
11+
So if the receiver receives `11000001`, `01110101` or `01000000` (i.e. a transmission with an odd number of 1 bits), it knows there is an error.
12+
13+
However, messages are rarely this short, and need to be transmitted in a sequence when they are longer.
14+
15+
For example, consider the message `11000000 00000001 11000000 11011110` (or `C0 01 C0 DE` in hex).
16+
17+
Since each transmission contains exactly eight bits, it can only contain seven bits of data and the parity bit.
18+
A parity bit must then be inserted after every seven bits of data:
1519

1620
```text
17-
C 0 0 1 C 0 D E
18-
1100_0000 0000_0001 1100_0000 1101_1110
19-
↑ ↑ ↑ ↑ (7th bits)
21+
11000000 00000001 11000000 11011110
22+
↑ ↑ ↑ ↑ (7th bits)
2023
```
2124

22-
The last group has only 4 bits (0b1110), so three 0 bits are appended to make it 7 bits:
25+
The transmission sequence for this message looks like this:
2326

2427
```text
25-
| 1100_000 | 0000_000 | 0111_000 | 0001_101 | 1110_000 |
28+
1100000_ 0000000_ 0111000_ 0001101_ 1110
29+
↑ ↑ ↑ ↑ (parity bits)
2630
```
2731

28-
The first group contains two 1s (an even number of 1s), so 0 is appended to the group.
29-
The second group has none, so append 0.
30-
The rest have three, so they append 1.
32+
The data in the first transmission in the sequence (`1100000`) has two 1 bits (an even number), so the parity bit is 0.
33+
The first transmission becomes `11000000` (or `C0` in hex).
34+
35+
The data in the next transmission (`0000000`) has zero 1 bits (an even number again), so the parity bit is 0 again.
36+
The second transmission thus becomes `00000000` (or `00` in hex).
37+
38+
The data for the next two transmissions (`0111000` and `0001101`) have three 1 bits.
39+
Their parity bits are set to 1 so that they have an even number of 1 bits in the transmission.
40+
They are transmitted as `01110001` and `00011011` (or `71` and `1B` in hex).
41+
42+
The last transmission (`1110`) has only four bits of data.
43+
Since exactly eight bits are transmitted at a time and the parity bit is the right most bit, three 0 bits and then the parity bit are added to make up eight bits.
44+
It now looks like this (where `_` is the parity bit):
3145

3246
```text
33-
| 1100_0000 | 0000_0000 | 0111_0001 | 0001_1011 | 1110_0001 |
34-
| C 0 | 0 0 | 7 1 | 1 B | E 1 | (in hex)
47+
1110 000_
48+
↑↑↑ (added 0 bits)
3549
```
3650

37-
Thus, the transmission sequence is 0xC0_00_71_1B_E1.
51+
There is an odd number of 1 bits again, so the parity bit is 1.
52+
The last transmission in the sequence becomes `11100001` (or `E1` in hex).
53+
54+
The entire transmission sequence for this message is `11000000 00000000 01110001 00011011 11100001` (or `C0 00 71 1B E1` in hex).
Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
# Introduction
22

3-
Trillions upon trillions of messages are exchanged between Earth and the neighboring galaxies every millisecond.
4-
But transmission over such long distances faces many obstacles.
5-
Pesky solar flares, worm holes, temporal distortions, stray forces, heck even the flap of a space butterfly wing can cause a random bit to flip during transmission.
6-
The difference between an "o" and a "k", a "[" and "{" or a cowboy emoji and a clown emoji can have huge ramifications, from missed orders to Galactic Stockmarket crashes.
7-
It is thus important for the receiver to know if a message has been corrupted so that the receiver can ask for the message to be sent again.
8-
9-
One day, an important message comes through.
10-
People have just heard about the legend about a way of transmitting in such a way that the receiver can tell when bit flipped ... the parity bit ...
3+
Trillions upon trillions of messages zip between Earth and neighboring galaxies every millisecond.
4+
But transmitting over such long distances is tricky.
5+
Pesky solar flares, temporal distortions, stray forces, and even the flap of a space butterfly's wing can cause a random bit to change during transmission.
6+
7+
Now imagine the consequences:
8+
9+
- Crashing the Intergalactic Share Market when "buy low" turns to "sell now".
10+
- Losing contact with the Kepler Whirl system when "save new worm hole" becomes "cave new worm hole".
11+
- Or plunging the universe into existential horror by replacing a cowboy emoji 🤠 with a clown emoji 🤡.
12+
13+
Detecting corrupted messages isn't just important — it's critical.
14+
The receiver _must_ know when something has gone wrong before disaster strikes.
15+
16+
But how?
17+
Scientists and engineers from across the universe have been battling this problem for eons.
18+
Entire cosmic AI superclusters churn through the data.
19+
And then, one day, a legend resurfaces — an ancient, powerful method, whispered in debugging forums, muttered by engineers who've seen too much...
20+
21+
The Parity Bit!
22+
23+
A method so simple, so powerful, that it might just save interstellar communication.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
public class {{ testClass }}
2+
{
3+
{{- for test in tests }}
4+
[Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}]
5+
public void {{ test.testMethod }}()
6+
{
7+
{{- message = string.replace test.input.message "\"" "" }}
8+
{{- if test.property == "transmitSequence" }}
9+
Assert.Equal({{ string.replace test.expected "\"" "" }}, IntergalacticTransmission.GetTransmitSequence({{ message }}));
10+
{{- else if test.property == "decodeMessage" }}
11+
{{- if test.expected.error }}
12+
Assert.Throws<ArgumentException>(() => IntergalacticTransmission.DecodeSequence({{ message }}));
13+
{{- else }}
14+
Assert.Equal({{ string.replace test.expected "\"" "" }}, IntergalacticTransmission.DecodeSequence({{ message }}));
15+
{{ end -}}
16+
{{ end -}}
17+
}
18+
{{ end -}}
19+
}

exercises/practice/intergalactic-transmission/.meta/config.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111
],
1212
"example": [
1313
".meta/Example.cs"
14+
],
15+
"invalidator": [
16+
"IntergalacticTransmission.csproj"
1417
]
1518
},
16-
"blurb": "Help detect errors in transmissions to neighboring galaxies"
19+
"blurb": "Add parity bits to a message for transmission",
20+
"source": "Kah Goh",
21+
"source_url": "https://github.com/exercism/problem-specifications/pull/2543"
1722
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[f99d4046-b429-4582-9324-f0bcac7ab51c]
13+
description = "calculate transmit sequences -> empty message"
14+
15+
[ee27ea2d-8999-4f23-9275-8f6879545f86]
16+
description = "calculate transmit sequences -> 0x00 is transmitted as 0x0000"
17+
18+
[97f27f98-8020-402d-be85-f21ba54a6df0]
19+
description = "calculate transmit sequences -> 0x02 is transmitted as 0x0300"
20+
21+
[24712fb9-0336-4e2f-835e-d2350f29c420]
22+
description = "calculate transmit sequences -> 0x06 is transmitted as 0x0600"
23+
24+
[7630b5a9-dba1-4178-b2a0-4a376f7414e0]
25+
description = "calculate transmit sequences -> 0x05 is transmitted as 0x0581"
26+
27+
[ab4fe80b-ef8e-4a99-b4fb-001937af415d]
28+
description = "calculate transmit sequences -> 0x29 is transmitted as 0x2881"
29+
30+
[4e200d84-593b-4449-b7c0-4de1b6a0955e]
31+
description = "calculate transmit sequences -> 0xc001c0de is transmitted as 0xc000711be1"
32+
33+
[fbc537e9-6b21-4f4a-8c2b-9cf9b702a9b7]
34+
description = "calculate transmit sequences -> six byte message"
35+
36+
[d5b75adf-b5fc-4f77-b4ab-77653e30f07c]
37+
description = "calculate transmit sequences -> seven byte message"
38+
39+
[6d8b297b-da1d-435e-bcd7-55fbb1400e73]
40+
description = "calculate transmit sequences -> eight byte message"
41+
42+
[54a0642a-d5aa-490c-be89-8e171a0cab6f]
43+
description = "calculate transmit sequences -> twenty byte message"
44+
45+
[879af739-0094-4736-9127-bd441b1ddbbf]
46+
description = "decode received messages -> zero message"
47+
48+
[7a89eeef-96c5-4329-a246-ec181a8e959a]
49+
description = "decode received messages -> 0x0300 is decoded to 0x02"
50+
51+
[3e515af7-8b62-417f-960c-3454bca7f806]
52+
description = "decode received messages -> 0x0581 is decoded to 0x05"
53+
54+
[a1b4a3f7-9f05-4b7a-b86e-d7c6fc3f16a9]
55+
description = "decode received messages -> 0x2881 is decoded to 0x29"
56+
57+
[2e99d617-4c91-4ad5-9217-e4b2447d6e4a]
58+
description = "decode received messages -> first byte has wrong parity"
59+
60+
[507e212d-3dae-42e8-88b4-2223838ff8d2]
61+
description = "decode received messages -> second byte has wrong parity"
62+
63+
[b985692e-6338-46c7-8cea-bc38996d4dfd]
64+
description = "decode received messages -> 0xcf4b00 is decoded to 0xce94"
65+
66+
[467549dc-a558-443b-80c5-ff3d4eb305d4]
67+
description = "decode received messages -> six byte message"
68+
69+
[1f3be5fb-093a-4661-9951-c1c4781c71ea]
70+
description = "decode received messages -> seven byte message"
71+
72+
[6065b8b3-9dcd-45c9-918c-b427cfdb28c1]
73+
description = "decode received messages -> last byte has wrong parity"
74+
75+
[98af97b7-9cca-4c4c-9de3-f70e227a4cb1]
76+
description = "decode received messages -> eight byte message"
77+
78+
[aa7d4785-2bb9-43a4-a38a-203325c464fb]
79+
description = "decode received messages -> twenty byte message"
80+
81+
[4c86e034-b066-42ac-8497-48f9bc1723c1]
82+
description = "decode received messages -> wrong parity on 16th byte"

exercises/practice/intergalactic-transmission/IntergalacticTransmission.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@
1515
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1" />
1616
<PackageReference Include="Exercism.Tests.xunit.v3" Version="0.1.0-beta1" />
1717
</ItemGroup>
18-
</Project>
18+
</Project>

0 commit comments

Comments
 (0)