there has got to be a better way...

M

Michael Barinek

i'm looking to simplify the below...

@tournament.rounds = Array.new(4) { Round.new }

@tournament.rounds[0].matches = Array.new(8) { Match.new }
@tournament.rounds[1].matches = Array.new(4) { Match.new }
@tournament.rounds[2].matches = Array.new(2) { Match.new }
@tournament.rounds[3].matches = Array.new(1) { Match.new }

thoughts/suggestions?
 
B

Brad Phelan

Michael said:
i'm looking to simplify the below...

@tournament.rounds = Array.new(4) { Round.new }

@tournament.rounds[0].matches = Array.new(8) { Match.new }
@tournament.rounds[1].matches = Array.new(4) { Match.new }
@tournament.rounds[2].matches = Array.new(2) { Match.new }
@tournament.rounds[3].matches = Array.new(1) { Match.new }

thoughts/suggestions?

for i in 0..3
@tournament.rounds = Array.new( 1 << ( 3 - i ) ) { Match. new }
end

perhapps.
 
M

Matthew Harris

@tournament.rounds = Array.new(4) { Round.new }

@tournaments.rounds.inject(8) do |size, round|
round.matches = Array.new(size) { Match.new }
size / 2
end


Something like that?

i'm looking to simplify the below...

@tournament.rounds = Array.new(4) { Round.new }

@tournament.rounds[0].matches = Array.new(8) { Match.new }
@tournament.rounds[1].matches = Array.new(4) { Match.new }
@tournament.rounds[2].matches = Array.new(2) { Match.new }
@tournament.rounds[3].matches = Array.new(1) { Match.new }

thoughts/suggestions?
 
F

Farrel Lifson

i'm looking to simplify the below...

@tournament.rounds = Array.new(4) { Round.new }

@tournament.rounds.each_with_index do |round,roundNumber|
round.matches = Array.new(2**(@tournamet.rounds.length - 1 -
roundNumber)) {Match.new}
end
 
A

ara.t.howard

i'm looking to simplify the below...

@tournament.rounds = Array.new(4) { Round.new }

@tournament.rounds[0].matches = Array.new(8) { Match.new }
@tournament.rounds[1].matches = Array.new(4) { Match.new }
@tournament.rounds[2].matches = Array.new(2) { Match.new }
@tournament.rounds[3].matches = Array.new(1) { Match.new }

thoughts/suggestions?

this may work for your needs


harp:~ > cat a.rb
class Match
def initialize(idx) @idx = idx end
def inspect() "#{ self.class } <#{ @idx }>" end
end

class Round < ::Array
def self.new(*a,&b) super(*a){|*_| Match.new *_ } end
end

class Tournament < ::Array
end

def Match(*a,&b) Match.new(*a,&b); end
def Round(*a,&b) Round.new(*a,&b); end
def Tournament(*a,&b) Tournament.new(*a,&b); end

tournament = Tournament[ Round(8), Round(4), Round(2), Round(1) ]

p tournament[0]
p tournament[1]
p tournament[2]
p tournament[3]

p tournament[0][7]
p tournament[3][0]



harp:~ > ruby a.rb
[Match <0>, Match <1>, Match <2>, Match <3>, Match <4>, Match <5>, Match <6>, Match <7>]
[Match <0>, Match <1>, Match <2>, Match <3>]
[Match <0>, Match <1>]
[Match <0>]
Match <7>
Match <0>


regards.

-a
 
M

Morton Goldberg

A small variant on answers already posted -- use reverse! to avoid
index twiddling.

Regards, Morton

--------------- start of code
! /usr/bin/ruby -w

class M
attr_accessor :ii
# **** support for pretty printing
def to_s
"M.#@ii"
end
end

class R
attr_accessor :mm, :ii
# **** support for pretty printing
def to_s
s = (mm.collect {|m| m.to_s}).join(", ")
"[R#@ii: #{s}]"
end
end

class T
def initialize
# **** start of answer
@rr = Array.new(4) {R.new}
@rr.each_with_index {|r, i| r.mm = Array.new(1 << i) {M.new}}
@rr.reverse!
# **** end of answer
# **** support for pretty printing
tag = 1
@rr.each_with_index do |r, i|
r.ii = i + 1
r.mm.each do |m|
m.ii = tag
tag += 1
end
end
end
# **** support for pretty printing
def to_s
s = (@rr.collect {|r| r.to_s}).join(", ")
"[T: #{s}]"
end
end

t = T.new
puts t => [T: [R1: M.1, M.2, M.3, M.4, M.5, M.6, M.7, M.8], \
[R2: M.9, M.10, M.11, M.12], [R3: M.13, M.14], \
[R4: M.15]]

--------------- end of code
 
D

Dave Howell

i'm looking to simplify the below...

@tournament.rounds = Array.new(4) { Round.new }

@tournament.rounds[0].matches = Array.new(8) { Match.new }
@tournament.rounds[1].matches = Array.new(4) { Match.new }
@tournament.rounds[2].matches = Array.new(2) { Match.new }
@tournament.rounds[3].matches = Array.new(1) { Match.new }

thoughts/suggestions?

I'm not a big proponent of trying to do everything in one line, because
it often results in code that's hard to figure out when you go back to
look at it later. So I was a little surprised when this turned into one
line of active code. :)

class Match
def initialize
# put whatever's needed here, of course
end
end

class Tournament
attr_reader :rounds
def initialize(num_of_rounds)
@rounds = (1..num_of_rounds).collect do |round_number|
Array.new(2**(num_of_rounds-round_number)) {|i| Match.new}
end
end
end

@tournament = Tournament.new(4)

@tournament.rounds.each_with_index {|round, round_num| puts "Number of
matches in round " + (round_num + 1).to_s + " is " + round.size.to_s}


Number of matches in round 1 is 8
Number of matches in round 2 is 4
Number of matches in round 3 is 2
Number of matches in round 4 is 1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top