【问题标题】:ruby - how to reorder an array based on another array?ruby - 如何根据另一个数组重新排序数组?
【发布时间】:2012-02-29 06:17:14
【问题描述】:

我正在编写一个“洗牌程序”来玩 Ruby。将其视为我分配给自己的个人作业以了解更多信息:)

我想要的输出在这里:

----Triple Cut Deck on 3rd and 5th cards---------
  -- reset
Number: 1,  Position: 3, Suit: Clubs, Card: 3
Number: 2,  Position: 4, Suit: Clubs, Card: 4
Number: 3,  Position: 1, Suit: Clubs, Card: 5
Number: 4,  Position: 2, Suit: Clubs, Card: 6
Number: 5,  Position: 5, Suit: Clubs, Card: Ace
Number: 6,  Position: 6, Suit: Clubs, Card: 2

但我得到了:

----Triple Cut Deck on 3rd and 5th cards---------
  -- reset
Number: 1,  Position: 3, Suit: Clubs, Card: 3
Number: 2,  Position: 4, Suit: Clubs, Card: 4
Number: 3,  Position: 5, Suit: Clubs, Card: 5
Number: 4,  Position: 5, Suit: Clubs, Card: 5
Number: 5,  Position: 6, Suit: Clubs, Card: 6
Number: 6,  Position: 6, Suit: Clubs, Card: 6

基本上我想要的是重新排序卡片,以便“Ace, 2, 3, 4, 5, 6”的卡片顺序从“1,2,3,4,5”更改为“5” , 6, 3, 4, 1, 2"。换句话说,底部的顶部两张牌(按顺序),顶部和中间的底部两张牌保持不变。这是 3 路切割的一个版本.

我很难让这个数组“重新排序”正常工作。 现在 card 'rank' 和 card_position 像上图一样乱七八糟,有重复等等。

class Card
  RANKS = %w(Ace 2 3 4 5 6 7 8 9 10 J Q K )
  SUITS = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
  SCORES = [1..54]
  attr_accessor :rank, :suit, :card_position

  def initialize(id, rank='', suit='', card_position=0)
    self.card_position =  id
    self.rank = RANKS[(id % 14)-1]
    self.suit = SUITS[(id / 14)]
  end
end

class Deck
  DECK_SIZE = 6
  attr_accessor :cards
  def initialize
    self.cards = (1..DECK_SIZE).to_a.collect { |id| Card.new(id) }
    @deck = cards
  end

  def process_cards

    puts "\n----Triple Cut Deck on 3rd and 5th cards---------"
    self.triple_cut_deck(3, 5, true)
    self.show_deck

  end

  def show_deck
    @deck.sort_by!(&:card_position).each_with_index do |card, index|
      puts 'Number: ' + (index+1).to_s + ",  Position: #{card.card_position.to_s}, Suit: #{card.suit.to_s}, Card: #{card.rank.to_s}"
    end
  end

  def triple_cut_deck(top_cut, bottom_cut, reset_deck=false)
    reset_the_deck(reset_deck)

    top_cut-= 1
    bottom_cut-= 1
    deck_array_size = DECK_SIZE-1

    @new_deck = []
    @new_deck[0..1] = @deck[4..5]
    @new_deck[2..3] = @deck[2..3]
    @new_deck[4..5] = @deck[0..1]

    DECK_SIZE.times do |card|
      @deck[card].card_position= @new_deck[card].card_position
      @deck[card].card_position= @new_deck[card].card_position
      @deck[card].card_position= @new_deck[card].card_position
    end
  end


  def reset_the_deck(reset_deck)
    puts reset_deck == true ? "  -- reset" : 'no-reset'
    initialize if (true && reset_deck)
  end

end

【问题讨论】:

标签: ruby arrays


【解决方案1】:

您试图将位置作为方法和数组中的实际位置。这是很多簿记。切割和三重或四重切割基本上是同一回事。

Ranks = %w(Ace 2 3 4 5 6 7 8 9 10 J Q K )
Suits = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
Card = Struct.new(:rank, :suit)
deck = Suits.product(Ranks).map{|suit, rank| Card.new(rank, suit) }
hand = deck[0, 6]
def cut(hand, *at) #returns a new hand, multi-cutted 
  indices = (at.map{|i| i-1}+[0, hand.size]).sort
  res = indices.each_cons(2).map{|i1,i2| hand[i1..i2-1] }
  res.reverse.flatten
end
p cut(hand, 3, 5)

输出:

 [#<struct Card rank="Ace", suit="Clubs">, #<struct Card rank="2", suit="Clubs">,
    #<struct Card rank="3", suit="Clubs">, #<struct Card rank="4", suit="Clubs">,
    #<struct Card rank="5", suit="Clubs">, #<struct Card rank="6", suit="Clubs">]

    [#<struct Card rank="5", suit="Clubs">, #<struct Card rank="6", suit="Clubs">,
    #<struct Card rank="3", suit="Clubs">, #<struct Card rank="4", suit="Clubs">,
    #<struct Card rank="Ace", suit="Clubs">, #<struct Card rank="2", suit="Clubs">]

【讨论】:

    【解决方案2】:

    这是你想要的吗?

    a = [1,2,3,4,5,6]
    
    n = 2
    b = a[-n, n] + a[n..-(n+1)] + a[0,n]
    
    p a # => [1,2,3,4,5,6]
    p b # => [5,6,3,4,1,2]
    

    【讨论】:

    • 完全正确。语法很繁琐。谢谢。运气好(堆栈溢出)。是不是很棒?!
    【解决方案3】:

    不太可能是最快的解决方案,但您可以将两个数组zip 放在一起(首先使用排序键的数组)并对结果进行排序,如下所示:

    a = [ 8, 4, 2, 7, 5 ]
    b = [ 5, 7, 0, 3, 3 ]
    
    a.zip(b).sort.transpose.last
    # => [0, 7, 3, 3, 5]
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-23
    • 2020-12-04
    • 1970-01-01
    • 2013-10-22
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多