Ruby 斐波那契乘法 Table
Ruby Fibonacci Multiplication Table
Ruby斐波那契乘法Table这是一道面试题。
最近开始从事软件开发,遇到了这个挑战。你能告诉我下面,我如何用 Ruby 语言写斐波那契乘法 table 吗?在过去的几天里,我一直在尝试实现它,但似乎我面临着一堵砖墙,代码让我感到疯狂,而且我在面试中失败了,但目前这并不重要。我将不胜感激任何帮助。非常感谢。
应用程序控制器:
class ApplicationController < Sinatra::Base
configure do
set :public_folder, 'public'
set :views, 'app/views'
end
get '/' do
@time_of_day = Time.now
erb :index
end
end
斐波那契控制器:
class FibonacciController < ApplicationController
get '/fibonacci' do
place = params[:place].to_i
@sequence = fib(place)
erb :fibonacci
end
def fib(place)
res = []
a = 0
b = 1
while b < place do
res << b
a,b = b,a+b
end
res
end
end
Fibonacci.erb
<div class="container">
<h1> Fibonacci sequence: </h1>
<div class="sub-container">
<p> Generated fibonacci sequence: </p>
<%= @sequence.join(', ') %>
</div>
</div>
Index.erb
<div class="container">
Date and time: <%= @time_of_day %>
<br>
<h1> Fibonacci Multiplication Table </h1>
<p> Enter your number below: </p>
<form method="GET" action="/fibonacci">
<label for="sequence">
<input type="integer" name="place" placeholder="Insert your number">
<input type="submit">
</form>
</div>
(这是本次挑战的最终目标)
FibonacciController 代码
class FibonacciController < ApplicationController
get '/fibonacci' do
place = params[:place].to_i
sequence = fib(place)
@table = generate_table(sequence)
erb :fibonacci
end
def fib(place)
return [] if place <= 0
a = 0
b = 1
res = [a]
while res.length < place do
res << b
a,b = b, a+b
end
res
end
def generate_table(sequence)
return [] if sequence.length.zero?
cols = []
(sequence.length + 1).times do |row|
row_data = []
(sequence.length + 1).times do |col|
row_data << generate_table_element(row, col, sequence)
end
cols << row_data
end
cols
end
def generate_table_element(row, col, sequence)
return '_' if row.zero? && col.zero?
return sequence[col - 1] if row.zero?
return sequence[row - 1] if col.zero?
sequence[col - 1] * sequence[row - 1]
end
end
并且在 erb 文件中
<p> Generated fibonacci sequence: </p>
<% @table.each do |table_row| %>
<%= table_row.join(',') %>
<br/>
<% end %>
Ruby斐波那契乘法Table这是一道面试题。
最近开始从事软件开发,遇到了这个挑战。你能告诉我下面,我如何用 Ruby 语言写斐波那契乘法 table 吗?在过去的几天里,我一直在尝试实现它,但似乎我面临着一堵砖墙,代码让我感到疯狂,而且我在面试中失败了,但目前这并不重要。我将不胜感激任何帮助。非常感谢。
应用程序控制器:
class ApplicationController < Sinatra::Base
configure do
set :public_folder, 'public'
set :views, 'app/views'
end
get '/' do
@time_of_day = Time.now
erb :index
end
end
斐波那契控制器:
class FibonacciController < ApplicationController
get '/fibonacci' do
place = params[:place].to_i
@sequence = fib(place)
erb :fibonacci
end
def fib(place)
res = []
a = 0
b = 1
while b < place do
res << b
a,b = b,a+b
end
res
end
end
Fibonacci.erb
<div class="container">
<h1> Fibonacci sequence: </h1>
<div class="sub-container">
<p> Generated fibonacci sequence: </p>
<%= @sequence.join(', ') %>
</div>
</div>
Index.erb
<div class="container">
Date and time: <%= @time_of_day %>
<br>
<h1> Fibonacci Multiplication Table </h1>
<p> Enter your number below: </p>
<form method="GET" action="/fibonacci">
<label for="sequence">
<input type="integer" name="place" placeholder="Insert your number">
<input type="submit">
</form>
</div>
(这是本次挑战的最终目标)
FibonacciController 代码
class FibonacciController < ApplicationController
get '/fibonacci' do
place = params[:place].to_i
sequence = fib(place)
@table = generate_table(sequence)
erb :fibonacci
end
def fib(place)
return [] if place <= 0
a = 0
b = 1
res = [a]
while res.length < place do
res << b
a,b = b, a+b
end
res
end
def generate_table(sequence)
return [] if sequence.length.zero?
cols = []
(sequence.length + 1).times do |row|
row_data = []
(sequence.length + 1).times do |col|
row_data << generate_table_element(row, col, sequence)
end
cols << row_data
end
cols
end
def generate_table_element(row, col, sequence)
return '_' if row.zero? && col.zero?
return sequence[col - 1] if row.zero?
return sequence[row - 1] if col.zero?
sequence[col - 1] * sequence[row - 1]
end
end
并且在 erb 文件中
<p> Generated fibonacci sequence: </p>
<% @table.each do |table_row| %>
<%= table_row.join(',') %>
<br/>
<% end %>