• 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

   1  def shotening(str)
   2    return str[0..0] + str[1..-2].gsub!(/\s/,"").size.to_s + str[-1..-1]
   3  end
   4  
   5  str = "something new and exciting"
   6  
   7  shotening(str)    #=> "s21g"

ってことなんですかね?

posted by Png y_tsuda on Fri 22 Aug 2008 at 13:59 with 2 comments

最近全然JavaScriptを書いていなかったので、少し書いてみる。

お題は、どう書く.org - LL Golf Hole 6 - 10進数を2進数に基数変換する です。

ブックマークレットにしてみた。

   1  javascript: n=window.prompt("10進数を入力すると、2進数にしてくれるよ!",  ""); alert(parseInt(n, 10).toString(2));

上のコードをアドレスバーに貼り付ければ実行できます。

コードの内容は、

  1. window.promptで数字を得る。(これは文字列)
  2. parseInt(n, 10)で、さっき得た文字列を数値(10進数)に変換
  3. 得た数値を2進数(文字列)に変換
  4. 表示

文字列を1度、数値に変換しないといけないのが少し気にいらないなぁ。

何かいい方法があるだろうか。

posted by Png y_tsuda on Fri 22 Aug 2008 at 11:41

昨日に引き続きIntegerに関して。 個人的に、Integerブームが起こってます。

Integerに関しては、http://www.ruby-doc.orgのClass: Integerを参照のこと。

今日は最大公約数と最小公倍数を同時に返してくれるInteger#gcdlcmについてです。

このInteger#gcdlcmの実装が気に入らない!!

どこが気に入らないのかという本題に入る前に、Integer#lcmについての実装を見てみよう!

   1  # File lib/rational.rb, line 457
   2    def lcm(other)
   3      if self.zero? or other.zero?
   4        0
   5      else
   6        (self.div(self.gcd(other)) * other).abs
   7      end
   8    end

オーソドックスなの最小公倍数の求め方。 ここではあまり問題にしない。

このコードをよーく覚えておいてください。

ここからが本題です。

Integer#gcdlcmの実装を見て見る。

   1  # File lib/rational.rb, line 473
   2    def gcdlcm(other)
   3      gcd = self.gcd(other)
   4      if self.zero? or other.zero?
   5        [gcd, 0]
   6      else
   7        [gcd, (self.div(gcd) * other).abs]
   8      end
   9    end

Integer#lcmにそっくり!

・・・って、Integer#lcmのコードにInteger#gcdいれただけじゃないですか・・・。

せっかくこのコードの直前で、Integer#lcmを定義してるのに、何で使わないんやろうか・・・。

   1  def gcdlcm(other)
   2    return [self.gcd(other), self.lcm(other)]
   3  end

という風に書いても不都合はなさそうやけど・・?

むしろ、こういう風に書いてくれたほうが、気持ちいいんやけども。

ん~、Integerには謎がいっぱいや。

posted by Png y_tsuda on Fri 15 Aug 2008 at 04:06

Rubyには、最大公約数を求めるInteger#gcd と Integer#gcd2がある。 「gcd"2"ってなんやねん!」と気になったので、実装を見てみた。

   1  # File lib/mathn.rb, line 19
   2    def gcd2(int)
   3      a = self.abs
   4      b = int.abs
   5      a, b = b, a if a < b
   6      
   7      pd_a = a.prime_division
   8      pd_b = b.prime_division
   9      
  10      gcd = 1
  11      for pair in pd_a
  12        as = pd_b.assoc(pair[0])
  13        if as
  14          gcd *= as[0] ** [as[1], pair[1]].min
  15        end
  16      end
  17      return gcd
  18    end

2数を因数分解し、どちらにもある数をかけていくと言う強引なものだった。

ついでにInteger#gcdのほうも見てみた。

   1  # File lib/rational.rb, line 438
   2    def gcd(other)
   3      min = self.abs
   4      max = other.abs
   5      while min > 0
   6        tmp = min
   7        min = max % min
   8        max = tmp
   9      end
  10      max
  11    end

コチラは、ユークリッドの互除法。 シンプルだ。

参考までに、この2つに関してベンチマークしてみた。

  • コード

       1  require 'mathn'
       2  require 'benchmark'
       3  
       4  Benchmark.bm do |x|
       5    x.report("Integer#gcd :"){ 12345.gcd(123456789) }
       6    x.report("Integer#gcd2:"){ 12345.gcd2(123456789) }
       7  end
    

  • 結果

       1        user     system      total        real
       2  Integer#gcd :  0.000000   0.000000   0.000000 (  0.000000)
       3  Integer#gcd2:  0.516000   0.000000   0.516000 (  0.516000)
    

結果は当たり前といったら当たり前なんやけども、Integer#gcd2のほうが遅かった。

何でInteger#gcdとInteger#gcd2の2つが存在するんやろ・・・。

というか、Integer#gcd2が何で存在しているのかが不思議!!

何かメリットでもあるんやろか・・・?

posted by Png y_tsuda on Thu 14 Aug 2008 at 00:14

せっかくアカウントをとったので、テストがてらプログラムを載せてみる。

お題は、どう書く.orgにあった、一番最近の問題で。

問題の内容は、 どう書く.org - LL Golf Hole 3 - 13日の金曜日を数え上げるを参照のこと。

   1  require 'date'
   2  
   3  from = DateTime.now
   4  to = DateTime.parse("2013-12-31")
   5  
   6  puts (from..to).find_all{|d| d.wday==5 and d.mday==13}.size

こんな感じになりました。

コードがちゃんとハイライトされてる! s21g blogすごくイイ!!

こんな便利なブログサービスを提供してくれている瀧内さんに感謝、感謝です。

posted by Png y_tsuda on Thu 7 Aug 2008 at 00:58 with 3 comments