简体   繁体   English

尝试删除数组中的元素:Ruby

[英]Trying to remove elements in an array : Ruby

I'm working on a Codewar challenge in Ruby to remove elements from a array of strings.我正在 Ruby 中处理 Codewar 挑战,以从字符串数组中删除元素。 So far I have tried using the Array.delete_at(Array.index(value)) which is meant to remove the first occurrence of a repeating value from the array however that did not work.到目前为止,我已经尝试使用Array.delete_at(Array.index(value)) ,它旨在从数组中删除第一次出现的重复值,但是这不起作用。 I believe I may need to combine that with something else but not sure what.我相信我可能需要将它与其他东西结合起来,但不确定是什么。

these are what my tests currently look like when I run them:这些是我运行测试时当前的样子:

Expected: ["Hello", "Hello Again"], instead got: ["Hello"]
Expected: [1, 3, 5, 7, 9], instead got: [1]
Test Passed: Value == [[1, 2]]
Test Passed: Value == [["Goodbye"]]
Test Passed: Value == []

so far I'm using the .shift method, and that seems to have done half the job.到目前为止,我正在使用.shift方法,这似乎已经完成了一半的工作。 any suggestions on how I can target the whole substring.关于如何定位整个子字符串的任何建议。

def remove_every_other(arr)
  arr.shift(1) 
end

for more clarification please find the practise tests and link to the Kata below: https://www.codewars.com/kata/5769b3802ae6f8e4890009d2/train/ruby如需更多说明,请在下面找到练习测试和 Kata 链接: https : //www.codewars.com/kata/5769b3802ae6f8e4890009d2/train/ruby

Test.describe("Basic tests") do
  Test.assert_equals(remove_every_other(['Hello', 'Goodbye', 'Hello Again']),['Hello', 'Hello Again'])
  Test.assert_equals(remove_every_other([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),[1, 3, 5, 7, 9])
  Test.assert_equals(remove_every_other([[1, 2]]), [[1, 2]])
  Test.assert_equals(remove_every_other([['Goodbye'], {'Great': 'Job'}]), [['Goodbye']])
  Test.assert_equals(remove_every_other([]), [])
end

There's a ton of tools in Enumerable that make this trivial: Enumerable中有大量工具可以让这一切变得微不足道:

a = %w[ a b c d e f ]

a.each_slice(2).map(&:first)
# => ["a", "c", "e"]

Where that first carves up the array into pairs, then takes the first of each pair.首先将数组分割成对,然后获取每对中的第一个。

The problem with your shift approach is it does one operation, it doesn't iterate.您的shift方法的问题在于它只执行一项操作,而不是迭代。 You must work through the whole array to make this happen.必须遍历整个数组才能实现这一点。

Now you could use a combination of shift and an accumulator, but it's generally bad form to consume the array you're given when a more functional version exists.现在您可以使用shift和累加器的组合,但是当存在更多功能的版本时,使用给定的数组通常是不好的形式。 each_slice produces a new result, it doesn't alter the original, making it easier to coordinate in more complex code where that input value might be shared. each_slice产生一个新结果,它不会改变原始结果,从而更容易在可能共享输入值的更复杂代码中进行协调。

multiple solutions possible.可能有多种解决方案。 But - don't forget that you do not need to remove elements to solve this challenge - your method needs to return proper values that's all.但是 - 不要忘记您不需要删除元素来解决这个挑战 - 您的方法需要返回正确的值,仅此而已。 So following code should also work:所以下面的代码也应该有效:

   arr.select{ |e| arr.find_index(e).even? }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM