文档章节

Codewar-010 Rotate Array

圣洁之子
 圣洁之子
发布于 2016/11/24 07:39
字数 639
阅读 6
收藏 0

Create a method named "rotate" that returns a given array with the elements inside the array rotated n spaces.

If n is greater than 0 it should rotate the array to the right. If n is less than 0 it should rotate the array to the left. If n is 0, then it should return the array unchanged.

Example:

Object[] data = new Object[]{1, 2, 3, 4, 5};

rotate(data, 1)    =>    {5, 1, 2, 3, 4}
rotate(data, 2)    =>    {4, 5, 1, 2, 3}
rotate(data, 3)    =>    {3, 4, 5, 1, 2}
rotate(data, 4)    =>    {2, 3, 4, 5, 1}
rotate(data, 5)    =>    {1, 2, 3, 4, 5}

rotate(data, 0)    =>    {1, 2, 3, 4, 5}

rotate(data, -1)    =>    {2, 3, 4, 5, 1}
rotate(data, -2)    =>    {3, 4, 5, 1, 2}
rotate(data, -3)    =>    {4, 5, 1, 2, 3}
rotate(data, -4)    =>    {5, 1, 2, 3, 4}
rotate(data, -5)    =>    {1, 2, 3, 4, 5}

Furthermore the method should take ANY array of objects and perform this operation on them:

rotate(new Object[]{'a', 'b', 'c'}, 1)        =>    {'c', 'a', 'b'}
rotate(new Object[]{1.0, 2.0, 3.0}, 1)        =>    {3.0, 1.0, 2.0}
rotate(new Object[]{true, true, false}, 1)    =>    {false, true, true}

Finally the rotation shouldn't be limited by the indices available in the array. Meaning that if we exceed the indices of the array it keeps rotating.

Example:

Object[] data = new Object[]{1, 2, 3, 4, 5}

rotate(data, 7)        =>    {4, 5, 1, 2, 3}
rotate(data, 11)       =>    {5, 1, 2, 3, 4}
rotate(data, 12478)    =>    {3, 4, 5, 1, 2}
package codewars;

public class Rotator {

	public Object[] rotate(Object[] data, int n) {

		if (n == 0) {
			return data;
		} else {
			Object[] result = new Object[data.length];
			int steps = n % data.length;
			for (int i = 0; i < data.length; i++) {

				result[i] = data[(i - steps >= 0 ? (i - steps) % data.length
						: i - steps + data.length)];
			}

			return result;
		}
	}
}
package codewars;
import static org.junit.Assert.*;
import org.junit.*;

public class RotatorTest {
	private Rotator rotator;
  
  @Before
  public void setUp() {
  	this.rotator = new Rotator();
  }
  
  @After
  public void tearDown() {
  	this.rotator = null;
  }
  
  @Test
  public void testRotateOne_ArrayOfFive() {
  	assertArrayEquals(new Object[]{5, 1, 2, 3, 4},
    	rotator.rotate(new Object[]{1, 2, 3, 4, 5}, 1));
  }
 
 	@Test
  public void testRotateTwo_ArrayOfFive() {
  	assertArrayEquals(new Object[]{4, 5, 1, 2, 3},
    	rotator.rotate(new Object[]{1, 2, 3, 4, 5}, 2));
  }
  
  @Test
  public void testRotateThree_ArrayOfFive() {
  	assertArrayEquals(new Object[]{3, 4, 5, 1, 2},
    	rotator.rotate(new Object[]{1, 2, 3, 4, 5}, 3));
  }
  
  @Test
  public void testRotateFour_ArrayOfFive() {
  	assertArrayEquals(new Object[]{2, 3, 4, 5, 1},
    	rotator.rotate(new Object[]{1, 2, 3, 4, 5}, 4));
  }
 	
  @Test
  public void testRotateFive_ArrayOfFive() {
  	assertArrayEquals(new Object[]{1, 2, 3, 4, 5},
    	rotator.rotate(new Object[]{1, 2, 3, 4, 5}, 5));
  }
  
  @Test
  public void testRotateSix_ArrayOfFive() {
  	assertArrayEquals(new Object[]{5, 1, 2, 3, 4},
    	rotator.rotate(new Object[]{1, 2, 3, 4, 5}, 6));
  }
  
  @Test
  public void testRotateNegOne_ArrayOfFive() {
  	assertArrayEquals(new Object[]{2, 3, 4, 5, 1},
    	rotator.rotate(new Object[]{1, 2, 3, 4, 5}, -1));
  }
  
  @Test
  public void testRotateNegTwo_ArrayOfFive() {
  	assertArrayEquals(new Object[]{3, 4, 5, 1, 2},
    	rotator.rotate(new Object[]{1, 2, 3, 4, 5}, -2));
  }
  
  @Test
  public void testRotateNegThree_ArrayOfFive() {
  	assertArrayEquals(new Object[]{4, 5, 1, 2, 3},
    	rotator.rotate(new Object[]{1, 2, 3, 4, 5}, -3));
  }
  
  @Test
  public void testRotateNegFour_ArrayOfFive() {
  	assertArrayEquals(new Object[]{5, 1, 2, 3, 4},
    	rotator.rotate(new Object[]{1, 2, 3, 4, 5}, -4));
  }
  
  @Test
  public void testRotateNegFive_ArrayOfFive() {
  	assertArrayEquals(new Object[]{1, 2, 3, 4, 5},
    	rotator.rotate(new Object[]{1, 2, 3, 4, 5}, -5));
  }
  
  @Test
  public void testRotateNegSix_ArrayOfFive() {
  	assertArrayEquals(new Object[]{2, 3, 4, 5, 1},
    	rotator.rotate(new Object[]{1, 2, 3, 4, 5}, -6));
  }
}

https://www.codewars.com/kata/5469e0798a3502f4a90005c9/train/java

 

Jake from Codewars

Active in the last 15m

 

7 kyu - Rank earned!

 

A whole world awaits. You’re making coding moves – keep it up. With weekly practice, persistent focus and a kata a day, comes Mastery.

Why don't you share your awesomeness?

© 著作权归作者所有

共有 人打赏支持
圣洁之子
粉丝 6
博文 306
码字总数 92734
作品 0
深圳
后端工程师
codewar-010: Directions Reduction

https://www.codewars.com/kata/550f22f4d758534c1100025a/train/java

圣洁之子
2016/12/08
62
0
189. Rotate Array - LeetCode

Question 189. Rotate Array Solution 题目大意:数组中最后一个元素移到第一个,称动k次 思路:用笨方法,再复制一个数组 Java实现: 别人的实现:...

yysue
08/09
0
0
每天一个js小算法(日更 21天)

今天写代码的时候 处理定时器相关代码的时候遇到一个很基本的算法 卡住了很久 很气 相当气 然后开始思考为什么自己这么傻? 为什么自己写不出来然后意象不到的焦虑感油然而生 我怎么连这么简...

zz77zz
07/23
0
0
LeetCode:Rotate Array - 循环平移数组

1、题目名称 Rotate Array(循环平移数组) 2、题目地址 https://leetcode.com/problems/rotate-array/ 3、题目内容 英文:Rotate an array of n elements to the right by k steps. 中文:将......

北风其凉
2015/08/09
0
0
php二维数组转换为多维数组无限分类

这样的一个分类数组怎么用ui li无限分类表现出来,或是转换为多维数组表现 Array ( [list1] => Array ( [0] => Array ( [code] => - [name] => 设备1 ) [1] => Array ( [code] => 0 [name] =......

Evansin
2014/09/20
592
0

没有更多内容

加载失败,请刷新页面

加载更多

理解数据库的4种隔离级别

本文转自:http://m.blog.csdn.net/article/details?id=51924963 数据库事务的隔离级别有4种,由低到高分别为Read uncommitted 、Read committed 、Repeatable read 、Serializable 。而且,...

初雪之音
30分钟前
1
0
主流的消息队列MQ比较,详解MQ的4类应用场景

目前主流的MQ 1.ZeroMQ 号称最快的消息队列系统,尤其针对大吞吐量的需求场景。 扩展性好,开发比较灵活,采用C语言实现,实际上只是一个socket库的重新封装,如果做为消息队列使用,需要开发...

游人未归
今天
4
0
React 服务器渲染原理解析与实践

网盘下载地址 React 服务器渲染原理解析与实践 本套课程,讲解了React中SSR技术的整个搭建思路及流程,完整的从原理上讲清楚了SSR的概念,重点在于讲解编写SSR框架遇到的各种知识点,以及细节...

qq__2304636824
今天
4
0
Jenkins使用

clean install -Dmaven.test.skip=true 系统设置 全局工具配置 其他的配置类似 构建项目配置

1713716445
今天
2
0
多线程

1. 多线程概念。并发和并行的概念。 多线程指的是一段时间内cpu同时执行多个线程。一个程序至少运行>=1个进程,进程就是运行中的程序,而一个进程至少运行>=1个线程,线程是操作系统能调度的...

鱼想吃肉
今天
5
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部