# Week 5 assignment - Encoding and decoding a file

Hello All

I am solving the file encoding and decoding using the function 2a + 3. The encoding part appears simple but while decoding , we need to know how many digits to group (since they can be any number of digits) to get back the original character. For this I thought while I encode it, I will add a delimiter like “-” (hash) so that I can use this to determine how many digits I need to group

So if there is a word like “is”, I will get the digits for the letter “i” followed by a “-” and then the digits for “s”

Is this fine or is there an alternative way of doing it ?

Thanks
Harish

Hi @hari.yajurveda, It’s very simple, if y=f(a) = 2a+3 was used encode the ASCII values then you can tke the inverse of y=f(a) i,e,

y=f(a)

y=2a+3

\frac{y-3}{2} = a

where a is the original ASCII value and y is the encoded value of the character.

Thank you. Yes thats what am doing to decode the file. My question was when we decode, we have a file full of numbers corresponding to the characters that was in original file

Now, when I process this file, I need to know how many digits I need to take to get back my character

Lets look at this sentence for ex
“I learn”

This will get converted to 149 219205197231223

Now when I am decoding this back, and take the set of digits 219205197231223 for processing, how do I know I need to take 3 digits or 2 digits at a time for extracting the character (in some cases some characters have only 2 digit ascii value)

So I thought if I decode it as
149- 291-205-197-231-223, then I can be sure how many digits I need to take to get the character back. For ex I will do (291-3)/2 and do a chr() to get the character as there is a delimiter after that

Does this make sense ?

Before you say that your encoding function is enc(a) = 2a+3, you need to clearly define what ‘a’ is.
If you define ‘a’ as ASCII value, you need to clarify on what all characters will you encode?

If you that you are going to encode only the alphabets (caps or small) in your string, and retain all other characters as it is, then one thing you can do is check the ASCII (decimal) number of all those interested characters.

Turns out, when you encode your alphabets using your 2a+3 formula, you always get a 3-digit number.
So to decode (the encoded alphabets), you can assume a constant width of 3 digits and decode them.

Remember that, this may fail if you have numbers in your input string, since it will be retained as it when encoding, and create ambiguity when decoding. Hence a good practice would be to clearly define your problem before proceeding for a solution, and understanding the constraints under which your solution works and does not work.

Yes. “a” is defined as the ASCII value of the character (thats given in the assignment). I intend to encode the whole file with numbers/punctuation converted to the corresponding code

So I will insert a delimiter to know where toe start and end for a character while decoding