Archive

Posts Tagged ‘codingame’

Unary encoded for 7-bit ASCII

November 17, 2015 Leave a comment

Chuck Norris challenge in codingame.com

import java.util.*;
import java.io.*;
import java.math.*;

/**
 * unary encoded
 * @author grekz.wordpress.com
 **/
class Bytes {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        String MESSAGE = in.nextLine();
        StringBuilder binary = new StringBuilder(), result = new StringBuilder();
        
        for (byte b : MESSAGE.getBytes()) {
            int val = b << 1;
            for (int i = 0; i < 7; i++) {
                binary.append((val & 128) == 0 ? 0 : 1);
                val <<= 1;
            }
        }
        //grekz.wordpress.com
        char current = '2';
        for (int i = 0; i < binary.length(); i++) {
            char c = binary.charAt(i);
            if (current != c) {
                current = c;
                result.append(" ").append(current == '1' ? "0" : "00")
                        .append(" ");
            }
            result.append("0");
        }
        System.out.println(result.toString().trim());
        in.close();
    }
}
Advertisements
Categories: Beyond, CodinGame, Java, snippets

ASCII Art – Challenge

November 17, 2015 Leave a comment

This is the solution of the ASCII Art problem in the CodinGame page.
https://www.codingame.com/games/puzzles

import java.util.*;
import java.io.*;
import java.math.*;

/** @author Juan Mendoza grekz.wordpress.com
* ASCII art allows you to represent forms by using characters. To be precise, in our case, these forms are words. For example, the word “MANHATTAN” could be displayed as follows in ASCII art:
*
* # # # ### # # # ### ### # ###
* ### # # # # # # # # # # # # # #
* ### ### # # ### ### # # ### # #
* # # # # # # # # # # # # # # # #
* # # # # # # # # # # # # # # # #
*
* Your mission is to write a program that can display a line of text in ASCII art.
**/
class Solution {

  public static void main(String args[]) {
    Scanner in = new Scanner(System.in);
    int L = in.nextInt();
    in.nextLine();
    int H = in.nextInt();
    in.nextLine();
    String T = in.nextLine().toUpperCase();
    String [][] chars= new String[27][H];
    for (int i = 0; i < H; i++) {
        String ROW = in.nextLine();
        for(int j = 0; j < 27; j++){
            chars[j][i] = ROW.substring(j*L, (j+1) * L);
        }
    }
    for (int i = 0; i < H; i++) {
        for(char x : T.toCharArray() ){
            x -= 65;
            if(x < 0 || x > 26) x = 26;
            System.out.print(chars[x][i]);
        }
        System.out.println();
    }
  }
}
Categories: Beyond, CodinGame, Java, snippets