ミームの死骸を待ちながら

We are built as gene machines and cultured as meme machines, but we have the power to turn against our creators. We, alone on earth, can rebel against the tyranny of the selfish replicators. - Richard Dawkins "Selfish Gene"

We are built as gene machines and cultured as meme machines, but we have the power to turn against our creators.
We, alone on earth, can rebel against the tyranny of the selfish replicators.
- Richard Dawkins "Selfish Gene"

第一章

新版 C言語によるアルゴリズムとデータ構造この本の内容を演習問題をメインにRubyに翻訳していくよ。
演習1,2は簡単すぎたので3から10まで。「続きを読む」クリックすれば最後まで出る。
著者の柴田さんにに許可とろうと思ったけど、有名人すぎるのか本にはアドレス公開してないしググると後援会HPにぶちあたる上ただの著者名としての引用にひっかかりまくるし、しまいに面倒くさくなってあきらめた。Web上に全解答公開してるし、ヘタレRubyに翻訳したバージョンくらい大丈夫だろう。訴えるなら訴えるがいいさ
…あ、えと、やっぱまずいなら連絡ほしいかなーとか

演習1-3 1からnまでの和を求める

def sum(n)
  sum = 0
  sumstring = ""
  for i in 1..n    #1..nはRange Object
   sum += i
   if i != 1
   sumstring = sumstring + " + #{i.to_s}"
   else sumstring = "1"
   end
  end
  print "1からnまでの和\n"
  print sumstring, "=", sum
end

sum(rand(20))

演習1-4 2整数間の和を求める

def sumof(a,b)
  sum = 0
  case a <=> b
  when 1
    for i in b..a
      sum += i
    end
  when -1
    for i in a..b
      sum += i
    end
  when 0
    sum = 0
  end
  return sum
end
a = rand(10)
b = rand(10)
print a, "と", b, "の間の整数の和は\n"
p sumof(a,b)

演習1-5 正数の桁数を表示する

def digit(n)
  count = 0
  d = nil
  while d != 0
    d = n / 10
    n = d
    count += 1
  end
return count
end
n = 134343
print n, "の桁数は", digit(n), "である\n"

演習1-6 2数の差を出すプログラム(ツッコミ機能付き)

#二回に分けて引数取るプログラムってRuby,できるんかな。

def subtra(a,b)
  if a > b
    print "第二引数は第一引数以上に設定してください\n"
  else
    print b, " - ", a, "は", b-a, "です\n"
  end
end

a, b = ARGV[0].to_i, ARGV[1].to_i
×puts subtra(a,b)
→subtra(a,b)

#最後にnil帰ってくる。何で?(´・ω・`)

id:secondlifeさんにご指摘いただきました。printの戻り値はnilなので、subtra method内でprintした結果をputs subtra(a,b)で表示してたからnilまで出てた。修正。

演習1-7 直角三角形4パターン、段数指定して作る関数

このプログラムでは、ARGVで取った引数を4つ、順に左下、左上、右上、右下直角の三角形の段数として設定している。実行は

$ ruby pra1-7.rb 3 5 3 6

てな感じ。ちなみにARGVはArrayで入ってくる。故にARGV.each{ }とかもいけちゃう。また、ARGVで取った中身はstringになっているため、数値として扱う時は.to_i忘れずに。

def trilb
  a = []
ARGV.each {|item|
  a << item.to_i
}
  for i in 1..a[0]
    for j in 1..i
      print "*"
    end
    print "\n"
  end
end

def trilu
  a = []
ARGV.each {|item|
  a << item.to_i
}
  for i in 1..a[1]
    for j in 1..(a[1]-i+1)
      print "*"
    end
    print "\n"
  end
end


#そういえば、RubyってStringを掛け算して繰り返し表示できた気がする…


def triru
  a = []
ARGV.each {|item|
  a << item.to_i
}
  for i in 1..a[2]
   print " " * (i-1), "*" * (a[2]-i+1), "\n"
  end
end


def trirb
  a = []
ARGV.each {|item|
  a << item.to_i
}
  for i in 1..a[3]
   print " " * (a[3]-i), "*" * i, "\n"
  end
end

trilb
print "\n\n"
trilu
print "\n\n"
triru
print "\n\n"
trirb

#ary作成繰り返しはもっとなんとかなりそうだけどまぁいいや

演習1-8 n段のピラミッドを作る関数

#土台は(n-1)*2+1個。必ず奇数になる。
def spira(n)
  max = (n-1) * 2 + 1
  for i in 1..n
    m = (i-1) * 2 + 1
    print " " *  ((max-m)/2), "*" * m, "\n" 
  end
end

spira(rand(20))

演習1-9 n段の数字ピラミッドを作る関数

def npira(n)
  max = (n-1) * 2 + 1
  for i in 1..n
    m = (i-1) * 2 + 1
    num = (i % 10).to_s #CやとStringにしたいときどうする?
    print " " * ((max-m)/2), num * m, "\n"
  end
end

npira(rand(20))

演習1-10 九九の表を作る

for i in 1..9
  for j in 1..9
    printf("%2d", i * j)
    print "|" unless j == 9
  end
  print "\n"
#  print "-" * 26, "\n"  まぁこの行は出力の好みで
end