文档章节

codewars070 - Simple Encryption #1 - Alternating Split

圣洁之子
 圣洁之子
发布于 2017/04/21 16:41
字数 401
阅读 220
收藏 0

Instructions

https://www.codewars.com/kata/simple-encryption-number-1-alternating-split/train/java

Solution:

public class Kata {


	private static String alternatingSplit(final String text) {
		StringBuilder sb = new StringBuilder();
		final int len = text.length();
		final int step = len / 2;
		int pos = 1;
		for (int i = 0; i < step; i++) {
			sb.append(text.charAt(pos));
			pos += 2;
		}
		pos = 0;
		for (int i = 0; i < step; i++) {
			sb.append(text.charAt(pos));
			pos += 2;
		}
		if (len % 2 == 1) {
			sb.append(text.charAt(len - 1));
		}

		return sb.toString();
	}

	public static String encrypt(final String text, final int n) {
		if (text == null || "".equals(text.trim()) || n <= 0
				|| text.length() == 1) {
			return text;
		}
		String result = text;
		for (int i = 0; i < n; i++) {
			result = alternatingSplit(result);
		}
		return result;
	}

	private static String restore(final String encryptedText) {
		StringBuilder sb = new StringBuilder();
		final int len = encryptedText.length();
		final int step = len / 2;
		for (int i = 0; i < step; i++) {
			sb.append(encryptedText.charAt(step + i));
			sb.append(encryptedText.charAt(i));
		}
		if (len % 2 == 1) {
			sb.append(encryptedText.charAt(len - 1));
		}

		return sb.toString();
	}

	public static String decrypt(final String encryptedText, final int n) {
		if (encryptedText == null || "".equals(encryptedText.trim()) || n <= 0
				|| encryptedText.length() == 1) {
			return encryptedText;
		}
		String result = encryptedText;
		for (int i = 0; i < n; i++) {
			result = restore(result);
		}
		return result;
	}


  
}

Sample Tests:

import org.junit.Test;
import static org.junit.Assert.*;

public class ExampleTests {

  @Test
  public void testEncrypt() {
    // assertEquals("expected", "actual");
    assertEquals("This is a test!", Kata.encrypt("This is a test!", 0));
    assertEquals("hsi  etTi sats!", Kata.encrypt("This is a test!", 1));
    assertEquals("s eT ashi tist!", Kata.encrypt("This is a test!", 2));
    assertEquals(" Tah itse sits!", Kata.encrypt("This is a test!", 3));
    assertEquals("This is a test!", Kata.encrypt("This is a test!", 4));
    assertEquals("This is a test!", Kata.encrypt("This is a test!", -1));
    assertEquals("hskt svr neetn!Ti aai eyitrsig", Kata.encrypt("This kata is very interesting!", 1));
  }
    
  @Test
  public void testDecrypt() {
    // assertEquals("expected", "actual");
    assertEquals("This is a test!", Kata.decrypt("This is a test!", 0));
    assertEquals("This is a test!", Kata.decrypt("hsi  etTi sats!", 1));
    assertEquals("This is a test!", Kata.decrypt("s eT ashi tist!", 2));
    assertEquals("This is a test!", Kata.decrypt(" Tah itse sits!", 3));
    assertEquals("This is a test!", Kata.decrypt("This is a test!", 4));
    assertEquals("This is a test!", Kata.decrypt("This is a test!", -1));
    assertEquals("This kata is very interesting!", Kata.decrypt("hskt svr neetn!Ti aai eyitrsig", 1));
  }
    
  @Test
  public void testNullOrEmpty() {
    // assertEquals("expected", "actual");
    assertEquals("", Kata.encrypt("", 0));
    assertEquals("", Kata.decrypt("", 0));
    assertEquals(null, Kata.encrypt(null, 0));
    assertEquals(null, Kata.decrypt(null, 0));
  }

}

© 著作权归作者所有

圣洁之子
粉丝 8
博文 368
码字总数 110274
作品 0
深圳
后端工程师
私信 提问
Secured RESTful API that can be used by Web App

You seem to be confusing/merging two different concepts together. We start of talking about encrypting traffic (HTTPS) and then we start talking about different ways to manage a......

freeroad
2014/09/28
0
0
Kotlin String split 操作实践

内容 此文章展示kotlin中对String字符串的split操作,如果你有遇到这方面的需求,希望对你有用。 1. split + 正则 先看下系统函数的定义,接收两个函数: regex:表示一个不可变的正则表达式 ...

Pape
2017/11/20
0
0
Leetcode 693. Binary Number with Alternating Bits

文章作者:Tyan 博客:noahsnail.com | CSDN | 简书 1. Description 2. Solution Version 1 Version 2 Reference https://leetcode.com/problems/binary-number-with-alternating-bits/descr......

SnailTyan
2018/09/05
0
0
Nim如何读取二进制文件

参考资料:writing/reading binary file in Nim

路中鸟
2016/01/16
270
0
微软笔试题 《Image Encryption》

题目1 : Image Encryption 时间限制:10000ms单点时限:1000ms内存限制:256MB描述A fancy square image encryption algorithm works as follow: consider the image as an N x N matrix choose......

你假松假精
2016/09/07
21
0

没有更多内容

加载失败,请刷新页面

加载更多

Oracle SQL语法实例合集

如需转载请注明出处https://my.oschina.net/feistel/blog/3052024 目的:迅速激活Oracle SQL 参考:《Oracle从入门到精通》 ------------------------------------------------------------......

LoSingSang
今天
2
0
增加 PostgreSQL 服务进程的最大打开文件数

https://serverfault.com/questions/628610/increasing-nproc-for-processes-launched-by-systemd-on-centos-7 要在systemd的配置里加才行...

helloclia
今天
2
0
组合模式在商品分类列表中的应用

在所有的树形结构中最适合的设计模式就是组合模式,我们看看常用商品分类中如何使用。 先定义一个树形结构的商品接口 public interface TreeProduct { List<TreeProduct> allProducts(...

算法之名
今天
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部