Unary - puzzle discussion

You need 7-bit, not 6-bit.

My solution works on Visual Studio but this C++ line is giving me error on the site:
cout << basic_string_view(encoded_msg.begin()+1, encoded_msg.end());

how come?

What’s the error message?

the error is very long, but the relevant part is: error: no matching function for call to ‘basic_string_view(__gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string >, std::__cxx11::basic_string::iterator)’

this specific basic_string_view constructor I’m using (that takes two iterators) is a C++20 feature, and the code worked properly on VS, so my assumption is C++20 is not yet supported here :slight_smile:

That’s what many other players request here: https://www.codingame.com/forum/t/languages-update/1574/324

1 Like

So a common misunderstanding that I fell into myself here regarding the “7-bit” condition is that I and many others here falsely assumed that it suffices if the whole binary code base gets enough 0-padding at the very start of the entire sequence so that the binary code is a multiple of 7 and can then be converted into the so-called “unary code”.

But that assumption was wrong. You don’t just have to convert the entire binary code sequence to 7-bit as a whole, but rather have to convert each individual character to 7-bit.

The most common occurrence of that issue I have seen on this forum thread is that the problem starts on the fourth test with the first white space character after the first word “Chuck”. It works up until that point because all their characters start with “1” in binary. But the white space is the first issue because we mistakenly used 100000 for it, but in 7-bit it has a leading 0, i.e. 0100000.

So all you have to do to fix it is that upon converting ASCII characters to binary code you have to ensure each character is already 7-bit in binary before proceeding further, so make sure to handle each character individually if you have no means to directly transform it into 7-bit binary code.

That being said, I agree with many others that this puzzle probably should not be in the easy section, or that at least the instruction has to be formulated more concretely, pointing out such common misconceptions.

1 Like

How do I convert string to binary? I don’t see any guidance in the task. I copied some piece of code which seems to work for single charactes but it gives different output for CC or the full sentence.

c_bytes = str(''.join(format(ord(i), '08b') for i in message))[1:]
c_bytes = bin(int.from_bytes(message.encode(), 'big'))[2:]

I tired these but it doesn’t match CC
100001101000011
instead of
10000111000011

It’s converted in 7 bits even if the first one is 0.

Test 1 to 3 works, but test 4(Chuck Norris) gives me an error. Please help.
Why will this error only be an issue on test 4?

Errors

Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.String.get_Chars
at Solution.Main on line 58

The error means that the index is either less than 0 or greater than or equal to the length of the array (assuming the indexing is 0-based). You can check what the array and the index are at that point and investigate from there.