Last active
December 12, 2015 02:39
-
-
Save torazuka/4700676 to your computer and use it in GitHub Desktop.
第7回オフラインリアルタイムどう書く(Java解答)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.junit.Assert.assertEquals; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.List; | |
import org.junit.Test; | |
/** | |
* 問題: http://qiita.com/items/4364285801d1c9f370a1 | |
*/ | |
public class SelectChair { | |
protected final char EMPTY = '-'; | |
public String solve(final String string) throws IllegalArgumentException { | |
String[] split = string.split(":"); | |
char[] chairs = new char[Integer.valueOf(split[0])]; | |
Arrays.fill(chairs, EMPTY); | |
char[] inOut = split[1].toCharArray(); | |
for (char each : inOut) { | |
if (Character.isUpperCase(each)) { | |
int recomended = searchChair(chairs); | |
chairs[recomended] = each; | |
} else { | |
int awayIndex = awayChair(chairs, each); | |
chairs[awayIndex] = EMPTY; | |
} | |
} | |
String result = ""; | |
for (char each : chairs) { | |
result += each; | |
} | |
return result; | |
} | |
protected int searchChair(final char[] chairs) { | |
List<Integer> secondary = new ArrayList<>(); | |
List<Integer> guttari = new ArrayList<>(); | |
for (int i = 0; i < chairs.length; i++) { | |
char tmpChair = chairs[i]; | |
if (tmpChair == EMPTY) { | |
if (chairs.length == 1) { | |
return i; | |
} else if ((i == 0 && EMPTY == chairs[i + 1]) | |
|| (i == chairs.length - 1 && EMPTY == chairs[i - 1])) { | |
return i; | |
} else if ((i == 0 && EMPTY != chairs[i + 1]) | |
|| (i == chairs.length - 1 && EMPTY != chairs[i - 1])) { | |
secondary.add(i); | |
} else if (EMPTY == chairs[i + 1] && EMPTY == chairs[i - 1]) { | |
return i; | |
} else if (EMPTY == chairs[i + 1] || EMPTY == chairs[i - 1]) { | |
secondary.add(i); | |
} else { | |
guttari.add(i); | |
} | |
} | |
} | |
if (secondary.isEmpty() == false) { | |
return Collections.min(secondary); | |
} else if (guttari.isEmpty() == false) { | |
return Collections.min(guttari); | |
} | |
return new IllegalArgumentException("There are no seats left."); | |
} | |
protected int awayChair(final char[] chairs, final char c) | |
throws IllegalArgumentException { | |
for (int i = 0; i < chairs.length; i++) { | |
if (chairs[i] == Character.toUpperCase(c)) { | |
return i; | |
} | |
} | |
throw new IllegalArgumentException(Character.toUpperCase(c) | |
+ " hasn't seated."); | |
} | |
@Test | |
public void testSelectChairs() throws Exception { | |
/* 1 */test("6:NABEbBZn", "-ZAB-E"); | |
/* 2 */test("1:A", "A"); | |
/* 3 */test("1:Aa", "-"); | |
/* 4 */test("2:AB", "AB"); | |
/* 5 */test("2:AaB", "B-"); | |
/* 6 */test("2:AZa", "-Z"); | |
/* 7 */test("2:AZz", "A-"); | |
/* 8 */test("3:ABC", "ACB"); | |
/* 9 */test("3:ABCa", "-CB"); | |
/* 10 */test("4:ABCD", "ADBC"); | |
/* 11 */test("4:ABCbBD", "ABDC"); | |
/* 12 */test("4:ABCDabcA", "-D-A"); | |
/* 13 */test("5:NEXUS", "NUESX"); | |
/* 14 */test("5:ZYQMyqY", "ZM-Y-"); | |
/* 15 */test("5:ABCDbdXYc", "AYX--"); | |
/* 16 */test("6:FUTSAL", "FAULTS"); | |
/* 17 */test("6:ABCDEbcBC", "AECB-D"); | |
/* 18 */test("7:FMTOWNS", "FWMNTSO"); | |
/* 19 */test("7:ABCDEFGabcdfXYZ", "YE-X-GZ"); | |
/* 20 */test("10:ABCDEFGHIJ", "AGBHCIDJEF"); | |
} | |
protected void test(String in, String out) throws Exception { | |
SelectChair sc = new SelectChair(); | |
assertEquals(out, sc.solve(in)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment