Unary - puzzle discussion

Sorry for asking this stupid question but neither forum nor google search brought something and I really can’t be assed to reverse code it at the moment so:
What’s Chuck’s message?

His message is “Chuck Norris’ keyboard has 2 keys: 0 and white space.”

Do this:

  1. Click the Settings icon from the IDE
  2. Enable Expert Mode
  3. Click the 3 horizontal bars tile next to the Test Cases and Custom buttons. This will let you see all the test cases and their expected output.
1 Like

Thanks a lot!! I was stuck there as well, wondering why the hell it was not working. Adding the single condition “if to binary string length < 7 put 0 in front of the string” did the job ! :smiley:

I’m getting 75% on the problem using Java, I’m not sure why it’s failing the last prompt. Can someone tell me what’s going wrong with my code?

import java.util.*;
    import java.io.*;
    import java.math.*;
    
    class Solution {
        private static int length;
        private static char current;
        private static String out;
        private static String binIn;
        
        public static void main(String args[]) {
            Scanner in = new Scanner(System.in);
            String message = in.nextLine();
            byte[] bytes = message.getBytes();
            String[] asciiValues = new String[bytes.length];
            
            for(int i = 0; i < bytes.length; i++){
                asciiValues[i] = String.format("%07d",Integer.valueOf(Integer.toBinaryString((int)bytes[i])));
            }
        
            binIn = "";
            for(String str : asciiValues)
                binIn += str;
            
            out = " ";
            
            length = 1; current = binIn.charAt(0);
            
            for(int i = 1; i < binIn.length(); i++){
                if(binIn.charAt(i) != current) //new series
                    encode(i);
                else if (i == binIn.length()-1){ //end of input
                    length++;
                    encode(i);
                } else
                    length++;
            }
            System.out.println(out.trim());
        }
        
        private static void encode(int i){
            switch(current){
                case '0':
                    out+="0";
                case '1':
                    out+="0 ";
            }
            
            for(int j = 0; j<length; j++)
                out+="0";
            out+=" ";
            
            length = 1;
            current = binIn.charAt(i);
        }
    }

[edit: fixed some formatting and removed System.err.print statements]

Why does this problem assume the first index of the binary conversion is removed? To clarify, C is 01000011, yet this problem assumes 1000011 which makes no sense to me as CC would be 0100001101000011 and yet this uses 10000111000011 which wouldn’t be accurate at all. Am I missing something?

Il y a des incohérences dans ce puzzle lors d’une résolution en PHP. Certains caractères sortent en ASCII 6 bits et non 7 bits. Du coup il faut penser à vérifier la longueur de chaque caractère ASCII et à ajouter un “0” devant s’il ne fait que 6 bits au lieu de 7. Sans ça, on aura plusieurs “0” manquant dans le code final et ne pourra pas passer les 3ème et 4ème tests du puzzle.

my code outputs the correct string but it keeps telling me that it is wrong.
the out put for the first two test cases is the same as the examples. what is going on?
I’m using Python 3

Maybe you print to the wrong stream (error instead of out)?

@chr-m yes i did. thanx

1 Like

Should we begin to convert the entry to binary before starting to convert to unary to solve the problem?
Doit-on commencer par effectuer une conversion du texte en entrée en binaire avant d’effectuer une conversion en unaire?
Merci

Maybe yes but read the statement carefully.

I’m having a problem with my code, in java, the console is telling me that it’s taking too long to process. I chose to convert to binary using modulo method, which should work. Here is my code, is it so wrong ?
Thanks for your feedbacks !

class Solution {

public static void main(String args[]) {
    Scanner in = new Scanner(System.in);
    String MESSAGE = in.nextLine();
    String answer = "";

     for (int i = 0; i < MESSAGE.length(); i++) {
        int n_car = (int) MESSAGE.charAt(i); 
        
        do {
           int modulo = n_car % 2;
           
            if ( modulo == 0 ) {
                answer += "0 ";
                do {
                   answer += "0";
                   n_car /= 2;
                   modulo = n_car % 2;
                    } while ( modulo == 0 );
                answer += " ";
                }
             else if ( modulo == 1 ) {
                answer += "00 ";
                do {
                   answer += "0";
                   n_car = n_car / 2 - 1;
                   modulo = n_car % 2;
                    } while ( modulo == 0 );
                answer += " ";
                }
           
            } while ( n_car != 0 );  
    
     }
     
    System.out.println(answer);
}
}

Well, I solved it !

I have learned a lot in Java : the foreach loop, how to use stringBuilder (and append, insert, replace, etc.), and to think about break out from a loop before to cause an exception.

1 Like

Hello,
I have a strange issue my output seems to be ok but I have error messgae saying :
Found: “00 0”
Expected: "00 0 "

The 0 sequence depend of the input but I always have the " " at the end missing.

function toBin(str){
 var st,i,j,d;
 var arr = [];
 var len = str.length;
 for (i = 1; i<=len; i++){
                //reverse so its like a stack
  d = str.charCodeAt(len-i);
  for (j = 0; j < 8; j++) {
   arr.push(d%2);
   d = Math.floor(d/2);
  }
 }
 while(arr.length > 7){ // While the last element is a 0,
    arr.pop();                  // Remove that last element
}
        //reverse all bits again.
 return arr.reverse().join("");
}
var MESSAGE = readline();
    var bytes = []; // char codes
var bytesv2 = []; // char codes
var output = "";
 for (var i = 0; i < MESSAGE.length; i++) {
      output += toBin(MESSAGE[i]);
  }
 printErr(output);
var encoded='';
 var prev = -1;
 var first = true;
for(var i =0;i < output.length;i++)
{
    printErr(i);
    
      //printErr(prev);
    printErr('prev : '+prev);
    printErr('output[i] : '+output[i]);
        if(output[i]!==' '){
            if(output[i]!==prev){
                if(prev!==-1)
                     encoded+=' ';
                if(output[i]==='0'){
                    encoded+='00 0';
                }
                else{
                    encoded+='0 0';
                }
                prev = output[i];
            }
            else{
                encoded+= '0';
            }
            //print(encoded);
        }
        print(encoded);
}

Grrrrrrrrrrrr… je ne trouve pas pourquoi je n’arrive pas le dernier test (la phrase de Chucky…)

peux tu m’expliquer pourquoi il y a autan de zéro dans le troisième cas ?

As-tu bien lu la consigne ? :wink:

Effectivement, j’ai mis la consigne en français et tout va mieux. il va falloir que je bosse mon anglais.

I have this : on print : 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

Found : “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”

Expected : "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 "

I do not understand why the two are not the same