红宝石100门返回100零

时间:2016-10-11 04:37:22

标签: ruby null rosetta-code

我正在解决Ruby中的'100 doors' problem from Rosetta Code问题。简言之,将

  • 有100个门,全部关闭,指定为1到100
  • 进行100遍,指定为1至100
  • 在第i个通行证上,每一个门都被打开":如果它关闭则打开,如果它打开则关闭
  • 确定完100次通过后每扇门的状态。

因此,在第一次通过时,所有门都打开了。在第二次通过偶数门关闭。在i上切换i%3 == 0的第三个传递门,等等。

这是我尝试解决问题的方法。

visit_number = 0
door_array = []
door_array = 100.times.map {"closed"}

until visit_number == 100 do
  door_array = door_array.map.with_index { |door_status, door_index|
    if (door_index + 1) % (visit_number + 1) == 0
      if door_status == "closed"
        door_status = "open"
      elsif door_status == "open"
        door_status = "closed"
      end
    end
  }
  visit_number += 1
end

print door_array

但是当我运行它时,它会一直向我打印一个100 nil的数组:Look at all this nil !

我做错了什么?

4 个答案:

答案 0 :(得分:0)

这就是你的if条款返回的内容。只需显式添加返回值即可。

until visit_number == 100 do
  door_array = door_array.map.with_index { |door_status, door_index|
    if (door_index + 1) % (visit_number + 1) == 0
      if door_status == "closed"
        door_status = "open"
      elsif door_status == "open"
        door_status = "closed"
      end
    end
    door_status
  }
  visit_number += 1
end

OR:

1.upto(10) {|i| door_array[i*i-1] = 'open'}

答案 1 :(得分:0)

问题是外部if块在条件不满足时没有显式返回任何内容(因此隐式返回nil

快速修复:

visit_number = 0
door_array = []
door_array = 100.times.map {"closed"}

until visit_number == 100 do
  door_array = door_array.map.with_index { |door_status, door_index|
    if (door_index + 1) % (visit_number + 1) == 0
      if door_status == "closed"
        door_status = "open"
      elsif door_status == "open"
        door_status = "closed"
      end
    else  #<------------- Here
      door_status  #<------------- And here
    end
  }
  visit_number += 1
end

print door_array

答案 2 :(得分:0)

考虑以下方法:

door_array.map { |door|
  case door
  when "open"
    "closed"
  when "closed"
    "open"
  end
}

rule = { "open" => "closed", "closed" => "open" }
door_array.map { |door| rule[door] }

door_array.map { |door| door == 'open' ? 'closed' : 'open' }

答案 3 :(得分:0)

<强>代码

require 'prime'

def even_nbr_divisors?(n)
  return false if n==1
  arr = Prime.prime_division(n).map { |v,exp| (0..exp).map { |i| v**i } }
  arr.shift.product(*arr).map { |a| a.reduce(:*) }.size.even?
end

closed, open = (1..100).partition { |n| even_nbr_divisors?(n) }
closed #=> [ 2, 3, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 
       #    23, 24, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 38, 39, 40,
       #    41, 42, 43, 44, 45, 46, 47, 48, 50, 51, 52, 53, 54, 55, 56, 57,
       #    58, 59, 60, 61, 62, 63, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
       #    75, 76, 77, 78, 79, 80, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
       #    92, 93, 94, 95, 96, 97, 98, 99],
open   #= [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] 

<强>解释

所有门最初都关闭。考虑第24门。它在以下过程中切换:

pass  1: opened
pass  2: closed
pass  3: opened
pass  4: closed
pass  6: opened
pass  8: closed
pass 12: opened
pass 24: closed

请注意,每个24的除数都会切换一次门。因此,如果我们有一个方法divisors(n)返回了n的除数数组,我们可以按如下方式确定切换次数:

nbr_toggles = divisors(24).size
  #=> [1,2,3,4,6,8,12,24].size
  #=> 8

由于门被切换了偶数次,我们得出结论,在所有尘埃落定后,它将处于原始状态(关闭)。同样,对于n = 9

divisors(9).size
  #=> [1,3,9].size
  #=> 3

因此我们得出结论,#9门将在末尾开放,因为3是奇数。

divisors可以定义如下。

def divisors(n)
  arr = Prime.prime_division(n).map { |v,exp| (0..exp).map { |i| v**i } }
  arr.first.product(*arr[1..-1]).map { |a| a.reduce(:*) }
end

例如,

divisors 24
  #=> [1, 3, 2, 6, 4, 12, 8, 24] 
divisors 9
  #=> [1, 3, 9] 
divisors 1800
  #=> [1, 5, 25, 3, 15, 75, 9, 45, 225, 2, 10, 50, 6, 30, 150, 18, 90, 450,
  #    4, 20, 100, 12, 60, 300, 36, 180, 900, 8, 40, 200, 24, 120, 600, 72,
  #    360, 1800] 

由于我们只关心是否存在奇数或偶数的除数,我们可以改为写

def even_nbr_divisors?(n)
  return false if n==1
  arr = Prime.prime_division(n).map { |v,exp| (0..exp).map { |i| v**i } }
  arr.shift.product(*arr).map { |a| a.reduce(:*) }.size.even?
end

对于n = 24,步骤如下:

n   = 24
a   = Prime.prime_division(n)
  #=> [[2, 3], [3, 1]] 
arr = a.map { |v,exp| (0..exp).map { |i| v**i } }
  #=> [[1, 2, 4, 8], [1, 3]] 
b   = arr.shift
  #=> [1, 2, 4, 8] 
arr
  #=> [[1, 3]] 
c   = b.product(*arr)
  #=> [[1, 1], [1, 3], [2, 1], [2, 3], [4, 1], [4, 3], [8, 1], [8, 3]] 
d   = c.map { |a| a.reduce(:*) }
  #=> [1, 3, 2, 6, 4, 12, 8, 24] 
e   = d.size
  #=> 8 
e.even?
  #=> true 

最后,

(1..100).partition { |n| even_nbr_divisors?(n) }

返回上面显示的结果。