请帮助我的“随机播放”红宝石代码

发布于 2024-10-02 19:30:29 字数 474 浏览 7 评论 0原文

这就是问题

随机播放。现在你已经完成了你的 新的排序算法,怎么样 对面的?编写一个 shuffle 方法 接受一个数组并返回一个完全 洗牌版本。一如既往,你会 想测试一下,但是测试这个 更棘手的是:你如何测试 确保你得到一个完美的 洗牌?你甚至会说什么 完美的洗牌会是什么?现在测试 它。

这是我的代码答案:

def shuffle arr
    x = arr.length
while x != 0
        new_arr = []
    rand_arr = (rand(x))
    x--
    new_arr.push rand_arr
    arr.pop rand_arr
end

new_arr

end

puts (shuffle ([1,2,3]))

我的错误是什么?为什么这段代码不起作用?

this is the question

Shuffle. Now that you’ve finished your
new sorting algorithm, how about the
opposite? Write a shuffle method that
takes an array and returns a totally
shuffled version. As always, you’ll
want to test it, but testing this one
is trickier: How can you test to make
sure you are getting a perfect
shuffle? What would you even say a
perfect shuffle would be? Now test for
it.

This is my code answer:

def shuffle arr
    x = arr.length
while x != 0
        new_arr = []
    rand_arr = (rand(x))
    x--
    new_arr.push rand_arr
    arr.pop rand_arr
end

new_arr

end

puts (shuffle ([1,2,3]))

What are my mistakes? Why doesn't this code work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

放我走吧 2024-10-09 19:30:30

除了其他小错误之外,您似乎不了解 pop 和 push 正在做什么(从数组末尾获取或添加一些项目)。

您可能正在尝试编写如下所示的内容。

def shuffle arr
    x = arr.length
    new_arr = []
    while x != 0
        randpos = rand(x)
        x = x-1
        item = arr[randpos]
        new_arr.push item
        arr[randpos] = arr[x]
        arr.pop
    end

    new_arr

end

puts (shuffle ([1,2,3]))

Beside minor other errors you seems not to understand what pop and push are doing (taking or adding some items from the end of the array).

You are probably trying to write something like below.

def shuffle arr
    x = arr.length
    new_arr = []
    while x != 0
        randpos = rand(x)
        x = x-1
        item = arr[randpos]
        new_arr.push item
        arr[randpos] = arr[x]
        arr.pop
    end

    new_arr

end

puts (shuffle ([1,2,3]))
清晰传感 2024-10-09 19:30:30

您的索引与您的值混淆了。当您执行 new_arr.push rand_arr 时,您会将您想出的任何随机索引作为值放在 new_arr 的末尾。您要做的是 new_arr.push arr[rand_arr],其中 arr[rand_arr]rand_arr 中索引 rand_arr 处的值>arr。

You're getting your indexes mixed up with your values. When you do new_arr.push rand_arr, you're putting whatever random index you came up with as a value on the end of new_arr. What you meant to do is new_arr.push arr[rand_arr], where arr[rand_arr] is the value at the index rand_arr in arr.

芯好空 2024-10-09 19:30:30

Ruby 1.8.7 和 1.9.2 有一个内置的 Array#shuffle 方法。

Ruby 1.8.7 and 1.9.2 have a built-in Array#shuffle method.

人间☆小暴躁 2024-10-09 19:30:30

马克·托马斯的答案的一个变体。由于删除操作性能的原因,他的算法对于大型数组可能会非常慢。

class Array
  def shuffle!
    size.downto(1) do |n|
       index=rand(n)
       # swap elements at index and the end
       self[index], self[size-1] = self[size-1],self[index]
    end
    self
  end
end

puts [1,2,3].shuffle!

这个算法是O(size),而Mark的算法是O(size^2)。在我的计算机上,Mark 的答案需要 400 秒才能对我机器上的 1,000,000 个元素的数组进行洗牌,而我的方法只需 0.5 秒。

A variant of Mark Thomas's answer. His algorithm can be quite slow with a large array, due to delete operation performance.

class Array
  def shuffle!
    size.downto(1) do |n|
       index=rand(n)
       # swap elements at index and the end
       self[index], self[size-1] = self[size-1],self[index]
    end
    self
  end
end

puts [1,2,3].shuffle!

This algorithm is O(size), while Mark's algorithm is O(size^2). On my computer, Mark's answer takes 400 seconds to shuffle an array of 1,000,000 elements on my machine, versus 0.5 seconds with my method.

谜兔 2024-10-09 19:30:29

这是一个更加 Rubyish 的版本:

class Array
  def shuffle!
    size.downto(1) { |n| push delete_at(rand(n)) }
    self
  end
end

puts [1,2,3].shuffle!

Here's a far more Rubyish version:

class Array
  def shuffle!
    size.downto(1) { |n| push delete_at(rand(n)) }
    self
  end
end

puts [1,2,3].shuffle!
话少情深 2024-10-09 19:30:29

这是更简洁的编写方式:

def shuffle(arr)
  new_arr = []

  while (arr.any?) do 
    new_arr << arr.delete_at(rand(arr.length))
  end

  new_arr
end

以及一些测试:

5.times do
  puts shuffle((1..5).to_a).join(',')
end

>> 4,2,1,3,5
>> 3,2,1,4,5
>> 4,2,5,1,3
>> 5,2,1,4,3
>> 4,3,1,5,2

Here's a more concise way of writing it:

def shuffle(arr)
  new_arr = []

  while (arr.any?) do 
    new_arr << arr.delete_at(rand(arr.length))
  end

  new_arr
end

And some tests:

5.times do
  puts shuffle((1..5).to_a).join(',')
end

>> 4,2,1,3,5
>> 3,2,1,4,5
>> 4,2,5,1,3
>> 5,2,1,4,3
>> 4,3,1,5,2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文