3

I am trying to split the sub-arrays if there are more than 8. I have tried calling the rps_tournament_winner function on players if it has a flattened length longer than 16 but I get a "stack too deep error".

Do I have to work on the players variable or tournament? I'm looking for a nudge in the right direction; not a complete solution.

def rps_tournament_winner(tournament)
  return rps_game_winner(tournament) if tournament.flatten.length == 4
  players = tournament.flatten(2)  

  while players.length > 1
    players = players.each_slice(2).map { |x| rps_game_winner(x) }
  end

  players[0]
end
7
  • 1
    Looks like you're recursively calling the function without modifying the argument before doing so. This would cause an infinite loop. You need to reduce the argument tournament in some way first. Commented Mar 1, 2012 at 20:21
  • So if I check for length and if it is over a certain length could I split it into 2 smaller arrays and call the tournament function on each? Commented Mar 1, 2012 at 20:27
  • Got it sorted, thanks. Was able to tweak an If statement and got it running perfectly. Thanks for the help! Commented Mar 3, 2012 at 7:38
  • You should be able to delete your question yourself, if that's what you want to do. Commented Mar 3, 2012 at 7:49
  • 1
    You can add your own answer and accept that. It may help someone in the future and you don't lose reputation. Commented Mar 3, 2012 at 8:58

4 Answers 4

6

This is the shortest I was able to come up with (with recursion)

def rps_tournament_winner(games)
   if games.flatten.length > 4
     rps_game_winner([rps_tournament_winner(games[0]), rps_tournament_winner(games[1])])
   else
     rps_game_winner(games)
   end
end
Sign up to request clarification or add additional context in comments.

2 Comments

I think recursion was the way to go, nice solution =D
Yeah, when I realized flatten could be used, it became kind of obvious and it hit me :). Thanks :)
5

This is also an elegant solution w/o flattening the array:

def rps_tournament_winner(tournament)
    if tournament[0][0].is_a?(Array)
        tournament =[rps_tournament_winner(tournament[0]), rps_tournament_winner(tournament[1])]
    end
    rps_game_winner(tournament)
end

Comments

1

I solved it using recursion

class WrongNumberOfPlayersError < StandardError ; end
class NoSuchStrategyError < StandardError ; end

def rps_game_winner(game)
  raise WrongNumberOfPlayersError unless game.length == 2
  if game[0][0].is_a?(Array) then
    winner1 = rps_game_winner(game[0])
    winner2 = rps_game_winner(game[1])
    game = [winner1, winner2]
  end
  raise NoSuchStrategyError unless /^(P|R|S){2}$/ =~ game[0][1] + game[1][1]
  case game[0][1]
    when "R"
      if game[1][1] == "P" then
        game[1]
      else
        game[0]
      end
    when "P"
      if game[1][1] == "S" then
        game[1]
      else 
        game[0]
      end
    when "S"
      if game[1][1] == "R" then
        game[1]
      else
        game[0]
      end
  end
end

def rps_tournament_winner(tournament)
  rps_game_winner(tournament)
end

1 Comment

Cool =) I love how there are always multiple ways to solve programming problems (I am fairly new to programming at all)
0

Only answering this to close the question, hopefully my answer will help some one else =D

After staring at it for about three hours, my mind actually stopped napping and i came up with this:

def rps_tournament_winner(tournament)
  return rps_game_winner(tournament) if tournament.flatten.length == 4
  if tournament.flatten.length == 16
    players = tournament.flatten(2)
  else
    players = tournament.flatten(4)  
  end

  while players.length > 1
    players = players.each_slice(2).map { |x| rps_game_winner(x) }
  end

players[0]
end

The if statement allows me to check that if a tournament of 8 or more players (the testing was only for 8, 16 and 32 players so I doubt this would work for larger sets) My problem before was that I would only flatten(2) which for the larger tournament would not work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.