new sandbox project
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
<configuration>
|
||||
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<!-- <logger name="org.springframework" level="WARN" /> -->
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
|
||||
</configuration>
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd" >
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,42 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
|
||||
xsi:schemaLocation="
|
||||
http://java.sun.com/xml/ns/javaee
|
||||
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
|
||||
id="WebApp_ID" version="3.0">
|
||||
|
||||
<display-name>Spring MVC Application</display-name>
|
||||
|
||||
<!-- Spring root -->
|
||||
<context-param>
|
||||
<param-name>contextClass</param-name>
|
||||
<param-value>
|
||||
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
|
||||
</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>org.baeldung.config</param-value>
|
||||
</context-param>
|
||||
|
||||
<listener>
|
||||
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
|
||||
</listener>
|
||||
|
||||
<!-- Spring child -->
|
||||
<servlet>
|
||||
<servlet-name>api</servlet-name>
|
||||
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
<servlet-mapping>
|
||||
<servlet-name>api</servlet-name>
|
||||
<url-pattern>/</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
<welcome-file-list>
|
||||
<welcome-file />
|
||||
</welcome-file-list>
|
||||
|
||||
</web-app>
|
||||
@@ -0,0 +1,72 @@
|
||||
package org.baeldung.codility;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CodilityTest1 {
|
||||
|
||||
@Before
|
||||
public final void before() {
|
||||
//
|
||||
}
|
||||
|
||||
// tests
|
||||
|
||||
@Test
|
||||
public final void whenSolutionIsCalculated1_thenCorrect() {
|
||||
final int[] A = new int[] { 1, 4, 3, 3, 1, 2 };
|
||||
final int solution = solution(A);
|
||||
Assert.assertEquals(4, solution);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenSolutionIsCalculated2_thenCorrect() {
|
||||
final int[] A = new int[] { 6, 4, 4, 6 };
|
||||
final int solution = solution(A);
|
||||
Assert.assertEquals(-1, solution);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenSolutionIsCalculated3_thenCorrect() {
|
||||
final int[] A = new int[100000];
|
||||
final Random random = new Random();
|
||||
for (final int index : A) {
|
||||
A[index] = random.nextInt();
|
||||
}
|
||||
|
||||
final long start = System.currentTimeMillis();
|
||||
final int solution = solution(A);
|
||||
final long end = System.currentTimeMillis();
|
||||
System.out.println("Time: " + (end - start));
|
||||
System.out.println(solution);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
final int solution(final int elements[]) {
|
||||
final Map<Integer, Integer> collector = new LinkedHashMap<Integer, Integer>();
|
||||
Integer currentValue = null;
|
||||
for (final int element : elements) {
|
||||
currentValue = collector.get(element);
|
||||
if (currentValue == null) {
|
||||
collector.put(element, 1);
|
||||
} else if (currentValue >= 1) {
|
||||
collector.put(element, ++currentValue);
|
||||
}
|
||||
}
|
||||
|
||||
for (final Map.Entry<Integer, Integer> entry : collector.entrySet()) {
|
||||
if (entry.getValue() == 1) {
|
||||
return entry.getKey();
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package org.baeldung.codility;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CodilityTest2 {
|
||||
|
||||
// tests
|
||||
|
||||
@Test
|
||||
public final void whenSolutionIsCalculated1_thenCorrect() {
|
||||
final int solution = solution(955);
|
||||
Assert.assertEquals(4, solution);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenSolutionIsCalculated2_thenCorrect() {
|
||||
final int solution = solution(102);
|
||||
Assert.assertEquals(-1, solution);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenSolutionIsCalculated3_thenCorrect() {
|
||||
final int solution = solution(2);
|
||||
Assert.assertEquals(-1, solution);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenSolutionIsCalculated4_thenCorrect() {
|
||||
final int solution = solution2("codilitycodilityco");
|
||||
Assert.assertEquals(8, solution);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
public final int solution(final int decimal) {
|
||||
final String binaryString = Integer.toBinaryString(decimal);
|
||||
int lastPeriod = -1;
|
||||
for (int period = 1; period < (binaryString.length() / 2 + 1); period++) {
|
||||
final Matcher m = Pattern.compile("(\\S{" + period + ",})(?=.*?\\1)").matcher(binaryString);
|
||||
final boolean found = m.find();
|
||||
if (found && m.groupCount() > 0) {
|
||||
lastPeriod = period;
|
||||
}
|
||||
if (!found) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return lastPeriod;
|
||||
}
|
||||
|
||||
public final int solution2(final String binaryString) {
|
||||
int lastPeriod = -1;
|
||||
for (int period = 1; period < (binaryString.length() / 2 + 1); period++) {
|
||||
final Matcher m = Pattern.compile("(\\S{" + period + ",})(?=.*?\\1)").matcher(binaryString);
|
||||
final boolean found = m.find();
|
||||
if (found && m.groupCount() > 0) {
|
||||
lastPeriod = period;
|
||||
}
|
||||
if (!found) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return lastPeriod;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
package org.baeldung.codility;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CodilityTest3 {
|
||||
|
||||
// tests
|
||||
|
||||
@Test
|
||||
public final void whenSolutionIsCalculated1_thenCorrect() {
|
||||
final int[] moves = new int[] { 1, 3, 2, 5, 4, 4, 6, 3, 2 };
|
||||
final int solution = solution(moves);
|
||||
Assert.assertEquals(7, solution);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
public int solution(final int[] moves) {
|
||||
final World world = new World();
|
||||
int nextMove = 0;
|
||||
for (final int move : moves) {
|
||||
switch (nextMove) {
|
||||
case 0:
|
||||
if (world.moveNorth(move)) {
|
||||
nextMove = 1;
|
||||
break;
|
||||
} else {
|
||||
return world.movesCount;
|
||||
}
|
||||
case 1:
|
||||
if (world.moveEast(move)) {
|
||||
nextMove = 2;
|
||||
break;
|
||||
} else {
|
||||
return world.movesCount;
|
||||
}
|
||||
case 2:
|
||||
if (world.moveSouth(move)) {
|
||||
nextMove = 3;
|
||||
break;
|
||||
} else {
|
||||
return world.movesCount;
|
||||
}
|
||||
case 3:
|
||||
if (world.moveWest(move)) {
|
||||
nextMove = 0;
|
||||
break;
|
||||
} else {
|
||||
return world.movesCount;
|
||||
}
|
||||
default:
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
}
|
||||
|
||||
return world.movesCount;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class World {
|
||||
int movesCount = 1;
|
||||
int currentX = 0;
|
||||
int currentY = 0;
|
||||
Map<Integer, Integer> minXAtY;
|
||||
Map<Integer, Integer> maxXAtY;
|
||||
Map<Integer, Integer> minYAtX;
|
||||
Map<Integer, Integer> maxYAtX;
|
||||
|
||||
public World() {
|
||||
minXAtY = new HashMap<>();
|
||||
maxXAtY = new HashMap<>();
|
||||
minYAtX = new HashMap<>();
|
||||
maxYAtX = new HashMap<>();
|
||||
}
|
||||
|
||||
final boolean moveNorth(final int steps) {
|
||||
if (isMoveNorthValid(steps)) {
|
||||
storeMoveNorth(steps);
|
||||
movesCount++;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
final boolean moveEast(final int steps) {
|
||||
if (isMoveEastValid(steps)) {
|
||||
storeMoveEast(steps);
|
||||
movesCount++;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
final boolean moveSouth(final int steps) {
|
||||
if (isMoveSouthValid(steps)) {
|
||||
storeMoveSouth(steps);
|
||||
movesCount++;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
final boolean moveWest(final int steps) {
|
||||
if (isMoveWestValid(steps)) {
|
||||
storeMoveWest(steps);
|
||||
movesCount++;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
private boolean isMoveNorthValid(final int steps) {
|
||||
int currentPosition = currentY;
|
||||
for (int i = 1; i <= steps; i++) {
|
||||
currentPosition += 1;
|
||||
if (minXAtY.get(currentPosition) != null && minXAtY.get(currentPosition) > currentX) {
|
||||
return false;
|
||||
}
|
||||
if (maxXAtY.get(currentPosition) != null && maxXAtY.get(currentPosition) < currentX) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isMoveEastValid(final int steps) { //
|
||||
int currentPosition = currentX;
|
||||
for (int i = 1; i <= steps; i++) {
|
||||
currentPosition += 1;
|
||||
if (minYAtX.get(currentPosition) != null && minYAtX.get(currentPosition) < currentY) {
|
||||
return false;
|
||||
}
|
||||
if (maxYAtX.get(currentPosition) != null && maxYAtX.get(currentPosition) > currentY) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isMoveSouthValid(final int steps) {
|
||||
int currentPosition = currentY;
|
||||
for (int i = 1; i <= steps; i++) {
|
||||
currentPosition -= 1;
|
||||
if (minXAtY.get(currentPosition) != null && minXAtY.get(currentPosition) < currentX) {
|
||||
return false;
|
||||
}
|
||||
if (maxXAtY.get(currentPosition) != null && maxXAtY.get(currentPosition) > currentX) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isMoveWestValid(final int steps) {
|
||||
int currentPosition = currentX;
|
||||
for (int i = 1; i <= steps; i++) {
|
||||
currentPosition -= 1;
|
||||
if (minYAtX.get(currentPosition) != null && minYAtX.get(currentPosition) > currentY) {
|
||||
return false;
|
||||
}
|
||||
if (maxYAtX.get(currentPosition) != null && maxYAtX.get(currentPosition) < currentY) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void storeMoveNorth(final int steps) {
|
||||
currentY = currentY + steps;
|
||||
final Integer currentMaxYAtThisLevel = maxYAtX.get(currentX);
|
||||
if (currentMaxYAtThisLevel == null || currentMaxYAtThisLevel == null && currentMaxYAtThisLevel < currentY) {
|
||||
maxYAtX.put(currentX, currentY);
|
||||
}
|
||||
}
|
||||
|
||||
private void storeMoveEast(final int steps) {
|
||||
currentX = currentX + steps;
|
||||
final Integer currentMaxXAtThisLevel = maxXAtY.get(currentY);
|
||||
if (currentMaxXAtThisLevel == null || currentMaxXAtThisLevel == null && currentMaxXAtThisLevel < currentX) {
|
||||
maxXAtY.put(currentY, currentX);
|
||||
}
|
||||
}
|
||||
|
||||
private void storeMoveSouth(final int steps) {
|
||||
currentY = currentY - steps;
|
||||
final Integer currentMinYAtThisLevel = minYAtX.get(currentX);
|
||||
if (currentMinYAtThisLevel == null || currentMinYAtThisLevel == null && currentMinYAtThisLevel > currentY) {
|
||||
minYAtX.put(currentX, currentY);
|
||||
}
|
||||
}
|
||||
|
||||
private void storeMoveWest(final int steps) {
|
||||
currentX = currentX - steps;
|
||||
final Integer currentMinXAtThisLevel = minXAtY.get(currentY);
|
||||
if (currentMinXAtThisLevel == null || currentMinXAtThisLevel == null && currentMinXAtThisLevel > currentX) {
|
||||
minXAtY.put(currentY, currentX);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
Reference in New Issue
Block a user