Monday, 29 February 2016

Atbash

Getting better at the typing, new keyboard helps as it forces me to type inline with the guide no right hand b key. I now understand the support tipping the keyboard away significantly, raising the wrists makes that bit easier on the fingers for QWE and POI keys. A bit slow unless I am in the mood for it. 


black split keyboard with chicklet keys



Had a go at the atbash challenge for daily programmer, in my opinion they just look up the algorithm then implement it in their choosen language, not the most efficient but makes sense to me.

package com.company;

public class Main {

    public static void main(String[] args) {
        String inputString = "ullyzi\n" +
                "draziw\n" +
                "/i/wzrobkiltiznnvi\n" +
                "this is an example of the atbash ZZZ cipher";

        for (char aCharacter : inputString.toCharArray()) {
            if (Character.isLetter(aCharacter)) {
                System.out.print(atBashPreserveCase(aCharacter));
            } else {
                System.out.print(aCharacter);
            }
        }
    }

    private static char atBashPreserveCase(char aCharacter) {
        if (Character.isUpperCase(aCharacter))
            return Character.toUpperCase(atBash(aCharacter));
        else
            return atBash(aCharacter);
    }

    private static char atBash(char aCharacter) {
        aCharacter = Character.toLowerCase(aCharacter);
        if (aCharacter == 'm')
            return (char) aCharacter;
        else if (aCharacter > 'm')
            return (char) ('z' - aCharacter + 'a');
        else
            return (char) ('z' - (aCharacter - 'a'));
    }
}

Saturday, 20 February 2016

Another day another challenge

Over the month I have been working on Java OCA, now scoring in the 60% after a month in the high 40 low 50s, I need to break into the 80% range before I can book the exam. 

Started to learn to touch type as one of those things I have always wanted to get around to, typing club helps. 

Talking to the developers at work about interviews, quick as you can tic tac toe, best I can do in about 30 minutes, error I noticed afterwards is it works from top left as is 0,0. 

 package com.company;

import java.util.Arrays;
import java.util.Scanner;

public class TicTacToe {
    public char[][] board;

    public TicTacToe() {
        board = new char[3][3];
        for (char[] row: board) {
            for (int i = 0; i < row.length; i++) {
                row[i] = ' ';
            }
        }
        startGame();
    }

    public void printBoard() {
        for (char[] row : board) {
            for (char letter :row) {
                System.out.print("[ " + letter + " ]" );
            }
            System.out.println();
        }
    }

    public boolean placeValue(int col, int row, char letter) {
        if (board[row][col] != ' ') {
            System.out.println("Space taken");
            return false;
        } else {
            board[row][col] = letter;
            return true;
        }
    }

    public boolean isGameWon() {

        //horizontal
        for (char[] row: board) {
            if(row[0] != ' ') {
                if (row[0] == row[1] && row[1] == row[2])
                    return true;
            }
        }

        //vertical
        for (int i = 0; i < 3; i++) {
            if (board[0][i] != ' ' && board[0][i] == board[1][i] && 

            board[1][i] == board[2][i])
                return true;
        }
        //diagonal
        if (board[0][0] != ' ' && board[1][1] == board[0][0] &&              board[1][1] == board[2][2]) return true;
        if (board[2][2] != ' ' && board[1][1] == board[0][0] && 

        board[1][1] == board[2][2]) return true;

        return false;
    }

    public void startGame() {
        String inputText = "";
        Scanner reader = new Scanner(System.in);
        printBoard();
        while (
                Arrays.asList(board).contains(' ')
                || !isGameWon()
                ) {
            System.out.println("Enter co ords and char 0 0 A: ");
            inputText = reader.nextLine().trim();
            placeValue(Integer.parseInt(String.valueOf(inputText.charAt(0))),
                    Integer.parseInt(String.valueOf(inputText.charAt(2))),
                    inputText.charAt(4));
            printBoard();
        }
        if(isGameWon()) {
            System.out.println("You Win!");
        } else {
            System.out.println("Draw!");
        }

    }
}

Monday, 1 February 2016

Challenge #249 [Easy] Playing the Stock Market

Tired after work and over thought it, then oh is that it. 

import java.util.Arrays;

public class Main {

    public static void main(String[] args) {
        //String inputValues = "19.35 19.30 18.88 18.93 18.95 19.03 19.00 18.97 18.97 18.98";
        String inputValues = "9.20 8.03 10.02 8.08 8.14 8.10 8.31 8.28 8.35 8.34 8.39 8.45 8.38 8.38 8.32 8.36 8.28 8.28 8.38 8.48 8.49 8.54 8.73 8.72 8.76 8.74 8.87 8.82 8.81 8.82 8.85 8.85 8.86 8.63 8.70 8.68 8.72 8.77 8.69 8.65 8.70 8.98 8.98 8.87 8.71 9.17 9.34 9.28 8.98 9.02 9.16 9.15 9.07 9.14 9.13 9.10 9.16 9.06 9.10 9.15 9.11 8.72 8.86 8.83 8.70 8.69 8.73 8.73 8.67 8.70 8.69 8.81 8.82 8.83 8.91 8.80 8.97 8.86 8.81 8.87 8.82 8.78 8.82 8.77 8.54 8.32 8.33 8.32 8.51 8.53 8.52 8.41 8.55 8.31 8.38 8.34 8.34 8.19 8.17 8.16";
        String[] splitValues = inputValues.split(" ");
        System.out.println(Arrays.toString(splitValues));
        double[] stockTrades = new double[splitValues.length];

        for (int i = 0; i < splitValues.length; i++) {
            stockTrades[i] = Double.parseDouble(splitValues[i]);
        }

        double maxDiff = Double.MIN_VALUE, fValue = 0, lValue = 0;
        for (int i = 0; i < stockTrades.length; i++) {
            for (int j = i+2; j < stockTrades.length; j++) {
                if ((stockTrades[j] - stockTrades[i]) > maxDiff) {
                    fValue = stockTrades[i];
                    lValue = stockTrades[j];
                    maxDiff = lValue - fValue;
                }
            }
        }

        System.out.println(fValue +  " " + lValue);
    }
}


Also properly broke my lego picture maker, thank you version control! :D I did learn that with git you can check out a commit, so when I tried to commit it back to the branch, head was detached, easy way to fix. Create branch, check out new branch, commit changes, merge branches and done.