Unary - puzzle discussion

Thank you so much I had the exact same issue and couldn’t figure out what the problem was but your solution worked for me

Hello,
For the chuck norris problem:
For the CC chacracters:
Found: 0 0 00 0000 0 00
Expected: 0 0 00 0000 0 000
there is an extra 0 in the expected answer. where as in the rules it clearly sais that :
CC is coded as: 0 0 00 0000 0 000 00 0000 0 00
why while running the tests we have an extra 0 that is expected?
same for the %
Thank you,

Expected:       0 0 00 0000 0 000
CC is coded as: 0 0 00 0000 0 000 00 0000 0 00
3 Likes

Thank you. dont know why i had a weird behaviour i copied the code again and it worked:) thanl you for your help anyways !

You showed me the way , but I decided filling with zero every character. It worked.

Hello everybody,
I am having problem in Java
This is my code fragment:

if(msg.charAt(i)=='1'){ parity="0 "; // one 0 for a 1 j=i; while(msg.charAt(j)!='0'){ // loop until 0 comes counter1++; // no of 1s j++; } for(j=0;j<counter1;j++){ // loop for the no of 1s ans += '0'; // append 0s } ans += parity + ans; }else{ parity="00 "; j=i; **while(msg.charAt(j)!='1'){** counter0++; j++; } for(j=0;j<counter0;j++){ ans += '0'; } ans += parity + ans; } }

I get the error message:
java.lang.StringIndexOutOfBoundsException: String index out of range: 7
at java.lang.String.charAt on line 658
at Solution.main on line 53

The line of this error is in bold face (EDIT: between ** ** ) above.
I don’t understand why there is index out of bound exception!!

I’d say it’s pretty difficult for anyone to read your code and understand it.
What I would do:

  • check the values i and j can take
  • check length of msg
  • use System.err.println
  • define what your while loop is doing (while characters in my string are different than 1, update a counter and make me check the next element in the string… until?)
  • determine the conditions which will lead to this IOOB exception
1 Like

Thanks for the reply.

  • the variable i loops through the length of the msg
    (Sorry I omitted the line for(i=0;i<msg.length();i++) )

  • the while loops iterate through the number of zeroes or ones. If the first char is 1, loop until the char is 0 (while (msg.charAt(j)!=0) ), and increment the value of 1 counter (counter1) in every loop. If the first char is 0, loop until the next char is 1 (while (msg.charAt(j)!=0 ), and increment the value of 0 counter (counter0) in every loop.

  • the two while loops are identical and yet I get the exception error only in the second loop !!

what is the end condition of your loop? (besides the ioob exception :stuck_out_tongue: )

The end condition of the first for loop is the length of the msg (saved in variable i).
The end condition of both while loop is the change of data (from 1 to 0 or vice-versa)
The while loop continues until the data changes from 1 to 0 (or 0 to 1) . This continues for the length of the msg.

I’m still waiting for a reply!!! :tired_face:

I cannot help you better than I did. Your while loop while (msg.charAt(j)!=0) has an issue because msg has a finished length and j never stops to increase.

I have a problem with this puzzle.
I designed a solution in c++ and it was working fine for all but the last test.
So i submitted it in case the test base not working but the validation was and couldn’t make it work.

I then took the exact same solution in C# (copy/paste) juste changing the input / output functions and it’s working fine.

Is there a possibity that some of the test case are wrong or is Chuck Norris really to much for me to handle ?

I believe Chuck Norris is to much for you a the moment.

2 Likes
Scanner in = new Scanner(System.in);
String result="";
String message = in.nextLine();
//string letters to string binary
byte[] binaryString = message.getBytes();
for (byte b: binaryString) {
    result+=Integer.toBinaryString(b);
}
//System.out.println(result);
//string binary to encoded message
String answer="";
char previous='2';
for(int i=0;i<result.length();i++)
{
    char c = result.charAt(i);
    if(c==previous)
    {
        answer+="0";
    }
    else
    {
        if(previous!= '2')
            answer+=" ";
        previous=c;
        if(previous=='1')
            answer+="0 0";
        else if(previous=='0')
            answer+="00 0";
    }
}
System.out.println(answer);

I have a problem with this puzzle.
I failed tests: 3 and 4. Could you expain to me what’s wrong with my code?

I advise you to print your result in the console using System.err.println.
For the third test (character %) it has 6 bits only, while it has 7 for the first two tests… Description says The input message consists of ASCII characters (7-bit)

From that I think you’ll find your error :slight_smile:

Also, a good habit is to check what is the expected output (that’s how I understood your error) using this button:

2 Likes

@thibaudCG I found and fixed my error. Thank you for your help. :slight_smile:

4 Tests are OK but when submitting code, the test with # failed.
Don’t understand why because with % it’s OK

My Code works for one letter but after that it removes the beginning of the code when iterating back over itself, I have made use of regular expression and was wondering if anyone sees how I can have it iterate over the later part of the binary but not remove the start in doing so.

Many thanks, apologies if I posted this wrong but I am new to codeingame!

[no full code please]

To be completely clear–paniquesillo6’s error was that they only zero-padded once in the code, as opposed to zero-padding every character.

1 Like