2012/05/18


逆FizzBuzz問題 (Inverse FizzBuzz) - 猫とC#について書くmatarilloの雑記

逆FizzBuzz問題 (Inverse FizzBuzz) General | Inverse Fizzbuzz - just another scala quant を日本語にしました。 ちなみに...

http://d.hatena.ne.jp/matarillo/20120515/p1
逆FizzBuzzって、オートマトンなので正規表現を使うと楽に出来るはず。 #!perl
use strict;
use warnings;
use Test::More;

sub inv_fizzbuzz {
  my ($fz$n$pat) = (''1join ',*?'@_);
  while (1) {
    $fz .= $n % 15 > 0 ?
             $n % 3 > 0 ?
               $n % 5 > 0 ?
                 ',''buzz,''fizz,''fizzbuzz,';
    if ($fz =~ /,($pat)/) {
      my $rhs = $1;
      my $lhs = substr($fz0index($fz$rhs) + 1);
      $lhs =~ s/(fizz|buzz)//g;
      $rhs =~ s/(fizz|buzz)//g;
      $lhs = length($lhs);
      $rhs = $lhs + length($rhs||'');
      return [$lhs$rhs];
    }
    $n++;
  }
}

is_deeply(inv_fizzbuzz('fizz'), [33]);
is_deeply(inv_fizzbuzz('buzz'), [55]);
is_deeply(inv_fizzbuzz('fizz''buzz'), [35]);
is_deeply(inv_fizzbuzz('buzz''fizz'), [56]);
is_deeply(inv_fizzbuzz('fizz''fizz''buzz'), [610]);
is_deeply(inv_fizzbuzz('fizz''fizz'), [69]);
is_deeply(inv_fizzbuzz('fizz''buzz''fizz'), [36]);
is_deeply(inv_fizzbuzz('fizzbuzz''fizz'), [1518]);

