himorogiの日記

主にプログラミングに関することなど。少々ハード(電子工作)についても。

Mac OS X で IronPython を使う

■ Mono を install
※ Mono(version 2.4) の IronPython の version が古い(1.1)なので最新版(2.0.1)をdownload
※ Mono(version 2.4) は .Netframework 3.5 対応なので、IronPython 2.0.1 で問題ないようだ
※ IronPython は binary を download しても良い(というか、敢えて source → make する必要はない)

Rhino(Javascript)には標準入出力がないので、Java の標準入出力を使う

※ Sun JavaSDK の jrunscript を使わない場合

Java のクラスライブラリを読み込んで object を new

importPackage(java.io);
var rd = new BufferedReader(new InputStreamReader(java.lang.System['in']));
var wr = new OutputStreamWriter(java.lang.System['out']);

□入力のテスト

>print(rd.readLine());
harahirehoro
harahirehoro
>

※ ここでは入力した行をそのまま print してるだけなので、あんまり意味は無い…

□ 出力のテスト

>wr.write("hello java world");
>wr.flush();
hello java world>

※ 出力文字列に改行が入ってなかったので prompt が出力文の直後に登場している
※ flush() 必須

Rhino 版 Lotos (file name:kuji.js)

importPackage(java.io);
var rd = new BufferedReader(new InputStreamReader(java.lang.System['in']));
var wr = new OutputStreamWriter(java.lang.System['out']);

var lotos =function (depth){
  var ceil, mask=[0,0], coll =[];

  function getNum(){ return(function(x){ return x.substr(x.length-2,2) })("00"+ Math.ceil(Math.random()*ceil)); };
  isnotUniq= function(x) { return 0!=(mask[Math.floor(x/32)] &  1<<(x%32)); };
  pushNum= function(x) { mask[Math.floor(x/32)] |= 1<<(x%32); coll.push(x); }

  ceil=(function(x){for(i=c=0;i<x; c+= ++i);return c})(depth)*2+1;
  pushNum(getNum());
  while( coll.length<depth ){ pushNum((function(){ while(isnotUniq(x=getNum())); return x; })()); }
  return coll;
};
wr.write("5:minilot\n6:loto6\n");wr.flush();
sx=rd.readLine();
if(sx<5 || sx>6){ wr.write("bad arguments\n"); }else{ wr.write(lotos(sx).sort().toString()); }
wr.write("\n");wr.flush();

PowerShell 版 Lotos

PowerShell 版 Lotos を作るにあたり、PowerShell の学習をかねて、基本的なレベルから逐次記述

※ Loto6 と MiniLoto のルール

  • Loto6:1から43までの43個の数字の中から異なる6個の数字を選ぶ
  • MiniLoto:1から31までの31個の数字の中から異なる5個の数字を選ぶ

□ 1から43 までの数字を乱数で求める

PS >$oRnd = new-object random
PS >$oRnd.next(1,43)
30
PS >