6 kyu
Error correction #1 - Hamming Code
901 of 3,621zLuki
Description:
Translations appreciated
Background information
The Hamming Code is used to correct errors, so-called bit flips, in data transmissions. Later in the description follows a detailed explanation of how it works.
In this Kata we will implement the Hamming Code with bit length 3; this has some advantages and disadvantages:
- [ + ] It's simple to implement
- [ + ] Compared to other versions of hamming code, we can correct more mistakes
- [ - ] The size of the input triples
Task 1: Encode function
Implement the encode function, using the following steps:
- convert every letter of the text to its ASCII value; (ASCII value of space is 32)
- convert ASCII values to 8-bit binary;
- triple every bit;
- concatenate the result;
For example:
input: "hey"
--> 104, 101, 121 // ASCII values
--> 01101000, 01100101, 01111001 // binary
--> 000111111000111000000000 000111111000000111000111 000111111111111000000111 // tripled
--> "000111111000111000000000000111111000000111000111000111111111111000000111" // concatenated
Task 2: Decode function:
Check if any errors happened and correct them. Errors will be only bit flips, and not a loss of bits:
111
-->101
: this can and will happen111
-->11
: this cannot happen
Note: the length of the input string is also always divisible by 24 so that you can convert it to an ASCII value.
Steps:
- Split the input into groups of three characters;
- Check if an error occurred: replace each group with the character that occurs most often, e.g.
010
-->0
,110
-->1
, etc; - Take each group of 8 characters and convert that binary number;
- Convert the binary values to decimal (ASCII);
- Convert the ASCII values to characters and concatenate the result
For example:
input: "100111111000111001000010000111111000000111001111000111110110111000010111"
--> 100, 111, 111, 000, 111, 001, ... // triples
--> 0, 1, 1, 0, 1, 0, ... // corrected bits
--> 01101000, 01100101, 01111001 // bytes
--> 104, 101, 121 // ASCII values
--> "hey"
If you liked this kata, please try out some of my other katas:
Crack the PIN
Decode the QR-Code
Hack the NSA
Algorithms
Bits
Similar Kata:
Stats:
Created | Jun 29, 2020 |
Published | Aug 6, 2020 |
Warriors Trained | 9765 |
Total Skips | 575 |
Total Code Submissions | 12057 |
Total Times Completed | 3621 |
JavaScript Completions | 901 |
Python Completions | 1350 |
PHP Completions | 104 |
TypeScript Completions | 102 |
Java Completions | 333 |
C# Completions | 246 |
Julia Completions | 23 |
CoffeeScript Completions | 9 |
Rust Completions | 99 |
Ruby Completions | 80 |
C Completions | 84 |
C++ Completions | 174 |
Kotlin Completions | 61 |
Go Completions | 80 |
Dart Completions | 71 |
Groovy Completions | 10 |
PowerShell Completions | 12 |
Haskell Completions | 29 |
Lua Completions | 28 |
Clojure Completions | 10 |
NASM Completions | 6 |
D Completions | 3 |
Shell Completions | 10 |
Total Stars | 304 |
% of votes with a positive feedback rating | 95% of 680 |
Total "Very Satisfied" Votes | 617 |
Total "Somewhat Satisfied" Votes | 54 |
Total "Not Satisfied" Votes | 9 |
Total Rank Assessments | 11 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 6 kyu |
Lowest Assessed Rank | 8 kyu |