Simple Encryption #1 – Alternating Split

مساله:

برای ساخت رشته رمزگذاری شده:
هر کاراکتر دوم را از رشته بردارید، سپس کاراکترهای دیگر را که هر کاراکتر دوم نیستند ، بردارید و آنها را به عنوان رشته جدید به هم متصل کنید.
این کار را n بار انجام دهید!

برای مثال:

"This is a test!", 1 -> "hsi  etTi sats!"
"This is a test!", 2 -> "hsi  etTi sats!" -> "s eT ashi tist!"

Description:

For building the encrypted string:
Take every 2nd char from the string, then the other chars, that are not every 2nd char, and concat them as new String.
Do this n times!

Examples:

"This is a test!", 1 -> "hsi  etTi sats!"
"This is a test!", 2 -> "hsi  etTi sats!" -> "s eT ashi tist!"

Write two methods:

String encrypt(final String text, final int n)
String decrypt(final String encryptedText, final int n)

For both methods:
If the input-string is null or empty return exactly this value!
If n is <= 0 then return the input text.


public class Kata {

  public static String encrypt(final String text, final int n) {
    String result = text;
    int count = n;
    while (count > 0) {
      String oneTh = "", twoTh = "";
      String[] arr = result.split("");
      for (int i = 0; i < result.length(); i++) {
        if (i % 2 == 0)
          oneTh += arr[i];
        else
          twoTh += arr[i];
      }
      result = twoTh + oneTh;
      count--;
    }
    return result;   
  }
  
  public static String decrypt(final String encryptedText, final int n) {
    String result = encryptedText;
    int count = n;
    while (count > 0) {
      int length = result.length();
      int center = length / 2;
      String first = result.substring(0, center);
      String second = result.substring(center);
      result = "";
      for (int i = 0; i < center; i++) {
        result += second.split("")[i];
        result += first.split("")[i];
      }
      if (second.length() > center)
        result += second.split("")[second.length() - 1];
      count--;
    }
    return result;
  }
  
}
public class Kata {

  public static String encrypt(final String text, int n) {
                if (n <= 0 || text == null || text.isEmpty()) {
                        return text;
                }

                StringBuilder firstPart = new StringBuilder();
                StringBuilder secondPart = new StringBuilder();
                for (int i = 0; i < text.length(); i++) {
                        char aChar = text.charAt(i);
                        if (i % 2 == 1) {
                                firstPart.append(aChar);
                        } else {
                                secondPart.append(aChar);
                        }
                }

                return encrypt(firstPart.append(secondPart).toString(), --n);
        }

        public static String decrypt(final String encryptedText, int n) {
                if (n <= 0 || encryptedText == null || encryptedText.isEmpty()) {
                        return encryptedText;
                }

                StringBuilder text = new StringBuilder();
                final int half = encryptedText.length() / 2;
                for (int i = 0; i < half; i++) {
                        text.append(encryptedText.charAt(half + i)).append(encryptedText.charAt(i));
                }
                if (encryptedText.length() % 2 == 1) {
                        text.append(encryptedText.charAt(encryptedText.length() - 1));
                }

                return decrypt(text.toString(), --n);
        }
  
}
public class Kata {

  public static String encrypt(final String text, final int n) {
    String out=text;
    for(int i=0; i<n; i++) {
      String temp="";
      for(int j=1; j<out.length(); j+=2) temp+=out.charAt(j);
      for(int j=0; j<out.length(); j+=2) temp+=out.charAt(j);
      out=temp;
    }
    return out;   
  }
  
  public static String decrypt(final String encryptedText, final int n) {
    String out=encryptedText;
    for(int i=0; i<n; i++) {
      String temp="";
      for(int j=0; j<out.length()/2; j+=1) temp+=""+out.charAt(out.length()/2+j)+out.charAt(j);
      out=temp+encryptedText.substring(encryptedText.length()/2*2);
    }
    return out;
  }
  
}
import java.util.*;