done_testing;
追記1
Vimだとこうか?
functions:inv_fizzbuzz(...)
  let [fz, np= [''1join(a:000',\{-}')]
  while 1
    let fz .= n%15>0?n%3>0?n%5>0?",""buzz,""fizz,""fizzbuzz,"
    let m = matchstr(fz, p)
    if len(m) > 0
      let lhs = len(split(fz[:stridx(fz, m)-1], ','1))
      return [lhs, lhs + len(split(m, ','1))-1]
    endif
    let n += 1
  endwhile
endfunction

追記2
ちょっと改良。方針は変わってない。
#!perl
use strict;
use warnings;
use Test::More;

my $gs = 'AAFAABAFAAAFABAAFAAAZ';

sub inv_fizzbuzz {
  my ($fz$n$pat) = (''1,
    join('A*?'map({fizz=>'F'buzz=>'B'fizzbuzz=>'Z'}->{$_}, @_)));
  my $m;
  while (1) {
    $fz .= $n % 15 > 0 ?
             $n % 3 > 0 ?
               $n % 5 > 0 ?
                 'A''B''F''Z';
    last if length($fz) > length($gs) * length(@_);
    if ($fz =~ /^.*($pat)$/) {
      my $rhs = $1;
      my $lhs = substr($fz0rindex($fz$rhs) + 1);
      $m = [$lhs$rhsif !defined($m) || length(@{$m}[1]gt length($rhs);
    }
    $n++;
  }
  return [] unless $m;
  
  my ($lhs$rhs) = @{$m};
  $lhs = length($lhs);
  $rhs = $lhs + length($rhs||'') - 1;
  return [$lhs$rhs];
}

is_deeply(inv_fizzbuzz('fizz'), [33]);
is_deeply(inv_fizzbuzz('buzz'), [55]);
is_deeply(inv_fizzbuzz('fizz''buzz'), [910]);
is_deeply(inv_fizzbuzz('buzz''fizz'), [56]);
is_deeply(inv_fizzbuzz('fizz''fizz''buzz'), [610]);
is_deeply(inv_fizzbuzz('fizz''fizz'), [69]);
is_deeply(inv_fizzbuzz('fizz''buzz''fizz'), [36]);
is_deeply(inv_fizzbuzz('fizzbuzz''fizz'), [1518]);
is_deeply(inv_fizzbuzz('fizzbuzz''fizz'), [1518]);
is_deeply(inv_fizzbuzz('buzz''buzz'), []);

done_testing;
vimスクリプト版はkoronさんが書いてた。
koron/zzub-zzif-vim ツキ GitHub
https://github.com/koron/zzub-zzif-vim
Posted at by



2012/05/10


mattn/go-mruby - GitHub

go-mruby make interface to embed mruby into go.

https://github.com/mattn/go-mruby
Matzさんにヒントも貰いながら、さっき出来ました。
メソッドは RunEval があり、mrubyの評価値を取得する事が出来ます。Go言語から渡した引数が ARGV になります。なので mruby 上では String とは限りません。
package main

import "github.com/mattn/go-mruby"

func main() {
    mrb := mruby.New()
    defer mrb.Close()

    println(mrb.Eval(`"hello " + ARGV[0]`"mruby").(string))

    for _, i := range mrb.Eval(`ARGV.map {|x| x + 1}`123).([]interface{}) {
        println(i.(int32)) // 2 3 4
    }
}
良かったら遊んでみて下さい。
Posted at by



2012/05/09


しなちく 「お前それ、シナチクって言いたかっただけやろ」

...
...
...
...

「そうですが、何か」

mruby で mruby-httpmruby-uv を使い、sinatra ライクなのが動くまでに到った。
ショボいアプリケーションなら書ける様になったけど、mrubyはまだファイルIOが出来ないのでアプリと呼べる様な物が動くにはまだまだ先。
require 'HTTP'
require 'UV'

# {{{
module Sinatic
  @routes = { 'GET' => [], 'POST' => [] }
  def self.route(method, path, opts, &block)
    @routes[method] << [path, opts, block]
  end
  def self.do(r)
    @routes[r.method].each {|path|
      if path[0] == r.path
        param = {}
        r.body.split('&').each {|x|
          tokens = x.split('=')
          param[tokens[0]] = HTTP::URL::decode(tokens[1])
        }
        body = path[2].call(r, param)
        return [
          "HTTP/1.0 200 OK",
          "Content-Type: text/html; charset=utf-8",
          "Content-Length: #{body.size}",
          """"].join("\r\n") + body
      end
    }
    return "HTTP/1.0 404 Not Found\r\nContent-Length: 10\r\n\r\nNot Found\n"
  end
  def self.run()
    s = UV::TCP.new()
    s.bind(UV::ip4_addr('127.0.0.1'8888))
    s.data = []
    s.listen(50) {|x|
      return if x != 0
      c = s.accept()
      c.read_start {|b|
        h = HTTP::Parser.new()
        h.parse_request(b) {|r|
          i = b.index("\r\n\r\n") + 4
          r.body = b.slice(i, b.size - i)
          c.write(::Sinatic.do(r)) {|x| c.close }
        }
      }
      s.data << c
    }
    while 1 do
      NOTE: must be call run_once to run GC.
      UV::run_once()
    end
  end
end

module Kernel
  def get(path, opts={}, &block)
    ::Sinatic.route 'GET', path, opts, &block
  end
  def post(path, opts={}, &block)
    ::Sinatic.route 'POST', path, opts, &block
  end
end
# }}}

get "/foo.js" do
'$(function() {
  $("#foo").text("hello world");
})'
end

get "/" do
'
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="/foo.js"></script>
<div id="foo"></div>
<form action="/add" method="post">
<label for="name"/>お名前</label>
<input type="text" id="name" name="name" value="">
<input type="submit">
</form>
'
end

post "/add" do |rparam|
"
<meta http-equiv=refresh content='2; URL=/'>
通報しますた「#{param['name']}
"
end


Sinatic.run

# vim: set fdm=marker:
Posted at by