Skip to content

Instantly share code, notes, and snippets.

View PrashantDhungana's full-sized avatar
🚀
Levelling Up ⬆

Prashant Sharma Dhungana PrashantDhungana

🚀
Levelling Up ⬆
View GitHub Profile
@joepie91
joepie91 / .md
Last active October 22, 2024 17:59
Prefix codes (explained simply)

A "prefix code" is a type of encoding mechanism ("code"). For something to be a prefix code, the entire set of possible encoded values ("codewords") must not contain any values that start with any other value in the set.

For example: [3, 11, 22] is a prefix code, because none of the values start with ("have a prefix of") any of the other values. However, [1, 12, 33] is not a prefix code, because one of the values (12) starts with another of the values (1).

Prefix codes are useful because, if you have a complete and accurate sequence of values, you can pick out each value without needing to know where one value starts and ends.

For example, let's say we have the following codewords: [1, 2, 33, 34, 50, 61]. And let's say that the sequence of numbers we've received looks like this:

1611333425012