2010年2月27日土曜日

Ruby: 線形探索法組んでみた

線形探索法をRubyで実装してみた。
配列[5, 2, 1, 3, 4]からanswer=3を探索するプログラム。
if文の書き方に手間取った。
あと、型を書かなくていいってことに戸惑った。
whileじゃなくて、eachとかでも良かったのかな?
繰り返しの実装方法も慣れない。

〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
array = [5, 2, 1, 3, 4];

index = 0;
answer = 3;
array[5] = 1;

while index < array.length
if array[index] == answer then
break
else
index = index + 1
end
end

if index < array.length - 1 then
print "success, answer=array[", index, "]:", array[index]
else
print "failure";
end
〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓

2010年2月10日水曜日

Ruby

Ruby始めました。