Ruby on rails 序列生成器,kata,ruby

Ruby on rails 序列生成器,kata,ruby,ruby-on-rails,ruby,algorithm,enumerator,Ruby On Rails,Ruby,Algorithm,Enumerator,我正在用Ruby做这个“序列生成器”Kata,不知怎的,我不知道如何做到这一点。但我试过了。请给我跳支舞。谢谢大家! 编写一个生成器序列_gen(JavaScript中的sequenceGen),给定序列的第一个项将生成(可能)无限数量的项,其中每个后续项是前x项的总和,其中x是初始参数的数量(此类序列的示例为斐波那契、Tribonacci和Lucas数序列) 示例: fib = sequence_gen(0, 1) # returns an Enumerator fib.next = 0 #

我正在用Ruby做这个“序列生成器”Kata,不知怎的,我不知道如何做到这一点。但我试过了。请给我跳支舞。谢谢大家!

编写一个生成器序列_gen(JavaScript中的sequenceGen),给定序列的第一个项将生成(可能)无限数量的项,其中每个后续项是前x项的总和,其中x是初始参数的数量(此类序列的示例为斐波那契、Tribonacci和Lucas数序列)

示例:

fib = sequence_gen(0, 1) # returns an Enumerator
fib.next = 0 # first term (provided)
fib.next = 1 # second term (provided)
fib.next = 1 # third term (sum of first and second terms)
fib.next = 2 # fourth term (sum of second and third terms)
fib.next = 3 # fifth term (sum of third and fourth terms)
fib.next = 5 # sixth term (sum of fourth and fifth terms)
fib.next = 8 # seventh term (sum of fifth and sixth terms)


trib = sequence_gen(0,1,1) # returns an Enumerator
trib.next = 0 # first term (provided)
trib.next = 1 # second term (provided)
trib.next = 1 # third term (provided)
trib.next = 2 # fourth term (sum of first, second and third terms)
trib.next = 4 # fifth term (sum of second, third and fourth terms)
trib.next = 7 # sixth term (sum of third, fourth and fifth terms)

lucas = sequence_gen(2,1) # returns an Enumerator
lucas.take(10) = [2, 1, 3, 4, 7, 11, 18, 29, 47, 76]
我的解决方案:

def sequence_gen(e, vs)
  while true
    begin
      vs = e.next_values
      return $!.result
    end
    y = yield(*vs)
    e.feed y
  end
end
最好的


代码

您可以在枚举器中使用无限循环,如下所示:

def sequence_gen(*args)
  Enumerator.new do |y|
    args.each { |e| y << e }
    loop do    
      y << sum = args.inject(:+)
      args = args.drop(1).push(sum) 
    end    
  end
end
序列的前几个项是传递给
sequence\u gen(0,1)
的参数,即
0
1
。这意味着我们首先需要按顺序生成参数0和1。这就是上面的代码所做的

无限循环

产出:

The first seven terms of fib = sequence_gen(0, 1) are:
0
1
1
2
3
5
8

The first six terms of trib = sequence_gen(0,1,1) are:
0
1
1
2
4
7

The first ten terms of lucas = sequence_gen(2,1) are:
[2, 1, 3, 4, 7, 11, 18, 29, 47, 76]

“y”和“y”之间的关系是什么?看看这个,了解更多关于枚举数的信息。据我所知,“y@HanJiang”我添加了一些解释,以帮助您进一步理解我的代码。枚举数在一开始是一个棘手的概念,但很值得理解。因此,如果需要,请慢慢来,先做一些简单的示例。谢谢很
loop do    
  y << sum = args.inject(:+)
  args = args.push(sum).drop(1) 
end
puts 'The first seven terms of fib = sequence_gen(0, 1) are:'
fib = sequence_gen(0, 1) # returns an Enumerator
p fib.next #=> 0 # first term (provided)
p fib.next #=> 1 # second term (provided)
p fib.next #=> 1 # third term (sum of first and second terms)
p fib.next #=> 2 # fourth term (sum of second and third terms)
p fib.next #=> 3 # fifth term (sum of third and fourth terms)
p fib.next #=> 5 # sixth term (sum of fourth and fifth terms)
p fib.next #=> 8 # seventh term (sum of fifth and sixth terms)
puts

puts 'The first six terms of trib = sequence_gen(0,1,1) are:'
trib = sequence_gen(0,1,1) # returns an Enumerator
p trib.next #=> 0 # first term (provided)
p trib.next #=> 1 # second term (provided)
p trib.next #=> 1 # third term (provided)
p trib.next #=> 2 # fourth term (sum of first, second and third terms)
p trib.next #=> 4 # fifth term (sum of second, third and fourth terms)
p trib.next #=> 7 # sixth term (sum of third, fourth and fifth terms)
puts

puts 'The first ten terms of lucas = sequence_gen(2,1) are:'
lucas = sequence_gen(2,1) # returns an Enumerator
p lucas.take(10) #=> [2, 1, 3, 4, 7, 11, 18, 29, 47, 76]
The first seven terms of fib = sequence_gen(0, 1) are:
0
1
1
2
3
5
8

The first six terms of trib = sequence_gen(0,1,1) are:
0
1
1
2
4
7

The first ten terms of lucas = sequence_gen(2,1) are:
[2, 1, 3, 4, 7, 11, 18, 29, 47, 76]