6 kyu
Error correction #1 - Hamming Code
904 of 3,647zLuki
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 | 9876 |
Total Skips | 578 |
Total Code Submissions | 12134 |
Total Times Completed | 3647 |
JavaScript Completions | 904 |
Python Completions | 1360 |
PHP Completions | 104 |
TypeScript Completions | 104 |
Java Completions | 335 |
C# Completions | 247 |
Julia Completions | 23 |
CoffeeScript Completions | 9 |
Rust Completions | 100 |
Ruby Completions | 81 |
C Completions | 86 |
C++ Completions | 175 |
Kotlin Completions | 62 |
Go Completions | 81 |
Dart Completions | 75 |
Groovy Completions | 10 |
PowerShell Completions | 12 |
Haskell Completions | 29 |
Lua Completions | 28 |
Clojure Completions | 10 |
NASM Completions | 6 |
D Completions | 3 |
Shell Completions | 11 |
Total Stars | 303 |
% of votes with a positive feedback rating | 95% of 685 |
Total "Very Satisfied" Votes | 621 |
Total "Somewhat Satisfied" Votes | 55 |
Total "Not Satisfied" Votes | 9 |
Total Rank Assessments | 11 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 6 kyu |
Lowest Assessed Rank | 8 kyu |