Chuck Norris


Codingame: Chuck Norris
Original problem

The Goal

Binary with 0 and 1 is good, but binary with only 0, or almost, is even better! Originally, this is a concept designed by Chuck Norris to send so called unary messages.

Write a program that takes an incoming message as input and displays as output the message encoded using Chuck Norris’ method.

Rules

Here is the encoding principle:

  • The input message consists of ASCII characters (7-bit)
  • The encoded output message consists of blocks of 0
  • A block is separated from another block by a space
  • Two consecutive blocks are used to produce a series of same value bits (only1 or0 values):
    – First block: it is always 0 or 00. If it is 0, then the series contains 1, if not, it contains 0
    – Second block: the number of 0 in this block is the number of bits in the series
Example

Let’s take a simple example with a message which consists of only one character: Capital C. C in binary is represented as 1000011, so with Chuck Norris’ technique this gives:

  • 0 0 (the first series consists of only a single 1)
  • 00 0000 (the second series consists of four 0)
  • 0 00 (the third consists of two 1)

So C is coded as: 0 0 00 0000 0 00

Second example, we want to encode the message CC (i.e. the 14 bits 10000111000011) :

  • 0 0 (one single 1)
  • 00 0000 (four 0)
  • 0 000 (three 1)
  • 00 0000 (four 0)
  • 0 00 (two 1)

So CC is coded as: 0 0 00 0000 0 000 00 0000 0 00

Game Input
Input
Line 1: the message consisting of N ASCII characters (without carriage return)
Output
The encoded message
Constraints
0 < N < 100
Java Solution
class Solution {

     public String toBinary(int dec) {
         String res = "";
         String temp = "";
         while (dec > 0) {
             if (dec % 2 != 0) res += 1;
             if (dec % 2 == 0) res += 0;
             dec /= 2;
         }
         for (int i = res.length() - 1; i >= 0; i--) {
             temp += res.charAt(i);
         }
         res = temp;
         if (res.length() == 6) res = "0" + res;
         return res;
     }

     public String toUnary(String binary) {
         StringBuilder builder = new StringBuilder();
         for (int i = 0; i < binary.length(); i++) {
             if (binary.charAt(i) == '1') {
                 if (i == 0 || (i > 0 && binary.charAt(i - 1) == '0')) {
                     if (i == 0) builder.append("0 0");
                     if (i > 0) builder.append(" 0 0");
                 }
                 if ((i > 0 && binary.charAt(i - 1) == '1')) {
                     builder.append("0");
                 }
             }
             if (binary.charAt(i) == '0') {
                 if ((i == 0) || (i > 0 && binary.charAt(i - 1) == '1')) {
                     if (i == 0) builder.append("00 0");
                     if (i > 0) builder.append(" 00 0");
                 }
                 if ((i > 0 && binary.charAt(i - 1) == '0')) {
                     builder.append("0");
                 }
             }
         }
         binary = builder.toString();
         return binary;
     }

     public String encode(String chuckMessage) {
         String result = "";
         StringBuilder builder = new StringBuilder();
         for (int i = 0; i < chuckMessage.length(); i++) {
             int a = (int) (chuckMessage.charAt(i));
             result = toBinary(a);
             builder.append(result);
         }
         result = builder.toString();
         return toUnary(result);
     }

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

         System.out.println(object.encode(MESSAGE));
     }
 }

Dodaj komentarz

Twój adres email nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *