Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

Rubequeローカル実行確認用テンプレート

More than 5 years have passed since last update.

※Rubequeは『sicuro』というライブラリを使ってコードを実行しているっぽいので
『sicuro』のgemをインストールしてください。

ローカルでも『sicuro』を使った実行確認をしたい場合に使うテンプレートです。
=== Code Area ===間にコードを書いて実行します。

rubeque_template.rb
require "bundler/setup"
require "sicuro"

TIMELIMIT = 15
MEMLIMIT  = 30

Sicuro.setup(TIMELIMIT, MEMLIMIT)

common = <<CODE
def assert_equal(x, y, message = nil)
  if x != y
    raise message ? message : "The value '\#{x}' does not equal '\#{y}'."
  else
    return true
  end
end
CODE

puts Sicuro.eval(<<CODE)
#{common}
# === Code Area ===

# === Code Area ===
CODE

Rubequeサイトでは以下のコードがFiberError: can't alloc machine stack to fiberとなる。
ローカルではエラーが再現しなかった。

prime_factorization.rb
require "bundler/setup"
require "sicuro"

TIMELIMIT = 1
MEMLIMIT  = 1

Sicuro.setup(TIMELIMIT, MEMLIMIT)

common = <<CODE
def assert_equal(x, y, message = nil)
  if x != y
    raise message ? message : "The value '\#{x}' does not equal '\#{y}'."
  else
    return true
  end
end
CODE

puts Sicuro.eval(<<CODE)
#{common}
# === Code Area ===
def prime_factorization(num)

  prime =  Enumerator.new do |y|
    mem = []
    is_prime = lambda { |e| mem.find { |_| e.gcd(_) != 1 } }
    2.upto(num) do |n|
      unless is_prime[n]
        mem << n
        y << n
      end
    end
  end

  o = []
  loop do
    n = prime.next
    if num % n == 0
      num /= n
      o << n
      prime.rewind
    end   
  end
  o

end

p assert_equal prime_factorization(42), [2, 3, 7]
p assert_equal prime_factorization(44), [2, 2, 11]
p assert_equal prime_factorization(75), [3, 5, 5]
p assert_equal prime_factorization(123456), [2, 2, 2, 2, 2, 2, 3, 643]
# === Code Area ===
CODE

Herokuとの環境問題っぽいなー

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away