Unary - puzzle discussion

Hi. I’m stuck to the Chuck test and I can’t understand why.

### Sortie console

### Sortie standard :

0 0 00 0000 0 0000 00 0 0 0 00 000 0 000 00 0 0 0 00 0 0 000 00 000 0 0000 00 0 0 0 00 0 0 000 00 00000 0 0 00 00 0 000 00 0 0 00 00 0 0 0000000 00 00 0 0 00 0 0 000 00 00 0 0 00 0 0 00 00 0 0 0 00 00 0 0000 00 00 0 000 00 00 0 0000 00 00000 0 00 00 0 0 0 00 0 0 0000 00 00 0 0 00 0 0 00000 00 00 0 000 00 000 0 0 00 0 0 00 00 0 0 000000 00 0000 0 0000 00 00 0 0 00 0 0 00 00 00 0 0 00 00 0 0 00 00000 0 00 00 0 0 0 00 000 0 00 00 0000 0 0000 00 00 0 000 00 00000 0 00 00 00 0 0 00 0 0 0 00 00000 0 00 00 0 0 0 00 0 0 0000 00 00 0 0 00 0 0 00000 00 00 0 0000 00 00 0 00000 00 0 0 0 00 0 0 0 00 00000 0 00 00 0000 0 0 00 00000 0 00 00 0000 0 000 00 0 0 000 00 0 0 00 00 00 0 0 00 00 0 0 00 00000 0 000 00 0 0 00000 00 0 0 0 00 000 0 00 00 0 0 0 00 00 0 0000 00 0 0 0 00 00 0 00 00 00 0 0 00 0 0 00 00 00000 0 000 00 00 0 00000 00 0000 0 00 00 0000 0 000 00 000 0 0000 00 00 0 0 00 0 0 00 00 0 0 000 00 0

### Échec

Trouvé :
0 0 00 0000 0 0000 00 0 0 0 00 000 0 000 00 0 0 0 00 0 0 000 00 000 0 0000 00 0 0 0 00 0 0 000...

Attendu :
0 0 00 0000 0 0000 00 0 0 0 00 000 0 000 00 0 0 0 00 0 0 000 00 000 0 0000 00 0 0 0 00 0 0 00 ...

The problem seems to start with the space, but it’s correctly converted…

k is 1101011.
Space is 0100000 instead of 100000.
So the last bits you show there (000) is wrong.

2 Likes

Why would it be 7 and not 8? Is this a standard I’m not familiar with?

Standard or not, “7 bits” is what the puzzle statement states. Either you observe it to pass the test cases and validators, or you ignore it to fail.

THANK YOU dude

Thanks for this note, helped me unblock this one after a looooong while : )

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <bitset>

using namespace std;

/**
 * Auto-generated code below aims at helping you parse
 * the standard input according to the problem statement.
 **/

string encryptMessage(string message, int bit = 7){
    string unaryMessage = "";
    string binary = "";

    if(bit == 7){
        for(int i = 0; i< message.size() ; i++){
            binary += bitset<7>(message[i]).to_string(); // convert message to 7 bit binary message
        }
    }

    for(int i =0 ; i< binary.size(); i++){
        if(binary[i] == '1'){
            unaryMessage +="0 ";
            for(int j = i ; binary[j] == '1' ; j++){
                i++;
                unaryMessage.push_back('0');
            }
            unaryMessage.push_back(' ');
        }
        if(binary[i] == '0'){
            unaryMessage +="00 ";
            for(int j = i ; binary[j] == '0' ; j++){
                i++;
                unaryMessage.push_back('0');
            }
            unaryMessage.push_back(' ');
        }
    }
    return unaryMessage;   
}

int main()
{
    string message;
    getline(cin, message);

    // Write an answer using cout. DON'T FORGET THE "<< endl"
    // To debug: cerr << "Debug messages..." << endl;

    cout << encryptMessage(message, 7) << endl;
}

found “0 0 00 0000 0 0”
expected “0 0 00 0000 0 00”

miss ‘0’ , I don’t understand why? Someone can help me please !!!

You have put i++ in various places with unintended effects. I suggest you to learn debugging by inserting code to print to error stream, so you can see the values of the variables at various points of the code like this:

    for(int i =0 ; i< binary.size(); i++){
        if(binary[i] == '1'){
            unaryMessage +="0 ";
            cerr << "(1)" << i << ":" << unaryMessage << endl;
            for(int j = i ; binary[j] == '1' ; j++){
                i++;
                unaryMessage.push_back('0');
                cerr << "(2)" << i << ":" << unaryMessage << endl;
            }
            unaryMessage.push_back(' ');
            cerr << "(3)" << i << ":" << unaryMessage << endl;
        }
        if(binary[i] == '0'){
            unaryMessage +="00 ";
            cerr << "(4)" << i << ":" << unaryMessage << endl;
            for(int j = i ; binary[j] == '0' ; j++){
                i++;
                unaryMessage.push_back('0');
                cerr << "(5)" << i << ":" << unaryMessage << endl;
            }
            unaryMessage.push_back(' ');
            cerr << "(6)" << i << ":" << unaryMessage << endl;
        }
    }

You’ll find that when you have finished the second if-block, i = 5. Then execution goes back to the last statement of the outer for-loop, which is i++, so i becomes 6 (which is the ending “1” position). Then the for-loop starts again, execution goes to the first if-block, and it just sees 1 “1” instead of 2 "1"s.

I get the correct answer on the Test4, and the custom test case. But it is still failing…

Do you mean you fail on submission? If so, you may check which validator(s) you are failing by clicking RESULTS on the left, and then MY REPORT and DETAILS.

The Chuck thing is really hard and I can’t get it to run with over 3h work
[Moderator’s edit: no full code please]

I done it by writeing a simple decode (easy) found some bugs that the previous test cases doesn’t cover

I have re-checked your code. The index you need to check is 6 instead of 5, and you need an additional pair of brackets in that line so that the condition is interpreted correctly.

This were the bugs I mentioned
But for all poeople who have problems with this challenge I really recommend writing a decoder so you can see what is blown up

Hello All, I am using C++ and I got my code to work and looking at the pros results. They have this line
((MESSAGE[0] & (1 << 6)) >> 6). I don’t understand what the << and >> means in this expression. Anyone know where I can go to learn more about this?

Bitwise operations in C - Wikipedia.

These operators exist in most languages.

Am I the only one slightly irked by using a 7 bit representation of a character instead of 8?

Hello ! I can’t reach 100% score. During the puzzle, 2 first lines ask for C and CC, but when I Submit, looks like 2 lines are programmed for G and GG. Did I miss something :thinking: ?

I don’t understand your question, but tests and validators are different for most of the puzzles here.

Hello! I finished the puzzle with 100% success, but I was wondering what else I would need to add to the code in order to get it to compile on my computer? I copy-pasted what I wrote into a visual studio code file, but when I go to compile it, it gives an error asking for a template class for the bitset function, and I have no idea where to start.