https://www.codewars.com/kata/first-variation-on-caesar-cipher/java
The action of a Caesar cipher is to replace each plaintext letter with a different one a fixed number of places up or down the alphabet.
This program performs a variation of the Caesar shift. The shift increases by 1 for each character (on each iteration).
If the shift is initially 1, the first character of the message to be encoded will be shifted by 1, the second character will be shifted by 2, etc...
Coding: Parameters and return of function "movingShift"
param s: a string to be coded
param shift: an integer giving the initial shift
The function "movingShift" first codes the entire string and then returns an array of strings containing the coded string in 5 parts (five parts because, to avoid more risks, the coded message will be given to five runners, one piece for each runner).
If possible the message will be equally divided by message length between the five runners. If this is not possible, parts 1 to 5 will have subsequently non-increasing lengths, such that parts 1 to 4 are at least as long as when evenly divided, but at most 1 longer. If the last part is the empty string this empty string must be shown in the resulting array.
For example, if the coded message has a length of 17 the five parts will have lengths of 4, 4, 4, 4, 1. The parts 1, 2, 3, 4 are evenly split and the last part of length 1 is shorter. If the length is 16 the parts will be of lengths 4, 4, 4, 4, 0. Parts 1, 2, 3, 4 are evenly split and the fifth runner will stay at home since his part is the empty string. If the length is 11, equal parts would be of length 2.2, hence parts will be of lengths 3, 3, 3, 2, 0.
You will also implement a "demovingShift" function with two parameters
Decoding: parameters and return of function "demovingShift"
1) an array of strings: s (possibly resulting from "movingShift", with 5 strings)
2) an int shift
"demovingShift" returns a string.
Example:
u = "I should have known that you would have a perfect answer for me!!!"
movingShift(u, 1) returns :
v = ["J vltasl rlhr ", "zdfog odxr ypw", " atasl rlhr p ", "gwkzzyq zntyhv", " lvz wp!!!"]
(quotes added in order to see the strings and the spaces, your program won't write these quotes, see Example Test Cases)
and demovingShift(v, 1) returns u.
#Ref:
Caesar Cipher : http://en.wikipedia.org/wiki/Caesar_cipher
Solution
import java.util.*;
public class CaesarCipher {
static char[]alpha = {' ','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
static char[]ALPHA = {' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
public static List<String> movingShift(String s, int shift) {
char[] list = s.toCharArray();
int [] array = new int[list.length];
int cnt=0;
for(int i=0; i<list.length; i++) {
if(list[i]==' ') {cnt++;}
else {
if(Character.isLowerCase(list[i])==true) {array[i] = Arrays.binarySearch(alpha, list[i]);}
else if(Character.isUpperCase(list[i])==true) {array[i] = Arrays.binarySearch(ALPHA, list[i]);}
else {continue;}
int b = cnt+shift+array[i];
if(b >= 26) {
b = ((cnt+shift+array[i])%26);
if(b == 0) {b=26;}
}
if(Character.isLowerCase(list[i])==true) {list[i]=alpha[b];}
else if(Character.isUpperCase(list[i])==true) {list[i]=ALPHA[b];}
cnt++;
}
}
int len = list.length / 4;
int fin = list.length % 4;
for(;len-1>=fin+4;) {len -=1; fin +=4;}
int a = list.length;
int cntlen=0;
int cntfin=0;
for(;a>0;) {
if(len>fin) {
if(a - len >= 0) {a-=len; cntlen++;}
else if(a-fin >= 0){a-=fin; cntfin++;}
}
else {
if(a - fin >= 0) {a-=fin; cntfin++;}
else if(a-len >= 0){a-=len; cntlen++;}
}
}
String listresult = String.valueOf(list);
List<String> listre = new ArrayList<String>();
int count = 0;
if(len>fin) {count = len;} else {count = fin;}
for(int i =0; i<list.length; )
{
if(len>fin) {
for(int j=0; j<cntlen; j++) {
listre.add(listresult.substring(i, len+i));
i+=len;
}
for(int k=0; k<cntfin; k++) {
listre.add(listresult.substring(i, fin+i));
i+=fin;
}
}
else {
for(int k=0; k<cntfin; k++) {
listre.add(listresult.substring(i, fin+i));
i+=fin;
}
for(int j=0; j<cntlen; j++) {
listre.add(listresult.substring(i, len+i));
i+=len;
}
}
}
if(cntlen+cntfin != 5) listre.add("");
return listre;
}
public static String demovingShift(List<String> s, int shift) {
String listobj = "";
for(int i=0; i<s.size(); i++) {listobj += s.get(i);}
char[] delist = listobj.toCharArray();
int [] array = new int[delist.length];
int cnt=0;
for(int i=0; i<delist.length; i++) {
if(delist[i]==' ') {cnt++;}
else {
if(Character.isLowerCase(delist[i])==true) {array[i] = Arrays.binarySearch(alpha, delist[i]);}
else if(Character.isUpperCase(delist[i])==true) {array[i] = Arrays.binarySearch(ALPHA, delist[i]);}
else {continue;}
int c = array[i]-shift-cnt;
if(c <= -26) {
c = ((array[i]-shift-cnt)%26);
if(c == 0) {c=-26;}
}
if(c<0) {c= 26+c;}
if(c == 0) {c=26;}
if(Character.isLowerCase(delist[i])==true) {delist[i]=alpha[c];}
else if(Character.isUpperCase(delist[i])==true) { delist[i]=ALPHA[c];}
cnt++;
}
}
String deresult = String.valueOf(delist);
return deresult;
}
}
코드리뷰는 나중에.