<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>Latest comments posted by nattou_curry</title><link>http://ja.doukaku.org/user/913/</link><description>Latest comments posted by nattou_curry(long)</description><language>ja</language><lastBuildDate>Fri, 09 Jan 2009 04:41:14 -0000</lastBuildDate><item><title>nattou_curry's comment on タブ区切りデータの処理
</title><link>http://ja.doukaku.org/comment/7759/</link><description>



&lt;a href="http://ja.doukaku.org/209/"&gt;タブ区切りデータの処理&lt;/a&gt;
(&lt;a href="http://ja.doukaku.org/209/nested/"&gt;Nested&lt;/a&gt; 
 &lt;a href="http://ja.doukaku.org/209/flatten/"&gt;Flatten&lt;/a&gt;)


&lt;hr&gt;
  &lt;pre class='compact'&gt;import java.io.*;
import java.util.*;

class Table {
	
	public static void main( String[] args ) {
		Table table = new Table()
			.input( "in.tsv" )
			.sort( 0 )
			.replace( 1, 2 )
			.increment( 3 )
			.output( "out.tsv" );
	}
	
	Cell[][] cells;
	
	class Cell {
		String value;
		Cell( String value ) {
			this.value = value;
		}
		public String toString() { return value; }
		public int compareTo( Cell that ) { return this.value.compareTo( that.value ); }
		public void increment() { value = ( Integer.parseInt( value ) + 1 ) + ""; }
	}
	
	class Sorter implements Comparable {
		Cell key;
		Cell[] row;
		Sorter( Cell key, Cell[] row ) {
			this.key = key;
			this.row = row;
		}
		public int compareTo( Object that ) {
			return this.key.compareTo( ( (Sorter) that ).key );
		}
	}
	
	Table input( String file ) {
		try {
			BufferedReader in = new BufferedReader( new FileReader( file ) );
			List&amp;lt;Cell[]&amp;gt; list = new ArrayList&amp;lt;Cell[]&amp;gt;();
			String line;
			while ( ( line = in.readLine() ) != null ) {
				list.add( makeCells( line.split( "\t" ) ) );
			}
			in.close();
			cells = list.toArray( new Cell[0][] );
			return this;
		} catch ( IOException e ) { throw new RuntimeException ( e ); }
	}
	
	Table sort( int ic ) {
		Cell[] column = column( ic );
		Sorter[] sorters = new Sorter[rows()-1];
		for ( int ir = 1; ir &amp;lt; rows(); ++ir ) {
			sorters[ir-1] = new Sorter( column[ir], row( ir ) );
		}
		Arrays.sort( sorters );
		for ( int ir = 1; ir &amp;lt; rows(); ++ir ) {
			cells[ir] = sorters[ir-1].row;
		}
		return this;
	}
	
	Table replace( int ic1, int ic2 ) {
		Cell[] column1 = column( ic1 );
		Cell[] column2 = column( ic2 );
		set( ic1, column2 );
		set( ic2, column1 );
		return this;
	}
	
	Table increment( int ic ) {
		Cell[] column = column( ic );
		for ( int ir = 1; ir &amp;lt; rows(); ++ir ) {
			column[ir].increment();
		}
		return this;
	}
	
	Table output( String file ) {
		try {
			PrintWriter out = new PrintWriter( new BufferedWriter( new FileWriter( file ) ) );
			out.print( toString() );
			out.flush();
			out.close();
			return this;
		} catch ( IOException e ) { throw new RuntimeException( e ); }
	}

	Cell[] makeCells( String[] array ) {
		Cell[] cells = new Cell[array.length];
		for ( int i = 0; i &amp;lt; array.length; ++i ) {
			cells[i] = new Cell( array[i] );
		}
		return cells;
	}
	
	void set( int ic, Cell[] column ) {
		for ( int ir = 0; ir &amp;lt; rows(); ++ir ) {
			cells[ir][ic] = column[ir];
		}
	}
	
	Cell[] row( int ir ) {
		Cell[] bak = new Cell[columns()];
		for ( int ic = 0; ic &amp;lt; columns(); ++ic ) {
			bak[ic] = cells[ir][ic];
		}
		return bak;
	}
	
	Cell[] column( int ic ) {
		Cell[] bak = new Cell[rows()];
		for ( int ir = 0; ir &amp;lt; rows(); ++ir ) {
			bak[ir] = cells[ir][ic];
		}
		return bak;
	}
	
	int rows() { return cells.length; }
	
	int columns() { return cells[0].length; }
	
	public String toString() {
		StringBuilder bak = new StringBuilder();
		for ( int ir = 0; ir &amp;lt; cells.length; ++ir ) {
			for ( int ic = 0; ic &amp;lt; cells[0].length; ++ic ) {
				bak.append( ic &amp;gt; 0 ? "\t"
						: "" ).append( cells[ir][ic].toString() );
			}
			bak.append( "\r\n" );
		}
		return bak.toString();
	}
}&lt;/pre&gt;
&lt;hr&gt;
  

</description><guid>http://ja.doukaku.org/comment/7759/</guid></item><item><title>nattou_curry's comment on LL Golf Hole 8 - 横向きのピラミッドを作る
</title><link>http://ja.doukaku.org/comment/7501/</link><description>



&lt;a href="http://ja.doukaku.org/203/"&gt;LL Golf Hole 8 - 横向きのピラミッドを作る&lt;/a&gt;
(&lt;a href="http://ja.doukaku.org/203/nested/"&gt;Nested&lt;/a&gt; 
 &lt;a href="http://ja.doukaku.org/203/flatten/"&gt;Flatten&lt;/a&gt;)


&lt;hr&gt;
  今さらながらせっかく書いたので。タブと改行を除いて152Bです。
&lt;hr&gt;
  
    &lt;table&gt;&lt;tr&gt;&lt;td class="linenos"&gt;&lt;pre&gt;1
2
3
4
5
6
7
8&lt;/pre&gt;&lt;/td&gt;&lt;td class="code"&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;parseInt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;]);&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;++&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;;)&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="nl"&gt;x:&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;)&lt;/span&gt;
                &lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;--&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;*&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;\n&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
    &lt;div align = "right" style="margin-right: 1em;"&gt;
      [&lt;a href="http://ja.doukaku.org/lang/java/"&gt;
      Java
      &lt;/a&gt;]
      [&lt;a href="http://ja.doukaku.org/comment/7501/download/"&gt;
      download code
      &lt;/a&gt;]
      &lt;/a&gt;]
    &lt;/div&gt;
    &lt;hr&gt;
  

</description><guid>http://ja.doukaku.org/comment/7501/</guid></item></channel></rss>