Unary - puzzle discussion

hi @MedAyoub for quick reply. I am using format(ord(token), ‘07b’) for converting characters into binary values. On individual level character conversion is perfect and my code is working fine with other three cases. So I am confused about what could be the problem?

@push44 I think you miss somthing , I use C and C++ so can’t help you , sorry

Are you failing the 4th test case? If so, you may consider checking whether your code handles spaces and special characters correctly.

No problem and thanks for offering the help.

Yes, I was confused because code is handling special characters correctly. The problem was with spaces which I realized later from your comment. Problem is solved now. Thanks!

1 Like

Salut ! J’utilise ce code mais ça coince au niveau de la conversion en unaire :confused: je sais que il y a un problème avec mes espaces que j’ai pas encore corrigé mais il y a un autre problème, la fin de la chaine binaire est pas traduite j’ai l’impression c’est assez bizarre :frowning:

    import sys
import math
compteur0 = 0
compteur1 = 0
reponse = ""
message = input()
binary_converted = ' '.join(format(ord(c), 'b') for c in message)
print (binary_converted)
for c in binary_converted:
    if c == "0":
        compteur0 = compteur0 +1
        if compteur1 > 0:
            reponse = reponse + " 0 "
            while compteur1 > 0:
                compteur1 = compteur1 - 1
                reponse = reponse + "0"
    if c == "1":
        compteur1 = compteur1 +1
        if compteur0 > 0:
            reponse = reponse + " 00 "
            while compteur0 > 0:
                compteur0 = compteur0 - 1
                reponse = reponse + "0"
print (reponse)

Attention, l’ASCII doit être ajusté à 7 bits !

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.