public class Kata {

  public static String encrypt(final String text, final int n) {
    if (text == null || text.isEmpty() || n < 1) return text;
    
    return encrypt(text.replaceAll("(.)(.)?", "$2") + text.replaceAll("(.)(.)?", "$1"), n - 1);
  }

  public static String decrypt(final String text, final int n) {
    if (text == null || text.isEmpty() || n < 1) return text;

    StringBuilder output = new StringBuilder();
    Iterator<String> first = Arrays.asList(text.substring(0, text.length()/2).split("")).iterator();
    Iterator<String> last = Arrays.asList(text.substring(text.length()/2).split("")).iterator();
    
    while (first.hasNext() || last.hasNext()) {
      if (last.hasNext()) output.append(last.next());
      if (first.hasNext()) output.append(first.next());
    }
    
    return decrypt(output.toString(), n - 1);
  }
  
}
import java.util.stream.IntStream;
import java.util.stream.Collectors;
import java.util.Arrays;

public class Kata {

  public static String encrypt(final String text, final int n) {
    if(text == null || n < 1) return text;
    String first = IntStream.range(0, text.length())
                            .filter(i -> i%2 == 1)
                            .mapToObj(i -> text.charAt(i))
                            .map(Object::toString)
                            .collect(Collectors.joining(""));
    String second = IntStream.range(0, text.length())
                              .filter(i -> i%2 == 0)
                              .mapToObj(i -> text.charAt(i))
                              .map(Object::toString)
                              .collect(Collectors.joining(""));
    return encrypt(first + second, n - 1);
}
  
  
  public static String decrypt(final String text, final int n) {
    if(text == null || n < 1) return text;
    String decrypted = IntStream.range(0, text.length())
                              .mapToObj(i -> (i%2 == 1) ? text.charAt(i/2) : text.charAt(text.length()/2 + i/2))
                              .map(Object::toString)
                              .collect(Collectors.joining(""));
   return decrypt(decrypted, n - 1);                     
  }
}
public class Kata {

  public static String encrypt(final String text, final int n) {
    if (n<=0) return text;
      StringBuilder str = new StringBuilder();
      for (int i=1; i<text.length(); i+=2)
        str.append(text.substring(i,i+1));
      for (int i=0; i<text.length(); i+=2)
        str.append(text.substring(i,i+1));
    return (n==1)? str.toString() : encrypt(str.toString(), n-1);
  }
  
  public static String decrypt(final String entext, final int n) {
    if (n<=0) return entext;
    StringBuilder str = new StringBuilder();
    str.append(entext.substring(0,entext.length()/2));
    for (int i=entext.length()/2, j=0; i<entext.length(); i++, j+=2)
      str.insert(j,entext.substring(i,i+1)); 
    return (n==1)? str.toString() : decrypt(str.toString(),n-1);
  }
  
}
public class Kata {

  public static String encrypt(final String text, final int n) {
    if(text == null || text == "" || n <= 0)return text;
    int half = text.length()/2;
    StringBuilder front = new StringBuilder(half);
    StringBuilder back = new StringBuilder(half);
    for(int i = 0;i<text.length();i++){
      if((i & 1) == 1){
        front.append(text.charAt(i));
      }else{
        back.append(text.charAt(i));
      }
    }
    return encrypt(front.append(back.toString()).toString(),n-1);
  }
  
  public static String decrypt(final String text, final int n) {
    if(text == null || text == "" || n <= 0)return text;
    int half = text.length()/2;
    StringBuilder result = new StringBuilder(text.length());
    int x = 0,y = 0;
    for(int i = 0;i<text.length();i++){
      if((i & 1) == 1){
        result.append(text.charAt(x++));
      }else{
        result.append(text.charAt(half + y++));
      }
    }
    return decrypt(result.toString(),n-1);
  }
  
}

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *