文档章节

Codewar-010 Rotate Array

圣洁之子
 圣洁之子
发布于 2016/11/24 07:39
字数 639
阅读 8
收藏 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?

© 著作权归作者所有

共有 人打赏支持
上一篇: Codewar-011 How Much?
下一篇: POJ 1010
圣洁之子
粉丝 6
博文 329
码字总数 97282
作品 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
2018/08/09
0
0
Leetcode 189. Rotate Array

文章作者:Tyan 博客:noahsnail.com | CSDN | 简书 1. Description 2. Solution Version 1 Version 2 Version 3 Reference https://leetcode.com/problems/rotate-array/description/......

SnailTyan
2018/10/13
0
0
Rotate Array(leetCode189)

Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: and k = 3Output: Explanation:rotate 1 steps to the right: rotate 2 steps to ......

woshixin
2018/11/19
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

没有更多内容

加载失败,请刷新页面

加载更多

Cookie 显示用户上次访问的时间

import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;import javax.serv......

gwl_
今天
1
0
网络编程

第14天 网络编程 今日内容介绍  网络通信协议  UDP通信  TCP通信 今日学习目标  能够辨别UDP和TCP协议特点  能够说出UDP协议下两个常用类名称  能够说出TCP协议下两个常用类名称...

stars永恒
今天
1
0
二进制相关

二进制 众所周知计算机使用的是二进制,数字的二进制是如何表示的呢? 实际就是逢二进一。比如 2 用二进制就是 10。那么根据此可以推算出 5的二进制等于 10*10+1 即为 101。 在计算机中,负数以...

NotFound403
昨天
3
0
day22:

1、写一个getinterface.sh 脚本可以接受选项[i,I],完成下面任务: 1)使用格式:getinterface.sh [-i interface | -I ip] 2)当用户使用-i选项时,显示指定网卡的IP地址;当用户使用-I选项...

芬野de博客
昨天
2
0
Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现

自Spring Cloud Alibaba发布第一个Release以来,就备受国内开发者的高度关注。虽然Spring Cloud Alibaba还没能纳入Spring Cloud的主版本管理中,但是凭借阿里中间件团队的背景,还是得到不少...

程序猿DD
昨天
4
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部