Language detail: Java
Coverage: 97.87%
|
number of '+' ratings |
contribution for coverage |
Unsolved challenges
codes
タブ区切りデータの処理
(Nested
Flatten)
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<Cell[]> list = new ArrayList<Cell[]>();
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 < rows(); ++ir ) {
sorters[ir-1] = new Sorter( column[ir], row( ir ) );
}
Arrays.sort( sorters );
for ( int ir = 1; ir < 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 < 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 < array.length; ++i ) {
cells[i] = new Cell( array[i] );
}
return cells;
}
void set( int ic, Cell[] column ) {
for ( int ir = 0; ir < rows(); ++ir ) {
cells[ir][ic] = column[ir];
}
}
Cell[] row( int ir ) {
Cell[] bak = new Cell[columns()];
for ( int ic = 0; ic < columns(); ++ic ) {
bak[ic] = cells[ir][ic];
}
return bak;
}
Cell[] column( int ic ) {
Cell[] bak = new Cell[rows()];
for ( int ir = 0; ir < 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 < cells.length; ++ir ) {
for ( int ic = 0; ic < cells[0].length; ++ic ) {
bak.append( ic > 0 ? "\t"
: "" ).append( cells[ir][ic].toString() );
}
bak.append( "\r\n" );
}
return bak.toString();
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Sample {
private List<String> header;
private List<List<String>> data;
public Sample(String str) {
boolean isFirst = true;
String[] lines = str.split("\n");
data = new ArrayList<List<String>>(lines.length - 1);
for (String line : str.split("\n")) {
List<String> row = Arrays.asList(line.split("\t"));
if (isFirst && !(isFirst = false)) {
header = row;
} else {
data.add(row);
}
}
}
public Sample sort(final int column) {
Collections.sort(data, new Comparator<List<String>>() {
public int compare(List<String> l, List<String> r) {
return r.get(column).compareTo(l.get(column));
}
});
return this;
}
public Sample swapColumn(final int column1, final int column2) {
List<List<String>> list = new ArrayList<List<String>>(data);
list.add(0, header);
forAll(list, new Closure() {
public void each(List<String> row) {
row.set(column1, row.set(column2, row.get(column1)));
}
});
return this;
}
public Sample addValue(final int column, final int add) {
forAll(new Closure() {
public void each(List<String> row) {
row.set(column,
Integer.toString(Integer.parseInt(row.get(column)) + add));
}
});
return this;
}
private void forAll(Closure c) {
forAll(data, c);
}
private void forAll(List<List<String>> list, Closure c) {
for (List<String> row : list) {
c.each(row);
}
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(join(header)).append("\n");
for (List<String> row : data) {
builder.append(join(row)).append("\n");
}
return builder.toString();
}
private String join(List<String> list) {
StringBuilder builder = new StringBuilder();
boolean isFirst = true;
for (String str : list) {
if (!isFirst) {
builder.append("\t");
} else {
isFirst = false;
}
builder.append(str);
}
return builder.toString();
}
private static interface Closure {
public void each(List<String> row);
}
public static void main(String[] args) {
String str =
"ID\tSurname\tForename\tAge\n" +
"1\tSato\tHanako\t17\n" +
"0\tSuzuki\tTaro\t18\n";
System.out.println(
new Sample(str).sort(1).swapColumn(1, 2).addValue(3, 1));
}
}
|
2^i * 3^j * 5^k なる整数
(Nested
Flatten)
別のアルゴリズムで解いてみました。
数が大きくなれば有利かな?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class Sample206 {
public static void main(String[] args) {
OUTER: for (int i = 1, n = 0; n < 100; i++) {
for (int p = 7; p <= i; p += 2) {
if (p % 3 == 0) continue;
if (p % 5 == 0) continue;
if (i % p == 0) {
continue OUTER;
}
}
System.out.println(i);
n++;
}
}
}
|
おなじアルゴリズムになってしまいました。 100個目は1536。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class Int235 {
public static void main(String[] a) {
int limit = 100;
for (int n = 0, i = 1; n < limit; i++) {
int tmp = i;
while (tmp % 2 == 0) tmp /= 2;
while (tmp % 3 == 0) tmp /= 3;
while (tmp % 5 == 0) tmp /= 5;
if (tmp == 1) {
System.out.println(i);
n++;
}
}
}
}
|
起動オプションの解析
(Nested
Flatten)
ApacheCommonsCLIを使って書いてみました。
see: Apache commons CLI
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.MissingArgumentException;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
class cmdopt {
public static void main(String[] args) {
CommandLineParser parser = new PosixParser();
Options options = new Options();
options.addOption(OptionBuilder.withArgName("o").withLongOpt("output").isRequired(true).withDescription("set output").create("o"));
options.addOption("q","quote",false,"set quote");
options.addOption("d","debug",true,"set debug level");
try {
CommandLine command = parser.parse(options,args);
if ( (command.hasOption("d") &&
( Integer.parseInt(command.getOptionValue("d")) < 0 ||
Integer.parseInt(command.getOptionValue("d")) > 2)) ||
command.getArgs().length == 0)
throw new MissingArgumentException("invalid argument.");
System.out.println("[オプション情報]");
System.out.println("o(output):\t"+(command.hasOption("o") ? "ON" : "OFF"));
System.out.println("q(quote):\t"+(command.hasOption("q") ? "ON" : "OFF"));
System.out.println("d(debug):\t"+(command.hasOption("d") ? command.getOptionValue("d") : "2"));
System.out.println();
System.out.println("[パラメータ情報]");
System.out.printf("指定数:\t%d\n",command.getArgs().length);
for (int i=0;i<command.getArgs().length;i++)
System.out.printf("%d:\t%s\n",i+1,command.getArgs()[i]);
} catch(ParseException ex) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("cmdopt",options);
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
|
LL Golf Hole 9 - トラックバックを打つ
(Nested
Flatten)
Javaもまだの様なので。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import java.io.*;
import java.net.*;
class T{
public static void main(String[] a) throws Exception{
HttpURLConnection c=(HttpURLConnection)(new URL("http://ll.jus.or.jp/2008/blog/archives/38/trackback")).openConnection();
c.setRequestMethod("POST");
c.setDoOutput(true);
OutputStreamWriter o=new OutputStreamWriter(c.getOutputStream());
o.write("title=LL+Golf+Hole+9&excerpt=trackback+from+LL+Golf+Hole+9.&url=http://ja.doukaku.org/207/&blog_name=LL+Golf+Hole+9");
o.flush();
o.close();
BufferedReader i=new BufferedReader(new InputStreamReader(c.getInputStream()));
while(i.ready())System.out.println(i.readLine());
c.disconnect();
}
}
|
文字列型日時ののN秒後時間取得
(Nested
Flatten)
フォーマットの指定がなかったので、とりあえずサンプルと同じにしてみました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Sample204 {
private static final DateFormat FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");
public static String dateEx(String time, int seconds) throws ParseException {
Date date = FORMAT.parse(time);
return dateEx(date, seconds);
}
public static String dateEx(int seconds) {
return dateEx(new Date(), seconds);
}
private static String dateEx(Date date, int seconds) {
Calendar cal = Calendar.getInstance();
cal.setTime( date );
cal.add(Calendar.SECOND, seconds);
return FORMAT.format(cal.getTime());
}
public static void main(String[] args) {
try {
System.out.println(dateEx("20080827235925",40));
System.out.println(dateEx(0));
} catch (ParseException ex) {
ex.printStackTrace();
}
}
}
|
LL Golf Hole 8 - 横向きのピラミッドを作る
(Nested
Flatten)
今さらながらせっかく書いたので。タブと改行を除いて152Bです。
1 2 3 4 5 6 7 8 | class T{
public static void main(String[]s){
int x,y,m=Integer.parseInt(s[0]);
for(x=0;++x<m*2;)
for(y=x<m?x:2*m-x;y>=0;)
System.out.print(y-->0?"*":"\n");
}
}
|
宣言をまとめると4バイト縮みます。
1 2 3 4 5 6 7 8 | class L{
public static void main(String[]s){
int i,j,n=Integer.parseInt(s[0]);
for(i=1;i>0;n--,i+=n>0?1:-1)
for(j=i;j>=0;j--)
System.out.print(j>0?"*":"\n");
}
}
|
ちょっと短くなったので再投稿。 改行とTabを除いて158B
1 2 3 4 5 6 7 8 | class L{
public static void main(String[]s){
int n=Integer.parseInt(s[0]);
for(int i=1;i>0;n--,i+=n>0?1:-1)
for(int j=i;j>=0;j--)
System.out.print(j>0?"*":"\n");
}
}
|
色々と駆使して短くしてみました。 改行・Tabを除いて 168B
1 2 3 4 5 6 7 8 | class Sample203{
public static void main(String[]s){
int n=Integer.parseInt(s[0]);
for(int l=1;l<n*2;l++)
for(int i=(l<n?l:n*2-l);i>=0;i--)
System.out.print(i>0?"*":"\n");
}
}
|
簡単そうに見えて難しかった。 結局この程度。
1 2 3 4 5 6 7 8 9 10 11 | class H8 {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
for(int i=1; i<=Integer.parseInt(args[0]); i++) {
for(int j=1; j<=i; j++) sb.append("*");
sb.append("\n");
}
System.out.print(sb);
System.out.println(sb.reverse().delete(0,target+2) );
}
}
|
LL Golf Hole 7 - バイト数を読みやすくする
(Nested
Flatten)
投稿直後に縮むことに気付いたので再投稿。 これで 164B
1 2 3 4 5 6 7 8 | class P{
public static void main(String[]a){
double d=Double.parseDouble(a[0]);
int i=0,l=1000;
for(;d>=l;d/=l,i++);
System.out.printf("%.1f"+" kMGTPEZY".charAt(i),d);
}
}
|
1000で割る方で書いてみました。 タブと改行を除いて 166B
1 2 3 4 5 6 7 8 | class P{
public static void main(String[]a){
double d=Double.parseDouble(a[0]);
int i=0,l=1000;
for(;d>=l;d/=l,i++);
System.out.printf("%.1f%s",d," kMGTPEZY".charAt(i));
}
}
|
LL Golf Hole 6 - 10進数を2進数に基数変換する
(Nested
Flatten)
標準入力から受け取った10進の整数を 2進数に変換します。 125B
1 2 3 4 5 | class B{
public static void main(String[]s){
System.out.println(Long.toString(new java.util.Scanner(System.in).nextInt(),2));
}
}
|
LL Golf Hole 5 - 最上位の桁を数え上げる
(Nested
Flatten)
Javaで頑張って縮めてみました。 改行・タブを消して 146B
1 2 3 4 5 6 7 8 | class LL_Golf_Hole_5{
public static void main(String[]s){
for(int i=0,t=1;i<=Integer.parseInt(s[0]);i+=t){
System.out.println(i);
if(i==t*10)t*=10;
}
}
}
|
echoクライアント
(Nested
Flatten)
Javaがまだの様なので。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
class Echo {
Socket socket;
OutputStream out;
InputStream in;
public Echo(String host,int port) throws Exception {
socket = new Socket(host,port);
out = socket.getOutputStream();
in = socket.getInputStream();
}
public void echo(int size,byte[] request) throws Exception {
byte[] response= new byte[size];
out.write(request,0,size);
out.flush();
in.read(response,0,size);
System.out.write(response,0,size);
}
public void close() throws Exception {
socket.close();
}
public static void main(String[] args) {
try {
Echo echo = new Echo(args[0],Integer.parseInt(args[1]));
byte[] request = new byte[2048];
int size;
while ((size = System.in.read(request)) > 0) {
echo.echo(size,request);
}
echo.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
|
LL Golf Hole 5 - 最上位の桁を数え上げる
(Nested
Flatten)
next >>
Javaだとこんなもんでしょうか?
1 2 3 4 5 6 7 8 | class LL_Golf_Hole_5 {
public static void main(String[] args) {
for(int i=0; i<=Integer.parseInt(args[0]); i++) {
if(String.valueOf(i).charAt(0) != String.valueOf(i-1).charAt(0) )
System.out.println(i);
}
}
}
|





匿名
#7769()
[
Java
]
Rating0/0=0.00
Rating0/0=0.00-0+
[ reply ]