U E D R , A S I H C RSS

Classify By Anagram/Passion

Parser.java

~cpp 
package anagram;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

public class Parser {
	
	/**
	 * @param file
	 */
	public Parser(File file) throws FileNotFoundException {
		this(new FileInputStream(file));
	}

	Hashtable result;
	InputStream ins;
	
	/**
	 * @param in
	 */
	public Parser(InputStream in) {
		this.ins = in;
	}

	/**
	 * @param input
	 */
	public Parser(String input) {
		ByteArrayInputStream byteInputStream = new ByteArrayInputStream( input.getBytes() );
		this.ins = byteInputStream;
	}
	
	/**
	 * 입력을 파싱해서 결과를 분석하는 함수
	 * 
	 */
	public void parse() throws IOException {
		result = new Hashtable();
		
		List lines = getItemLines();
		String item;
		String itemKey;
		for(int i=0 ; i<lines.size() ; i++)
		{
			item = (String)lines.get(i);
			
			itemKey = sortString(item);
			
			List itemList  = (List)result.get(itemKey);
				
			if (isCreated(itemList))
			{
				itemList = createNewEntry(itemKey);
			}
			
			itemList .add(item);
		}
	}

	private List createNewEntry(String itemKey) {
		List itemList;
		itemList = new ArrayList();
		result.put(itemKey, itemList);
		return itemList;
	}

	private boolean isCreated(List itemList) {
		return itemList == null;
	}

	/**
	 * 입력된 Item 의 갯수를 얻는 함수
	 * @return
	 */
	public int getItemCount() throws IOException {
		List lines = getItemLines();
		return lines.size();
	}

	/**
	 * 입력된 Item의 목록을 얻는 함수
	 * @return
	 */
	private List getItemLines() throws IOException {
		ArrayList lineList= new ArrayList();

		BufferedReader in = new BufferedReader(new InputStreamReader(ins));
		
		String line;
		
		while((line = in.readLine()) != null)
		{
			if (isValidLine(line))
				lineList.add(line);
		}
		
		return lineList;
	}

	private boolean isValidLine(String line) {
		return line.trim().length() > 0;
	}

	/**
	 * @return
	 */
	public int  getCount() {
		return result.size();
	}

	/**
	 * @param input
	 * @return
	 */
	public static String sortString(String input) {
		char tmpChar;
		StringBuffer buffer = new StringBuffer(input);
		for(int i=0; i<buffer.length()-1 ; i++)
		{
			for(int j=i+1; j<buffer.length() ; j++)
			{
				if (buffer.charAt(i) > buffer.charAt(j))
				{
					tmpChar = buffer.charAt(i);
					buffer.setCharAt(i, buffer.charAt(j));
					buffer.setCharAt(j, tmpChar);
				}
			}
		}
		
		return buffer.toString();
	}

	/**
	 * @param sortedItem
	 * @return
	 */
	public int getContainsCount(String itemKey) {
		List list = (List)result.get(itemKey);
		
		if (list == null)
			return 0;
		else		
			return list.size();
	}
	
	public void printResult(PrintStream out)
	{
		Enumeration enum = result.elements();
		List list;
		Iterator iterator;
		for(;enum.hasMoreElements();)
		{
			list = (List)enum.nextElement();
			iterator = list.iterator();
			for(;iterator.hasNext();)
			{
				out.print((String)iterator.next());
				out.print(" ");
			}
			out.println();
		}
	}
	
	public static void main(String[] args) throws IOException
	{
		long start = System.currentTimeMillis();

		String filename = "inputFile3.txt";
		InputStream in = new FileInputStream(filename);
		Parser parser = new Parser(System.in);
		parser.parse();
		 
		PrintStream out = new PrintStream(new BufferedOutputStream( new FileOutputStream( args[0] )));
		parser.printResult(out);
		
		long end = System.currentTimeMillis();
		
		System.out.println("time : "+(end-start)+"millis");
		
	}
}

AnagramTest.java

~cpp 
package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import junit.framework.TestCase;
import anagram.Parser;

/**
 * @author lgk9
 */
public class AnagramTest extends TestCase {
	
	String input;
	String input2;
	String input3;
	String input4;
	
	private Parser parser;
	private Parser parser2;
	 
	public void setUp()
	{
		input = "abc";
		input2 = "cde";
		input3 = "abc\nbcde";
		input4 = "abc\naabb\nacb\ncdd\ncba\nbaab\nbac\ndcd\nxds\n";
	}
	
	public void testOneLine1() throws IOException
	{
		parser = new  Parser(input);
		parser.parse();
		
		assertEquals(1, parser.getCount());
		
	}

	public void testOneLine2() throws IOException
	{
		parser = new  Parser(input2);
		parser.parse();
		
		assertEquals(1, parser.getCount());
		
	}

	public void testTwoLine() throws IOException
	{
		parser = new  Parser(input3);
		parser.parse();
		
		assertEquals(2, parser.getCount());
	}
	
	
	public void testGetItemCount() throws IOException
	{
		parser = new  Parser(input);
		parser2 = new Parser(input3);
		
		assertEquals(1, parser.getItemCount());
		assertEquals(2, parser2.getItemCount());
	}
	
	public void testEquals()
	{
		String input = "bca";
		String input2 = "ccabc";
		String input3 = "xds";
		
		assertEquals("abc", Parser.sortString(input));
		assertEquals("abccc", Parser.sortString(input2));
		assertEquals("dsx", Parser.sortString(input3));
	}
	
	public void testContains() throws IOException
	{
		
		parser = new Parser(input);
		parser.parse();
		
		String sortedItem = Parser.sortString(input);
		assertEquals(1, parser.getContainsCount(sortedItem));
	}

	public void testContains2() throws IOException
	{
		parser = new Parser(input4);
		parser.parse();
		
		assertEquals(4, parser.getContainsCount(Parser.sortString("abc")));
		assertEquals(2, parser.getContainsCount(Parser.sortString("aabb")));
		assertEquals(2, parser.getContainsCount(Parser.sortString("cdd")));
		assertEquals(1, parser.getContainsCount(Parser.sortString("xds")));
	}
	
	public void testReadFie() throws IOException
	{
		String filename = "inputFile1.txt";

		parser = new Parser(new File(filename));
		assertEquals(2284 , parser.getItemCount());
	}
	
	public void testParse() throws IOException
	{
		String filename = "inputFile3.txt";
		
		parser = new Parser(new File(filename));
		parser.parse();
	}
	
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:22:53
Processing time 0.0235 sec