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 happen
  • 111 --> 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

Stats:

CreatedJun 29, 2020
PublishedAug 6, 2020
Warriors Trained9765
Total Skips575
Total Code Submissions12057
Total Times Completed3621
JavaScript Completions901
Python Completions1350
PHP Completions104
TypeScript Completions102
Java Completions333
C# Completions246
Julia Completions23
CoffeeScript Completions9
Rust Completions99
Ruby Completions80
C Completions84
C++ Completions174
Kotlin Completions61
Go Completions80
Dart Completions71
Groovy Completions10
PowerShell Completions12
Haskell Completions29
Lua Completions28
Clojure Completions10
NASM Completions6
D Completions3
Shell Completions10
Total Stars304
% of votes with a positive feedback rating95% of 680
Total "Very Satisfied" Votes617
Total "Somewhat Satisfied" Votes54
Total "Not Satisfied" Votes9
Total Rank Assessments11
Average Assessed Rank
6 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • zLuki Avatar
  • natan Avatar
  • anter69 Avatar
  • imjasonmiller Avatar
  • dominikdosoudil Avatar
  • clcraig Avatar
  • user9644768 Avatar
  • hobovsky Avatar
  • trashy_incel Avatar
  • qmstuart Avatar
  • rge123 Avatar
  • akar-0 Avatar
  • porkfreezer Avatar
Ad