2007-10-29から1日間の記事一覧

八本目 さいころ

p rand(6)+1

七本目 appleにはマッチするけどgreen appleにはマッチしない置換(失敗)

str =####ここから %Q{this green apple is an apple but is not an green apple }###ここまで matchword = "apple" notmatchword = "green apple" str.each do |line| word = line while /apple/ =~ word do if word.index(/#{notmatchword}/) + notmatchwo…

六本目 CSV分解

str =####ここから %Q{one, two, three, four }###ここまで str.each do |line| ary = line.split(",") ary.each do |word| print word.strip print "\n" end end

五本目 四本目の逆

str =####ここから %Q{HELLO_WORLD THIS_IS_TEST_PATTERN TEST_METHOD }###ここまで str.each do |line| word = line while /[A-Z]*_/ =~ word do print $&.capitalize.chop word = $' end print word.capitalize.chomp print "\n" end

四本目 HelloHellWorld→HELLO_HELL_WORLD

str =####ここから %Q{HelloWorld ThisIsTestPattern TestMethod }###ここまで str.each do |line| word = line /[A-Z][a-z]*/ =~ word while /[A-Z][a-z]*/ =~ word do print $&.upcase! if $'.chomp.length > 0 then print '_' end word = $' end print "\…

三本目 ダブルクオートで囲まれた数字に+1

str =####ここから %Q{HashMap map = new HashMap(); map.put("0", "zero"); map.put("1", "one"); map.put("2", "two"); map.put("3", "three"); map.put("10", "ten"); }###ここまで str.each do |line| if /"[0-9]*"/ =~ line then print $` num = $&[1..…

二本目 メソッド生成

str = ####ここから "getParentWindow getParentFrame init " ###ここまで str.each do |line| print "public void "+line.chomp+"(){ //TODO } " end

一本目 ひながた(貼り付けたテキストをそのまま表示)

str = #↓↓↓↓ここから " getParentWindow getParentFrame init testPattern1 testPattern2 " #↑↑↑↑ここまで str.each do |line| print line end

十本目 あいさつ

print "Hello, warld"

九本目 さいころ一万回ふる

a = Array.new(10000) normdist = 0.0 max = 0.0 include Math for i in 1..10000 a[i] = rand(6)+1 max += a[i] end ave = max / 10000 print "平均:"+ ave.to_s for i in 1..10000 normdist += (a[i] - ave) * (a[i] - ave) end normdist = sqrt(normdist…

Ruby10本ノック

仕事に反復作業が多くてとても悲しい ので、モチベ維持&効率化のためにRubyを少しずつ勉強中 でもすぐ忘れる100本ノックにしたいとこだが、どうせ続かないので10本くらい目標。