codewars069 - Simple Encryption #4 - Qwerty

Instructions:

https://www.codewars.com/kata/57f14afa5f2f226d7d0000f4/train/java

Solution:

public class Kata
{

	private static final String first = "qwertyuiop";
	private static final String second = "asdfghjkl";
	private static final String third = "zxcvbnm,.";

	private static Character lower(Character c) {
		if (c == '<') {
			return ',';
		} else if (c == '>') {
			return '.';
		} else if (Character.isUpperCase(c)) {
			return Character.toLowerCase(c);
		} else {
			return c;
		}
	}

	private static Character upper(Character c) {
		if (c == ',') {
			return '<';
		} else if (c == '.') {
			return '>';
		} else if (Character.isLowerCase(c)) {
			return Character.toUpperCase(c);
		} else {
			return c;
		}
	}

	private static Character rotate(final Character c, String key, int direction) {
		Character lower = lower(c);
		String region = null;
		int index = -1;
		if (first.indexOf(lower) != -1) {
			index = 0;
			region = first;
		} else if (second.indexOf(lower) != -1) {
			index = 1;
			region = second;
		} else if (third.indexOf(lower) != -1) {
			index = 2;
			region = third;
		}

		if (region == null) {
			return c;
		} else {
			int pos = region.indexOf(lower);
			int step = Integer.valueOf(String.valueOf(key.charAt(index)));
			pos = pos + step * direction;
			if (pos >= region.length()) {
				pos = pos % region.length();
			} else if (pos < 0) {
				pos = region.length() + pos;
			}
			if (c == lower) {
				return region.charAt(pos);
			} else {
				return upper(region.charAt(pos));
			}
		}
	}

	public static String encrypt(String text, int key) {
		String strkey = String.format("%03d", key);
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < text.length(); i++) {
			sb.append(rotate(text.charAt(i), strkey, 1));
		}
		return sb.toString();
	}

	public static String decrypt(String encryptedText, int key) {
		String strkey = String.format("%03d", key);
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < encryptedText.length(); i++) {
			sb.append(rotate(encryptedText.charAt(i), strkey, -1));
		}
		return sb.toString();
	}



}

Sample Tests:

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;

public class SolutionTest {
    @Test
    public void encryptExampleTests() {
        assertEquals("S", Kata.encrypt("A", 111));
        assertEquals("Smb", Kata.encrypt("Abc", 212));
        assertEquals("Wave", Kata.encrypt("Wave", 0)); // -> 000
        assertEquals("Tg.y", Kata.encrypt("Wave", 345));
        assertEquals(">fdd", Kata.encrypt("Ball", 134));
        assertEquals(">gff", Kata.encrypt("Ball", 444));
    
        assertEquals("Iaqh qh g iyhi,", Kata.encrypt("This is a test.", 348));
        assertEquals("Sr pgi jlpl Jr,lqlage Zlow Piapc I.skiaa dw. l.s ibnepizi.p ugi. de.se.f l arkwper.c", Kata.encrypt("Do the kata Kobayashi Maru Test. Endless fun and excitement when finding a solution.", 583));          
    }
    
    @Test
    public void decryptExampleTests() {
        assertEquals("A", Kata.decrypt("S", 111));
        assertEquals("Abc", Kata.decrypt("Smb", 212));
        assertEquals("Wave", Kata.decrypt("Wave", 0)); // -> 000
        assertEquals("Wave", Kata.decrypt("Tg.y", 345));
        assertEquals("Ball", Kata.decrypt(">fdd", 134));
        assertEquals("Ball", Kata.decrypt(">gff", 444));
    
        assertEquals("This is a test.", Kata.decrypt("Iaqh qh g iyhi,", 348));
        assertEquals("Do the kata Kobayashi Maru Test. Endless fun and excitement when finding a solution.", Kata.decrypt("Sr pgi jlpl Jr,lqlage Zlow Piapc I.skiaa dw. l.s ibnepizi.p ugi. de.se.f l arkwper.c", 583));
    }
}
相關文章
相關標籤/搜索