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'));
    }
}

No comments:

Post a Comment