锁定老帖子 主题:Ruby每周一测 - 发牌趣题擂台
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2010-08-18
最后修改:2010-08-18
有52张扑克牌要随机发牌给四个玩家,并且四个玩家牌的数量是相同的? 用最精简的ruby语言写出来 结果示例: => [[43, 39, 47, 44, 46, 17, 38, 30, 16, 9, 25, 23, 51], [37, 11, 26, 45, 28, 27 , 35, 29, 41, 15, 5, 20, 21], [18, 7, 13, 42, 36, 31, 22, 2, 12, 3, 32, 8, 1], [ 49, 48, 10, 4, 33, 14, 34, 40, 24, 50, 52, 19, 6]] 其实是一则看到 求职版的这篇 今天面试遇到了雷人面试题求解 所启发,再则是延续下"Ruby每周一测"这个系列,三是活跃下本版气氛。 以下是我的解答版本,欢迎PK : (1..52).sort_by{rand}.inject([1,[],[],[],[]]){|r,i|r[(r[0]+=1)%4+1]<<i;r}[1..5] 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2010-08-18
(1..4).map{|i| (1..52).to_a.shuffle.indexes(i..i + 13)} |
|
返回顶楼 | |
发表时间:2010-08-18
Hooopo 写道 (1..4).map{|i| (1..52).to_a.shuffle.indexes(i..i + 13)} 虎炮你的代码有bug,每次shuffle出来的数组都不一样的 |
|
返回顶楼 | |
发表时间:2010-08-18
(1..52).sort_by{rand}.each_slice(13).map |
|
返回顶楼 | |
发表时间:2010-08-18
花花公子 写道 Hooopo 写道 (1..4).map{|i| (1..52).to_a.shuffle.indexes(i..i + 13)} 虎炮你的代码有bug,每次shuffle出来的数组都不一样的 bug了。。。 |
|
返回顶楼 | |
发表时间:2010-08-18
swordray 写道 (1..52).sort_by{rand}.each_slice(13).map 这个给力,给你加了3票 |
|
返回顶楼 | |
发表时间:2010-08-18
Hooopo 写道 (1..4).map{|i| (1..52).to_a.shuffle.indexes(i..i + 13)} rainchen 写道 swordray 写道 (1..52).sort_by{rand}.each_slice(13).map 这个给力,给你加了3票 我在加点力. 引用 Recipe 4.10. Shuffling an Array
Problem You want to put the elements of an array in random order. Solution The simplest way to shuffle an array (in Ruby 1.8 and above) is to sort it randomly: [1,2,3].sort_by { rand } # => [1, 3, 2] This is not the fastest way, though. Discussion It's hard to beat a random sort for brevity of code, but it does a lot of extra work. Like any general sort, a random sort will do about n log n variable swaps. But to shuffle a list, it suffices to put a randomly selected element in each position of the list. This can be done with only n variable swaps. class Array def shuffle! each_index do |i| j = rand(length-i) + i self[j], self[i] = self[i], self[j] end end def shuffle dup.shuffle! end end If you're shuffling a very large list, either Array#shuffle or Array#shuffle! will be significantly faster than a random sort. Here's a real-world example of shuffling using Array#shuffle: class Card def initialize(suit, rank) @suit = suit @rank = rank end def to_s "#{@suit} of #{@rank}" end end class Deck < Array attr_reader :cards @@suits = %w{Spades Hearts Clubs Diamonds} @@ranks = %w{Ace 2 3 4 5 6 7 8 9 10 Jack Queen King} def initialize @@suits.each { |suit| @@ranks.each { |rank| self << Card.new(rank, suit) } } end end deck = Deck.new deck.collect { |card| card.to_s } # => ["Ace of Spades", "2 of Spades", "3 of Spades", "4 of Spades",…] deck.shuffle! deck.collect { |card| card.to_s } # => ["6 of Clubs", "8 of Diamonds", "2 of Hearts", "5 of Clubs",…] See Also * Recipe 2.5, "Generating Random Numbers" * The Facets Core library provides implementations of Array#shuffle and Array#shuffle! (1..52).to_a.shuffle.each_slice(13).map |
|
返回顶楼 | |
发表时间:2010-08-18
swordray 写道 (1..52).sort_by{rand}.each_slice(13).map 引用的回复已经非常短了. 我蛋疼的提供另一种思路... (1..52).to_a.shuffle.each_slice(4).inject{|r,x| r.zip x}.each{|s| s.flatten!} 从一次分好到. 分好后再zip.. 蛋疼无极限. 如果没要求格式的话. 可以不需要最后面的 each .. zip一次其实也分好了. 嗯.. |
|
返回顶楼 | |
发表时间:2010-08-18
最后修改:2010-08-18
在rails里也可以这样
(1..52).to_a.shuffle.in_groups_of(13) |
|
返回顶楼 | |
发表时间:2010-08-18
最后修改:2010-08-18
Saito 写道 引用的回复已经非常短了. 我蛋疼的提供另一种思路... (1..52).to_a.shuffle.each_slice(4).inject{|r,x| r.zip x}.each{|s| s.flatten!} 从一次分好到. 分好后再zip.. 蛋疼无极限. 如果没要求格式的话. 可以不需要最后面的 each .. zip一次其实也分好了. 嗯.. (1..52).to_a.shuffle.each_slice(13).map 这样不久行了么 |
|
返回顶楼 | |