【问题标题】:Finding the sum of all the numbers between a and b: Ruby求 a 和 b 之间所有数字的总和:Ruby
【发布时间】:2020-09-17 08:32:09
【问题描述】:

已经在这个 Kata 上工作了很长一段时间,但仍然无法弄清楚我缺少什么。问题是给定两个整数 a 和 b,可以是正数或负数,找到包括它们之间的所有数字的总和并返回它。如果两个数相等,则返回 a 或 b。

到目前为止,这是我的解决方案的样子:

def get_sum(a,b)
  sum = [a+=b].sum      
  if sum == a or b 
    return a
  end 
end

这是输出结果:

Test Passed: Value == 1
Test Passed: Value == 3
Expected: 14, instead got: 4
Expected: 127759, instead got: 509
Expected: 44178, instead got: 444

我相信关键字是之间的所有数字,但我不确定如何在语法上编写它。 为了进一步说明,我在下面提供了一些示例。

get_sum(1, 0) == 1   # 1 + 0 = 1
get_sum(1, 2) == 3   # 1 + 2 = 3
get_sum(0, 1) == 1   # 0 + 1 = 1
get_sum(1, 1) == 1   # 1 Since both are same
get_sum(-1, 0) == -1 # -1 + 0 = -1
get_sum(-1, 2) == 2  # -1 + 0 + 1 + 2 = 2

https://www.codewars.com/kata/55f2b110f61eb01779000053/train/ruby

【问题讨论】:

  • 你能给出原始问题的链接吗?
  • return sum 在哪里?
  • 我对 ruby​​ 不熟悉,但我在你的代码中至少看到了三个非常可疑的地方。 1) 为什么会有a+=b? “包括它们在内的 a 和 b 之间的所有数字”表示 a + a+1 + a+2 + a+3 + ... + b。该列表中没有a+b。例如,如果a=100, b=120,那么a+b=220 没有帮助。 2)if sum == a or b。从ruby-doc.org/core-2.6.2/doc/syntax/precedence_rdoc.html 来看,这意味着“如果 sum == a,或者如果 b 为真”。你打算用这个测试做什么?我认为在 ruby​​ 中,除了falsenil 之外,一切都是真的,所以b 总是正确的。
  • 3) 在某些情况下,您会返回 a。在其他情况下,您不会返回任何东西。看起来你永远不会返回一笔款项。
  • 最后,知道对于任何非负的n1+2+...+n = n*(n+1)/2,我们得到:a + a+1 + a+2 + ... + b = a + a+1 + a+2 + ... + a+(b-a) = (b-a+1)*a + 1+2+...+(b-a) = (b-a+1)*a + (b-a)*(b-a+1)/2 = (b-a+1)*(a+(b-a)/2) = (b-a+1)*(a+b)/2,所以你可以写def get_sum(a,b) return (b-a+1)*(a+b)/2

标签: ruby algorithm


【解决方案1】:

你可以使用Arithmetic progression的公式:

def get_sum(a, b)
  a, b = b, a if a > b

  (b - a + 1) * (a + b) / 2
end

Active Support(Rails) extension for Range classmodern(>= 2.4) Ruby 做同样的事情。

因此,如果您的 Kata 站点使用 Rails 或现代 Ruby,您可以使用 @MBo 回答。通常此类站点会指定环境和解释器版本。

def get_sum(a, b)
  a, b = b, a if a > b

  (a..b).sum
end

【讨论】:

  • 是的,它似乎有效。请注意,它已经由 Range#sum (stackoverflow.com/questions/41449617/…) 以这种方式实现,所以它就像 MBo 的答案。
  • 我知道,已编辑帖子。我当前的项目使用旧的 Ruby 版本,但 ActiveSupport 仍然使用:sum + (actual_last - first + 1) * (actual_last + first) / 2
【解决方案2】:

除了 a=b 情况外,您的代码不会返回结果。另外 - [a+=b] 生成什么?具有单个元素a+b 的数组,所以它的总和就是a+b

创建一个范围并得到它的总和。

新增:参数排序

def get_sum(a,b)
    a, b = b, a if a > b
    return (a..b).sum      
end

print get_sum(1,3)
print get_sum(2,2)
print get_sum(-1,2)
print get_sum(3,-1)

>> 6 2 2 5

【讨论】:

  • 我不是投反对票的人,但我认为投反对票的理由是这个答案没有提供任何解释,也无助于 OP 了解他们的代码有什么问题。
  • 难以置信,真的这么简单吗?将来一定要记住这一点。
  • "Test.assert_equals(get_sum(5,-1),14)"时失败
  • 在两个测试用例失败的“codewars.com/kata/55f2b110f61eb01779000053/train/ruby”中测试
【解决方案3】:
def get_sum(a,b)
  sum = [a+=b].sum      
  if sum == a or b 
    return a
  end 
end

其他答案解释了你可以写什么,所以让我们检查你的代码:

  • a+=b 首先被调用。它基本上是a = a + b,因此它计算两个输入的总和,将其保存在a,并返回a
  • sum = [a].sum 创建一个包含一个元素的数组,并计算其总和(就是这一个元素)。所以sum = a
  • a 为真时a or b 只是a(即既不是false 也不是nil)。

这就是您的代码实际执行的操作:

def get_sum(a,b)
  a = a + b
  sum = a
  if sum == a
    return a
  end 
end

这只是:

def get_sum(a,b)
  a = a + b
  return a
end

或者:

def get_sum(a,b)
  a = a + b
end

或:

def get_sum(a,b)
  a + b
end

【讨论】:

    【解决方案4】:

    请在下面尝试并参考enter link description here

    def get_sum(a,b)
      return a if a == b  
      return (a..b).sum if b > a
      return (b..a).sum if a > b
    end
    

    测试:

    describe "Example Tests" do
      Test.assert_equals(get_sum(1,1),1)
      Test.assert_equals(get_sum(0,1),1)
      Test.assert_equals(get_sum(0,-1),-1)
      Test.assert_equals(get_sum(1,2),3)
      Test.assert_equals(get_sum(5,-1),14)
    end
    

    输出:

    Test Results:
     Example Tests
    Test Passed: Value == 1
    Test Passed: Value == -1
    Test Passed: Value == 3
    Test Passed: Value == 14
    You have passed all of the tests! :)
    

    【讨论】:

    • 保存比较:a, b = b, a if a > 0
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-27
    • 2022-10-05
    • 1970-01-01
    • 2017-12-24
    • 1970-01-01
    相关资源
    最近更新 更多