Ordinals are just fancy numbers that come after letters (like 1st, 2nd, or 3rd). But sometimes we need to store them in a database or send them over the internet without any mishaps.
To do this, we use some special techniques called “ordinal encoding” and “decoding”. Here’s how it works:
1. First, we convert our ordinals into numbers using their position in the alphabet (like 0 for first, 1 for second, etc.). For example, if we have a list of names with associated ordinals like this:
Alice (1st)
Bob (2nd)
Charlie (3rd)
We would convert them to numbers using the formula `(ordinal_position 1) * 10 + alphabetical_index` where `alphabetical_index` is the index of the first letter in their name. So for Alice, we have:
Alice (1st): `(1-1)*10+1 = 1`
Bob (2nd): `(2-1)*10+2 = 20`
Charlie (3rd): `(3-1)*10+3 = 30`
This gives us a unique number for each ordinal that we can store in our database or send over the internet.
2. When it’s time to decode these numbers back into their original form, we use some fancy math tricks (like division and modulo) to reverse this process. For example:
To get the position of an ordinal from its number, we divide by 10 and take the remainder when dividing by 3 (since there are only 26 letters in the alphabet). So for Alice’s number `1`, we have:
`(1 / 10) = 0` with a remainder of `1`. This tells us that our ordinal is in position `1st`.
To get the index of the first letter in their name, we divide by 3 again and take the floor (since there are only 26 letters in the alphabet). So for Alice’s number `1`, we have:
`(0 / 3) = 0` with a remainder of `0`. This tells us that their first letter is ‘A’, which has an index of `0` (since it comes before all other letters in the alphabet).
We’ve safely stored and sent our ordinals without any mishaps.