Created
November 14, 2012 12:43
-
-
Save torazuka/4071888 to your computer and use it in GitHub Desktop.
第五回オフライリアルタイムどう書く の部分問題の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 static org.junit.Assert.assertEquals; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Iterator; | |
import java.util.List; | |
import org.junit.Test; | |
class Card implements Comparable<Card> { | |
String suit; | |
String rank; | |
Card(char s, char r) { | |
suit = String.valueOf(s); | |
rank = String.valueOf(r); | |
} | |
public boolean isStrong(int rank) { | |
int n = this.convertRank(); | |
if (rank < n) { | |
return true; | |
} | |
return false; | |
} | |
@Override | |
public String toString() { | |
return suit + rank; | |
} | |
public int convertRank() { | |
if (rank.equals("T")) { | |
return 10; | |
} | |
if (rank.equals("J")) { | |
return 11; | |
} | |
if (rank.equals("Q")) { | |
return 12; | |
} | |
if (rank.equals("K")) { | |
return 13; | |
} | |
if (rank.equals("A")) { | |
return 14; | |
} | |
if (rank.equals("2")) { | |
return 15; | |
} | |
if (rank.equals("o")) { | |
return 16; | |
} | |
return Integer.valueOf(rank); | |
} | |
@Override | |
public int compareTo(Card o) { | |
int n = this.convertRank(); | |
int m = o.convertRank(); | |
if (m < n) { | |
return 1; | |
} | |
if (n < m) { | |
return -1; | |
} | |
return 0; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (obj instanceof Card) { | |
Card c = (Card) obj; | |
if (rank.equals(c.rank) && suit.equals(c.suit)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} | |
/** | |
* 問題(部分問題の解答): http://nabetani.sakura.ne.jp/hena/ord5dahimi/ | |
*/ | |
public class DahimiPart { | |
protected String solve(String input) { | |
String[] split = input.split(","); | |
if (split.length < 2) { | |
return "-"; | |
} | |
List<Card> spaceList = getCards(split[0]); | |
List<Card> hand = getCards(split[1]); | |
int spaceRank = getMinRank(spaceList).convertRank(); | |
return getResult(spaceRank, hand); | |
} | |
protected String getResult(int spaceRank, List<Card> hand) { | |
List<Card> cards = new ArrayList<>(); | |
for (Card card : hand) { | |
if (card.isStrong(spaceRank)) { | |
cards.add(card); | |
} | |
} | |
if (cards.size() == 0) { | |
return "-"; | |
} | |
String result = cards.get(0).toString(); | |
for (int i = 1; i < cards.size(); i++) { | |
result += "," + cards.get(i).toString(); | |
} | |
return result; | |
} | |
protected Card getMinRank(List<Card> space) { | |
if (hasJoker(space)) { | |
if (space.size() == 1) { | |
return new Card('J', 'o'); | |
} | |
space.remove(new Card('J', 'o')); | |
} | |
return Collections.min(space); | |
} | |
protected boolean hasJoker(List<Card> hand) { | |
return 0 < Collections.frequency(hand, new Card('J', 'o')) ? true | |
: false; | |
} | |
protected List<Card> getCards(String hand) { | |
List<Card> result = new ArrayList<>(); | |
for (int i = 0; i < hand.length(); i += 2) { | |
Card c = new Card(hand.charAt(i), hand.charAt(i + 1)); | |
result.add(c); | |
} | |
return result; | |
} | |
public static void main(String[] args) { | |
DahimiPart dahimi = new DahimiPart(); | |
System.out.println(dahimi.solve("JoC8,H6D7C5S9CQH9STDTCAD9S5DAS2CT")); | |
} | |
// 解答や期待値の文字列をソートする | |
protected String answerSort(String string) { | |
String result = ""; | |
String[] split = string.split(","); | |
if (split.length == 0) { | |
// カンマがない場合は'-'、あるいは1枚だけ | |
return string; | |
} | |
List<String> tmpOuter = new ArrayList<>(); | |
int innerUnit = split[0].length() / 2; | |
for (String str : split) { | |
List<String> tmpInner = new ArrayList<>(); | |
for (int i = 0; i < innerUnit * 2; i += 2) { | |
String s = new Card(str.charAt(i), str.charAt(i + 1)) | |
.toString(); | |
tmpInner.add(s); | |
} | |
Collections.sort(tmpInner); | |
StringBuilder inner = new StringBuilder(); | |
for (String s : tmpInner) { | |
inner.append(s); | |
} | |
tmpOuter.add(new String(inner)); | |
} | |
Collections.sort(tmpOuter); | |
Iterator<String> ite = tmpOuter.iterator(); | |
if (ite.hasNext()) { | |
result += ite.next() + ","; | |
} | |
return result.substring(result.length() - 1); | |
} | |
protected void test(String problem, String expect) { | |
String expectSort = answerSort(expect); | |
DahimiPart dahimi = new DahimiPart(); | |
String answer = dahimi.solve(problem); | |
String answerStr = answerSort(answer); | |
assertEquals(expectSort, answerStr); | |
} | |
@Test | |
public void testSolve() throws Exception { | |
/* #1 */test("DJ,", "-"); | |
/* #2 */test("H7,HK", "HK"); | |
/* #3 */test("S3,D4D2", "D4,D2"); | |
/* #4 */test("S9,C8H4", "-"); | |
/* #5 */test("S6,S7STCK", "CK,ST,S7"); | |
/* #6 */test("H4,SAS8CKH6S4", "S8,CK,H6,SA"); | |
/* #7 */test("ST,D6S8JoC7HQHAC2CK", "Jo,C2,CK,HA,HQ"); | |
/* #8 */test("SA,HAD6S8S6D3C4H2C5D4CKHQS7D5", "H2"); | |
/* #9 */test("S2,D8C9D6HQS7H4C6DTS5S6C7HAD4SQ", "-"); | |
/* #10 */test("Jo,HAC8DJSJDTH2", "-"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment