Click here to Skip to main content
15,740,731 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The heart of the encoding scheme for your program is a sequence of "key" strings of 0’s and 1’s as follows:

0, 00, 01, 10, 000, 001, 010, 011, 100, 101, 110, 0000, 0001, . . . , 1011, 1110, 00000, . . .

The first key in the sequence is of length 1, the next 3 are of length 2, the next 7 of length 3, the next 15 of length 4, etc. 
If two adjacent keys have the same length, the second can be obtained from the first by adding 1 (base 2).
 Notice that there are no keys in the sequence that consist only of 1’s.
The keys are mapped to the characters in the header in order. 
That is, the first key (0) is mapped to the first character in the header, the second key (00) to the second character in the header, the kth key is mapped to the kth character in the header. For example, suppose the header

Then 0 is mapped to A, 00 to B, 01 to #, 10 to T, 000 to A, ..., 110 to X, and 0000 to c. The encoded message contains only 0’s and 1’s and possibly carriage returns,
 which are to be ignored. The message is divided into segments. The first 3 digits of a segment give the binary representation of the length
  of the keys in the segment. For example, 
  if the first 3 digits are 010, then the remainder of the segment consists of keys of length 2 (00, 01, or 10).
   The end of the segment is a string of 1’s which is the same length as the length of the keys in the segment. 
   So a segment of keys of length 2 is terminated by 11. 
   The entire encoded message is terminated by 000 (which would signify a segment in which the keys have length 0). 
   The message is decoded by translating the keys in the segments one-at-a-time into the header characters to which they have been mapped.


The input file contains several data sets. Each data set consists of a header, which is on a single line by itself, and a message, which may
extend over several lines. The length of the header is limited only by the fact that key strings have a maximum length of 7 (111 in binary).
Ifthere are multiple copies of a character in a header, then several keys will map to that character.
 The encoded message contains only 0’s and 1’s, and it is a legitimate encoding according to the described scheme.
  That is, the message segments begin with the 3-digit length sequence andend with the appropriate sequence of 1’s.
   The keys in any given segment are all of the same length, and they all correspond to characters in the header.
    The message is terminated by 000. Carriage returns may appear anywhere within the message part. They are not to be considered
as part of the message.


For each data set, your program must write its decoded message on a separate line. There should not be blank lines between messages.

What I have tried:

using namespace std;
char c,code[8][1<<7];
char readchar(){
        c = getchar();
        if(c != '\n' && c != '\r') return c;
int readint(int c){
    int v = 0;
        v = 2*v + readchar() - '0';
    return v;
int readcodes(){
    code[1][0] = readchar();
    for(int len = 2;len < 8;len++){
        for(int j = 0;j < (1<<len) - 1;j++){
            c = getchar();
            if(c == EOF) return 0;
            if(c == '\n' || c == '\r') return 1;
            code[len][j] = c;
    return 1;
int main(){
    while(readcodes()){ //Exit when unable to read more code headers
            int len = readint(3);
            if(!len) break;
                int value = readint(len);
                if(value == (1<<len) -1 ) break;
    return 0;
Updated 22-Mar-22 20:52pm

.i just want a python code for description

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
Share this answer
CPallini 23-Mar-22 3:00am    
 It depends to what degree… - Yes and No…

First of all - both languages are very different. So translation would not be an easy task, also when your C++ code would (for example) do something Python by design can’t (like executing Assembler), its pretty much over (except if you provide a library - written in C++, which then would provide Python with those abilities) - but then its getting really hacky…

Now, how could it be done:

When you are able to abstract all instructions - and provide an equivalent to any, you can basically translate it - even with a different syntax… - Programming languages always follow their Syntax, which makes translation possible (without the use of AI).

Now this might become a rather long example, but just to demonstrate you, how difficult it would be, even with a rather easy snippet of code - i’ll mostly be using Java throughout that demo:

Original C++ snippet:

#include <iostream> 
int n = 3; 
if (n > 0) { 
	std::cout << "n is positive" << "\n"; 
std::cout << "Bye World!" << "\n"; 
Now, first you’d want to split this into logic entities. We have 3 Entities - 1 Assignment, 1 If-clause and 1 function-call.

To split this, you’d probably use a RegularExpression as those are incredibly mighty when it comes to such use-cases - but that would really blow up this demo now, because you’d also have to differentiate between blocks that need or do not need curly braces… but lets assume, you have split that sourcecode into the following Strings:

String[] entities = { 
	"#include <iostream>", 
	"int number = 3;", 
	"if (number > 0) { std::cout << "n is positive" << "\n"; }", 
	"std::cout << "goodbye cruel world." << "\n";" } 
Lets ignore the #include direction for now (I’ll come back to that later…)

Lets make an abstract class:

public abstract class Entity { 
	public String toPython(); 
	public String toCpp(); 
Next, since "int number = 3;" is an Assignment, lets create a class AssignmentEntitty:

public class AssignmentEntity extends Entity { 
	String targetType; 
	String traget; 
	String source; 
	public String toPython() { 
		return target + " = " + source; 
	public String toCpp() { 
		return targetType + " " + target + " = " + source + ";"; 
You’ll notice that i skipped the Constructor - to save a few lines…

For that If Clause - you’d also want to extend from Entity like so:

public class IfEntity extends Entity { 
	ConditionEntity condition; 
	ArrayList<Entity> onTrue; 
	ArrayList<Entity> onFalse; 
	public String toPython() { 
		return "if " + condition.toPython() + ":\n\t" + onTrue.toString();"; 
	public String toCpp() { 
		return "if (" + condition.toCpp() + ") {" + onTrue.toCpp() + "}"; 
This code will not work - i’ve taken a few shortcuts to make it easier for the reader. You cant run "toPython()" on an ArrayList like that of course - you’d have to make another Entity and so on and so on - also that ConditionEntity does not exist yet - and maybe is not even necessary…

To create all those entities, you now parse that first Array and create those entities in the order they appear. - If you do everything right, you will end up with working Python-Code, when you call your top most toPython()… but - this is pure overkill!

About the "#include" directive, well - to make things even more complicated, you’d have to also add to every of your entities, if they require special includes, and keep track of those aswell…

I do not think that there will ever be a program to really translate C++ to Python, but… for sure this would be a great practice…

Back in 2002, i once had to write software that would translate bytecode coming from an MQL2-Compiler back to MQL. And that above approach was, what i used. It worked fine, but it was a hell of a work…

I hope this was not too boring, and could at least point you in a direction - as well as maybe help others with a similar Problem. Anyways - this answer is by no means in any way complete… - i just hope it helps…
Share this answer
Vasudha jale 23-Mar-22 1:23am    
thanks for your quick response .but i dont want explanation its too long .i just want a python code for description
OriginalGriff 23-Mar-22 2:53am    
Don't bother - he want's his homework done, and thinks the C++ code will be close enough.
Basically, he doesn't want to learn Python (who does?) and doesn't know C++ ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900