Binary Mobile

Краен срок
11.12.2012 12:00

Срокът за предаване на решения е отминал

Binary mobile

Binary mobile (всеки да си го превежда „на вкус“) е структура, която се състои от два пръта („клони“) с общ край (няма binary mobile без клони, или само с един клон, или с повече от два клона). Клоните имат определена дължина (строго положителна) и на свободния им край е закачена или проста тежест, или друг binary mobile.

Създайте клас Branch, чийто конструктор приема два параметъра: дължината клона и структурата, която е закачена за него:

b1 = Branch.new 10, 3 # b1 е клон с дължина 10 и тежест с тегло 3
b2 = Branch.new 8, 4.2 # b2 е клон с дължина 8 и тежест с тегло 4.2

Създайте клас BinaryMobile, чийто конструктор приема 2 аргумента – ляв и десен Branch, съответно:

bm = BinaryMobile.new b1, b2

Теглото на един BinaryMobile се изчислява като се сумират теглата, закачени за клоните му. Дефинирайте метод BinaryMobile#weight, който да връща теглото на съответния binary mobile:

bm.weight # => 7.2

Един binary mobile се нарича балансиран, ако са изпълнени следните условия:

  1. Моментът, оказван от левия клон е равен на момента, оказван от десния клон (молим за извинение хората, учили физика, чиито чувства ще бъдат накърнени от грешните дефиниции :-)) Моментът, оказван от клон, е равен на произведението на дължината на клона и теглото на структурата, закачена на края му, например b1 ще оказва момент 30, а b2 ще оказва момент 33.6.
  2. Структурите, закачени на клоните също са балансирани. Приемаме, че простите тежести са балансирани.

Напишете метод BinaryMobile#balanced?, който да казва дали съответния binary mobile е балансиран:

bm.balanced? # => false

bm2 = BinaryMobile.new Branch.new(1, 2), Branch.new(2, 1)
bm2.balanced? # => true

Измислете си подходящи селектори за класа Branch. Тестовете ще използват само интерфейса на BinaryMobile (както и конструктора на Branch, естествено).

Възможно е на някои от вас задачата да ви заприлича на задача 2.29 от SICP. Има защо. :-)

Решения

Йордан Петров
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Йордан Петров
class Branch
def initialize(length, weight)
@length, @weight = length, weight
end
def weight
if @weight.is_a? BinaryMobile then @weight.weight
else @weight
end
end
def all_balanced?
if weight.is_a? BinaryMobile then @weight.all_balanced?
else true
end
end
def momentum
@length * if @weight.is_a? BinaryMobile then @weight.momentum
else @weight
end
end
end
class BinaryMobile
def initialize(left, right)
@left, @right = left, right
end
def weight
@left.weight + @right.weight
end
def all_balanced?
@left.all_balanced? and @right.all_balanced?
end
def momentum
@left.momentum + @right.momentum
end
def balanced?
if all_balanced? then @left.momentum == @right.momentum
else false
end
end
end
Ангел Николов
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Ангел Николов
class Branch
def initialize(length, weight)
@length = length
@weight = weight
end
def balanced?
@weight.class != BinaryMobile or @weight.balanced?
end
def momentum
@length * weight
end
def weight
if @weight.class == BinaryMobile
@weight.weight
else
@weight
end
end
end
class BinaryMobile
def initialize(lbranch, rbranch)
@lbranch = lbranch
@rbranch = rbranch
end
def weight
@lbranch.weight + @rbranch.weight
end
def balanced?
@lbranch.momentum == @rbranch.momentum and
@lbranch.balanced? and @rbranch.balanced?
end
end
Кирчо Кирев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Кирчо Кирев
class Branch
def initialize(length, value)
@length = length
@value = value
@value_is_binary_mobile = value.is_a? BinaryMobile
end
def weight
@value_is_binary_mobile ? @value.weight : @value
end
def balanced?
@value_is_binary_mobile ? @value.balanced? : true
end
def moment
weight * @length
end
end
class BinaryMobile
def initialize(left, right)
@left = left
@right = right
end
def weight
@left.weight + @right.weight
end
def balanced?
@left.moment == @right.moment and
@left.balanced? and
@right.balanced?
end
end
Александър Иванов
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Александър Иванов
class Branch
attr_accessor :length, :value
def initialize(length, value)
@length, @value = length, value
end
def weight
BinaryMobile === @value ? @value.weight : @value
end
def impulse
@length * weight
end
def balanced?
BinaryMobile === @value ? @value.balanced? : true
end
end
class BinaryMobile
def initialize(left, right)
@left, @right = left, right
end
def weight
@left.weight + @right.weight
end
def balanced?
@left.impulse == @right.impulse and [@left, @right].all?(&:balanced?)
end
end
Николай Димитров
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Николай Димитров
class Branch
attr_accessor :weight, :moment, :balanced
def initialize(length, structure)
@length = length
case structure
when Numeric
@weight = structure
@balanced = true
when BinaryMobile
@weight = structure.weight
@balanced = structure.balanced?
end
@moment = @length * @weight
end
end
class BinaryMobile
def initialize(left, right)
@left, @right = left, right
end
def weight
@left.weight + @right.weight
end
def balanced?
@left.moment == @right.moment and @left.balanced and @right.balanced
end
end
Станислав Гатев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Станислав Гатев
class StructureFactory
def self.get(structure)
if structure.kind_of? Numeric
NumericStructure.new(structure)
else
structure
end
end
end
class Structure
attr_reader :weight
def balanced?
true
end
end
class NumericStructure < Structure
attr_reader :weight
def initialize(number)
@weight = number
end
end
class BinaryMobile < Structure
def initialize(left_branch, right_branch)
@left_branch, @right_branch = left_branch, right_branch
end
def weight
@left_branch.weight + @right_branch.weight
end
def balanced?
@left_branch.balanced? and @right_branch.balanced? and @left_branch.moment == @right_branch.moment
end
end
class Branch
def initialize(length, structure)
@length, @structure = length, StructureFactory.get(structure)
end
def weight
@structure.weight
end
def moment
@length * @structure.weight
end
def balanced?
@structure.balanced?
end
end
Свилен Андонов
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Свилен Андонов
class Branch
attr_accessor :weight, :length, :type
def initialize length, weight
@weight = weight
@length = length
@type = :leaf if length.is_a? Numeric and weight.is_a? Numeric
@type = :node if not (length.is_a? Numeric and weight.is_a? Numeric)
end
end
class BinaryMobile
attr_accessor :left , :right
def initialize left, right
@left = left
@right = right
end
def depth value
return ((depth value.weight) + (depth value.length)) if value.type == :node
return value.weight * value.length if value.type == :leaf
end
def balanced?
left_sum = depth (@left)
right_sum = depth( @right)
left_sum == right_sum
end
def sum value
return ((sum value.weight) + (sum value.length)) if value.type == :node
return value.weight if value.type == :leaf
end
def weight
left_sum = sum (@left)
right_sum = sum( @right)
left_sum + right_sum
end
end
Камелия Пандаклиева
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Камелия Пандаклиева
class Branch
def initialize(length, weight)
@length = length
@weight = weight
end
def weight
if @weight.instance_of? BinaryMobile
@weight.weight
else
@weight
end
end
def balanced?
if @weight.instance_of? BinaryMobile
@weight.balanced?
else
true
end
end
def moment
@length * weight
end
end
class BinaryMobile
def initialize(left, right)
@left, @right = left, right
end
def weight
@left.weight + @right.weight
end
def balanced?
@left.moment == @right.moment and @left.balanced? and @right.balanced?
end
end
Николай Колев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Николай Колев
class Branch
def initialize(length, node)
@length = length
@node = node.is_a?(BinaryMobile) ? node : SimpleBranch.new(node)
end
def weight
@node.weight
end
def momentum
@length * @node.weight
end
def balanced?
@node.balanced?
end
end
class SimpleBranch
def initialize(value)
@value = value
end
def weight
@value
end
def balanced?
true
end
end
class BinaryMobile
def initialize(left_branch, right_branch)
@left_branch = left_branch
@right_branch = right_branch
end
def weight
@left_branch.weight + @right_branch.weight
end
def balanced?
@left_branch.momentum == @right_branch.momentum and
@left_branch.balanced? and @right_branch.balanced?
end
end
Виктория Христова
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Виктория Христова
class BinaryMobile
attr_accessor :left_branch, :right_branch
def initialize(left, right)
@left_branch = left
@right_branch = right
end
def weight
left_branch.weight + right_branch.weight
end
def balanced?
left_branch.balance == right_branch.balance and left_branch.balanced? and right_branch.balanced?
end
end
class Branch
attr_accessor :lenght, :struct
def initialize(lenght, struct)
@lenght = lenght
@struct = struct
end
def weight
struct.class == BinaryMobile ? struct.weight : struct
end
def balance
struct.class == BinaryMobile ? lenght * struct.weight : lenght * struct
end
def balanced?
struct.class == BinaryMobile ? struct.balanced? : true
end
end
Иван Георгиев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Иван Георгиев
class BinaryMobile
attr_accessor :left_branch, :right_branch
def initialize(left_branch, right_branch)
@left_branch = left_branch
@right_branch = right_branch
end
def weight
@left_branch.weight + @right_branch.weight
end
def partially_balanced?
@left_branch.length * @left_branch.weight == @right_branch.length * @right_branch.weight
end
def balanced?
if partially_balanced? and @left_branch.balanced? and @right_branch.balanced? then true
else false
end
end
end
class Branch
attr_accessor :length, :struct
def initialize(length, struct)
@length = length
@struct = struct
end
def weight
if @struct.class == Fixnum or @struct.class == Float then @struct
else @struct.weight
end
end
def balanced?
if @struct.class == Fixnum or @struct.class == Float then true
else @struct.balanced?
end
end
end
Изабела Недялкова
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Изабела Недялкова
class Branch
attr_accessor :lenght, :structure
def initialize(lenght, structure)
@lenght, @structure = lenght, structure
end
def branch_weight
if @structure.respond_to?(:branch_weight)
@structure.branch_weight
else
@structure
end
end
def moment
if @structure.respond_to?(:branch_weight)
@lenght * @structure.branch_weight
else
@lenght * @structure
end
end
end
class BinaryMobile
def initialize(left_branch, right_branch)
@left_branch, @right_branch = left_branch, right_branch
end
def weight
@left_branch.branch_weight + @right_branch.branch_weight
end
def balanced?
@left_branch.moment == @right_branch.moment
end
end
Елена Денева
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Елена Денева
class Branch
attr_accessor :length, :struct
def initialize length, struct
@length = length
@struct = struct
end
def balanced?
true
end
def weight
if struct.class == BinaryMobile
struct.weight
else
struct
end
end
def moment
length * weight
end
end
class BinaryMobile
attr_accessor :branch1, :branch2
def initialize branch1, branch2
@branch1 = branch1
@branch2 = branch2
end
def weight
branch1.weight + branch2.weight
end
def balanced?
branch1.balanced? && branch2.balanced? && branch1.moment == branch2.moment
end
end
Цвета Гергичанова
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Цвета Гергичанова
class Branch
attr_accessor :length, :weigh, :balance, :in_balance
def initialize(length, weigh)
@length = length
if weigh.class == BinaryMobile
@weigh = weigh.left.weigh + weigh.right.weigh
@in_balance = weigh.balanced?
else
@weigh = weigh
@in_balance = true
end
@balance = @weigh * @length
end
end
class BinaryMobile
attr_accessor :left, :right
def initialize(left, right)
@left = left
@right = right
end
def weight
left.weigh + right.weigh
end
def balanced?
left.balance == right.balance and left.in_balance and right.in_balance
end
end
Георги Георгиев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Георги Георгиев
class Branch
def initialize(length, structure)
@length = length
@structure = structure
end
def weight
@structure.is_a?(Numeric) ? @structure : @structure.weight
end
def momentum
@length * weight
end
def balanced?
@structure.is_a? Numeric or @structure.balanced?
end
end
class BinaryMobile
def initialize(left_branch, right_branch)
@left_branch = left_branch
@right_branch = right_branch
end
def weight
@left_branch.weight + @right_branch.weight
end
def balanced?
@left_branch.balanced? and
@right_branch.balanced? and
@left_branch.momentum == @right_branch.momentum
end
end
Филарета Йорданова
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Филарета Йорданова
class Branch
attr_accessor :length, :struct
def initialize(length,struct)
@length = length
@struct = struct
end
def weight
if struct.class == BinaryMobile
struct.weight
else
struct
end
end
def balanced?
true
end
end
class BinaryMobile
attr_accessor :left, :right
def initialize(branch1,branch2)
@left=branch1
@right=branch2
end
def weight
left.weight + right.weight
end
def moment?
left.length * left.weight == right.length * right.weight
end
def balanced?
left.balanced? and right.balanced? and moment?
end
end
Красимир Георгиев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Красимир Георгиев
class SimpleWeight
attr_reader :weight
def initialize(weight)
@weight = weight
end
def balanced?
true
end
end
class Branch
attr_reader :length, :object
def initialize(length, weight)
@length = length
if weight.is_a? Numeric
@object = SimpleWeight.new(weight)
else
@object = weight
end
end
def weight
object.weight
end
def torque
length * weight
end
def balanced?
object.balanced?
end
end
class BinaryMobile
attr_reader :left_branch, :right_branch
def initialize(left_branch, right_branch)
@left_branch = left_branch
@right_branch = right_branch
end
def weight
@left_branch.weight + @right_branch.weight
end
def balanced?
left_branch.torque == right_branch.torque and
left_branch.balanced? and right_branch.balanced?
end
end
Георги Гърдев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Георги Гърдев
class Branch
def initialize(length, value)
@length, @value = length, value
end
def weight
return @value unless @value.kind_of? BinaryMobile
@value.weight
end
def momentum
@length * weight
end
def balanced?
return true unless @value.kind_of? BinaryMobile
@value.balanced?
end
end
class BinaryMobile
def initialize(left, right)
@left, @right = left, right
end
def weight
@left.weight + @right.weight
end
def balanced?
@left.balanced? and @right.balanced? and @left.momentum == @right.momentum
end
end
Атанас Пройчев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Атанас Пройчев
class Branch
attr_accessor :weight
def initialize(length, weight)
@length = length
@weight = weight
end
def moment
@weight * @length
end
def balanced?
true
end
end
class BinaryMobile
def initialize(left, right)
@left = left
@right = right
end
def weight
@left.weight + @right.weight
end
def balanced?
@left.moment == @right.moment and @left.balanced? and @right.balanced?
end
end
Даяна Беркова
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Даяна Беркова
class Branch
attr_accessor :length, :branch_weight, :is_balanced
def initialize(length, structure)
@length = length
if structure.to_s =~ /\A\d+(.\d)*\z/
@branch_weight = structure
@is_balanced = true
else
@branch_weight = structure.weight
@is_balanced = structure.balanced?
end
end
end
class BinaryMobile
attr_accessor :left_branch, :right_branch, :weight
def initialize(left_branch, right_branch)
@left_branch = left_branch
@right_branch = right_branch
end
def weight
@weight = @left_branch.branch_weight + @right_branch.branch_weight
end
def balanced?
left_moment = @left_branch.length * @left_branch.branch_weight
right_moment = @right_branch.length * @right_branch.branch_weight
return true if left_moment == right_moment and @left_branch.is_balanced == true and @right_branch.is_balanced == true
false
end
end
Георги Пачов
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Георги Пачов
module Weight
def weight
raise NotImplementedError
end
end
class Branch
include Weight
def initialize(branch_length, weight)
@branch_length = branch_length
@weight = (weight.kind_of? Numeric) ? SimpleWeight.new(weight) : ComplexWeight.new(weight)
end
def weight
@weight.weight
end
def moment
@weight.weight * @branch_length
end
end
class BinaryMobile
include Weight
def initialize(branch_1, branch_2)
@branch_2 = branch_2
@branch_1 = branch_1
end
def weight
@branch_1.weight + @branch_2.weight
end
def balanced?
@branch_1.moment == @branch_2.moment
end
end
class SimpleWeight
include Weight
def initialize(weight)
@weight = weight
end
def weight
@weight
end
end
class ComplexWeight
include Weight
def initialize(binary_mobile)
@binary_mobile = binary_mobile
end
def weight
@binary_mobile.weight
end
end
Нели Хатева
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Нели Хатева
class Branch
attr_reader :length, :structure
def initialize(length, structure)
@length, @structure = length, structure
end
def weight
if @structure.kind_of? Numeric
@structure
else #@structure.kind_of? BinaryMobile
@structure.left_branch.weight + @structure.right_branch.weight
end
end
def torque
@length * weight
end
def balanced?
if @structure.kind_of? Numeric
true
else #@structure.kind_of? BinaryMobile
@structure.balanced?
end
end
end
class BinaryMobile
attr_reader :left_branch, :right_branch
def initialize(left_branch, right_branch)
@left_branch, @right_branch = left_branch, right_branch
end
def weight
@left_branch.weight + @right_branch.weight
end
def balanced?
@left_branch.torque == @right_branch.torque
end
end
Мартин Попов
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Мартин Попов
class Branch
attr_reader :item, :length
def initialize(length, item)
@length = length
@item = item
end
def moment
length * weight
end
def balanced_item?
item.class == BinaryMobile ? item.balanced? : true
end
def weight
item.class == BinaryMobile ? item.weight : item
end
end
class BinaryMobile
attr_reader :left, :right
def initialize(left, right)
@left = left
@right = right
end
def weight
left.weight + right.weight
end
def balanced?
left.moment == right.moment and left.balanced_item? and right.balanced_item?
end
end
Никола Таушанов
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Никола Таушанов
class BinaryMobile
def initialize(left, right)
@left, @right = left, right
end
def weight
@left.weight + @right.weight
end
def balanced?
@left.balanced? &&
@right.balanced? &&
@left.moment == @right.moment
end
end
class Branch
def initialize(length, structure)
@length, @structure = length, structure
end
def balanced?
begin
@structure.balanced?
rescue NoMethodError
true
end
end
def weight
begin
@structure.weight
rescue NoMethodError
@structure
end
end
def moment
@length*weight
end
end
Чанита Иванова
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Чанита Иванова
class Branch
attr_accessor :structure, :length
def initialize(length,structure)
@length = length
@structure = structure
end
def weight
if(structure.instance_of? BinaryMobile)
then result = structure.weight
else result = structure
end
result
end
end
class BinaryMobile
attr_accessor :left_branch, :right_branch
def initialize(branch_1,branch_2)
@left_branch = branch_1
@right_branch = branch_2
end
def weight
left = left_branch.weight
right = right_branch.weight
left+right
end
def balanced?
left_tag = left_branch.structure.balanced? if(left_branch.structure.instance_of? BinaryMobile)
left = left_branch.length*left_branch.weight
right_tag = right_branch.structure.balanced? if(right_branch.structure.instance_of? BinaryMobile)
right = right_branch.length*right_branch.weight
left_tag = true if(left_tag == nil)
right_tag = true if(right_tag == nil)
return false if((left != right) || !left_tag || !right_tag)
true
end
end
Емил Гоцев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Емил Гоцев
class Branch
attr_reader :structure
def initialize(length, structure)
@length = length
@structure = case structure
when BinaryMobile
structure
when SimpleStructure
structure
else
SimpleStructure.new structure
end
end
def weight
@structure.weight
end
def moment
@length * weight
end
end
class SimpleStructure
attr_reader :weight
def initialize(weight)
@weight = weight
end
def balanced?
true
end
end
class BinaryMobile
def initialize(left_branch, right_branch)
@left_branch, @right_branch = left_branch, right_branch
end
def weight
@left_branch.weight + @right_branch.weight
end
def balanced?
@left_branch.moment == @right_branch.moment and @left_branch.structure.balanced? and @right_branch.structure.balanced?
end
end
Светлана Величкова
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Светлана Величкова
class Branch
attr_accessor :length, :binaryMobile
attr_writer :weight
def initialize(length, weight)
@length = length
if weight.integer?
@weight = weight
else @binaryMobile = weight
end
end
def weight
if @weight
@weight
else @binaryMobile.weight
end
end
def balance
length * weight
end
def balanced?
true
end
end
class BinaryMobile
attr_accessor :left_branch, :rigth_branch
def initialize(left_branch, rigth_branch)
@left_branch = left_branch
@rigth_branch = rigth_branch
end
def weight
result = left_branch.weight + rigth_branch.weight
end
def integer?
false
end
def balanced?
if !left_branch.binaryMobile and
!rigth_branch.binaryMobile and
left_branch.balance == rigth_branch.balance
return true
end
if left_branch.balance != rigth_branch.balance
return false
else
return (left_branch.balanced? and rigth_branch.balanced?)
end
end
end
Милан Миланов
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Милан Миланов
class Branch
attr_reader :length
def initialize(length, structure)
@length, @structure = length, structure
end
def weight
@structure.kind_of?(BinaryMobile) ? @structure.weight : @structure
end
def balanced?
true
end
end
class BinaryMobile
def initialize(left_branch, right_branch)
@left_branch, @right_branch = left_branch, right_branch
end
def weight
@left_branch.weight + @right_branch.weight
end
def balanced?
@left_branch.length * @left_branch.weight == @right_branch.length * @right_branch.weight \
and @left_branch.balanced? and @right_branch.balanced?
end
end
Румен Палетов
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Румен Палетов
class Branch
attr_reader :dist, :node
def initialize(dist, node)
@dist = dist
@node = node
end
def weight
if node.is_a? BinaryMobile then node.weight
else node
end
end
def balanced
if node.is_a? BinaryMobile then node.balanced?
else true
end
end
end
class BinaryMobile
attr_reader :leftBranch, :rightBranch
def initialize(leftBranch, rightBranch)
@leftBranch = leftBranch
@rightBranch = rightBranch
end
def weight
leftBranch.weight + rightBranch.weight
end
def balanced?
leftBranch.weight * leftBranch.dist == rightBranch.weight * rightBranch.dist &&
leftBranch.balanced && rightBranch.balanced
end
end
Мирослав Крамолински
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Мирослав Крамолински
class BranchWeight
def initialize(weight)
@weight = weight
end
def weight
return @weight.weight if @weight.is_a? BinaryMobile
@weight
end
def balanced?
@weight.balanced? if @weight.is_a? BinaryMobile
true
end
end
class Branch
attr_accessor :length, :weight
def initialize(length, weight)
@length, @weight = length, BranchWeight.new(weight)
end
def weight
@weight.weight
end
def balanced?
@weight.balanced?
end
def balance
@weight.weight * @length
end
end
class BinaryMobile
def initialize(left, right)
@left, @right = left, right
end
def weight
@left.weight + @right.weight
end
def balanced?
@left.balanced? == @right.balanced? and @left.balanced? == true and @left.balance == @right.balance
end
end
Кристиан Азманов
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Кристиан Азманов
class Branch
def initialize(length,eValue)
@length=length
@endValue=eValue
end
def endValue
@endValue
end
def length
@length
end
end
class BinaryMobile
def initialize(lBranch,rBranch)
if(lBranch.endValue.class==Fixnum and rBranch.endValue.class==Fixnum)
then (@leftBranch=lBranch
@rightBranch=rBranch
)
end
if(lBranch.endValue.class==Fixnum and rBranch.endValue.class==BinaryMobile)
then (@leftBranch=lBranch
@rightBranch=rBranch
@rightBranch.endValue.Parent(true)
)
end
if(lBranch.endValue.class==BinaryMobile and rBranch.endValue.class==Fixnum)
then (@leftBranch=lBranch
@rightBranch=rBranch
@leftBranch.endValue.Parent(true)
)
end
if(lBranch.endValue.class==BinaryMobile and rBranch.endValue.class==BinaryMobile)
then (@leftBranch=lBranch
@rightBranch=rBranch
@leftBranch.endValue.Parent(true)
@rightBranch.endValue.Parent(true)
)
end
end
def Parent(a)
@parent = a
end
def hasParent
@parent
end
def weight
aLeft=@leftBranch.endValue.class
aRight=@rightBranch.endValue.class
if(aLeft==Fixnum and aRight==Fixnum )
then return (@leftBranch.endValue + @rightBranch.endValue) end
if(aLeft==Fixnum and aRight==BinaryMobile )
then return @leftBranch.endValue+(@rightBranch.endValue.weight) end
if(aLeft==BinaryMobile and aRight==Fixnum )
then return (@leftBranch.endValue.weight)+(@rightBranch.endValue) end
if(aLeft==BinaryMobile and aRight==BinaryMobile )
then return (@leftBranch.endValue.weight)+(@rightBranch.endValue.weight) end
end
def balanced
lbc=@leftBranch.endValue.class
rbc=@rightBranch.endValue.class
lbl=@leftBranch.length
lbev=@leftBranch.endValue
rbl=@rightBranch.length
rbev=@rightBranch.endValue
if((lbc==Fixnum and rbc==Fixnum)and(lbl*lbev==rbl*rbev)and (self.hasParent!=true))
then return lbl*lbev==rbl*rbev
end
if((lbc==Fixnum and rbc==Fixnum)and(lbl*lbev==rbl*rbev)and (self.hasParent==true))
then return self.weight
end
if((lbc==Fixnum and rbc==BinaryMobile))
then return lbl*lbev==rbl*(rbev.balanced)
end
if((lbc==BinaryMobile and rbc==Fixnum))
then return lbl*lbev.balanced==rbl*rbev
end
if((lbc==BinaryMobile and rbc==BinaryMobile))
then return (lbl*lbev.balanced)==rbl*(rbev.balanced)
end
end
end
Теодор Илиев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Теодор Илиев
class Branch
attr_reader :length
def initialize(branch_length, structure)
@length = branch_length
@structure = structure.is_a?(Numeric) ? Weight.new(structure) : structure
end
def weight()
@structure.weight
end
def balanced?()
@structure.balanced?
end
end
class Weight
attr_reader :weight
def initialize(weight)
@weight = weight
end
def balanced?()
true
end
end
class BinaryMobile
def initialize(left_branch, right_branch)
@left = left_branch
@right = right_branch
end
def weight()
@left.weight + @right.weight
end
def balanced?()
(@left.weight + @left.length) == (@right.weight + @right.length) and
@left.balanced? and @right.balanced?
end
end
Тихомир Янев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Тихомир Янев
class Branch
attr_reader :size, :other
def initialize(size, other)
@size, @other = size, other
end
end
class BinaryMobile
def initialize(b1, b2)
@b1, @b2 = b1, b2
end
def weight
branch_weight(@b1) + branch_weight(@b2)
end
def balanced?
@b1.size * branch_weight(@b1) == @b2.size * branch_weight(@b2)
end
private
def branch_weight(branch)
return branch.other if branch.other.class != branch.class
branch_weight(branch.other)
end
end
Гергана Петрова
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Гергана Петрова
class SimpleWeight
attr_accessor :weight
def initialize(weight)
@weight = weight
end
def balanced?
true
end
end
class Branch
attr_accessor :length
def initialize(length, weight)
@length = length
@weight = if weight.instance_of? BinaryMobile
weight
else
SimpleWeight.new weight
end
end
def moment
length * weight
end
def weight
@weight.weight
end
def balanced?
@weight.balanced?
end
end
class BinaryMobile
def initialize(left, right)
@left = left
@right = right
end
def weight
@left.weight + @right.weight
end
def balanced?
@left.moment == @right.moment and\
@left.balanced? and @right.balanced?
end
end
Христо Владев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Христо Владев
class Branch
def initialize(length, structure)
@length = length
@structure = structure
end
def weight
if @structure.respond_to? :weight
@structure.weight
else
@structure
end
end
def balanced?
if @structure.respond_to? :balanced?
@structure.balanced?
else
true
end
end
def torque
@length * weight
end
end
class BinaryMobile
def initialize(left_branch, rigth_branch)
@left_branch = left_branch
@rigth_branch = rigth_branch
end
def weight
@left_branch.weight + @rigth_branch.weight
end
def balanced?
@left_branch.torque == @rigth_branch.torque and
@left_branch.balanced? and
@rigth_branch.balanced?
end
end
Петър Костов
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Петър Костов
class Branch
def initialize(length, weight)
@length = length
@weight = weight
end
def weight
return @weight.weight if @weight.respond_to? :weight
@weight
end
def balanced?
return @weight.balanced? if @weight.respond_to? :balanced?
true
end
def moment
weight * @length
end
end
class BinaryMobile
def initialize(left, right)
@left = left
@right = right
end
def weight
@left.weight + @right.weight
end
def balanced?
@left.moment == @right.moment and @left.balanced? and @right.balanced?
end
end
Иван Арабаджиев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Иван Арабаджиев
class Branch
attr_accessor :momentum, :weight
def balanced?
true
end
def initialize(length, weight)
@weight = weight
@momentum = length * weight
end
end
class BinaryMobile
def initialize(left, right)
@left, @right = left, right
end
def weight
@left.weight + @right.weight
end
def balanced?
@left.balanced? and @right.balanced? and @left.momentum == @right.momentum
end
def momentum
@left.momentum + @right.momentum
end
end
Валентин Ейткен
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Валентин Ейткен
#!/usr/bin/env ruby
class Branch
def initialize(length, struct)
@length, @struct = length, struct
end
def weight
@struct.kind_of?(BinaryMobile) ? @struct.weight : @struct
end
def length
@struct.kind_of?(BinaryMobile) ? @length + @struct.length : @length
end
end
class BinaryMobile
def initialize(left_branch, right_branch)
@left_branch, @right_branch = left_branch, right_branch
end
def weight
@left_branch.weight + @right_branch.weight
end
def balanced?
@left_branch.weight * @left_branch.length ==
@right_branch.weight * @right_branch.length
end
end
Йоана Тодорова
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Йоана Тодорова
class Branch
attr_accessor :structure
def iniтialize(length, structure)
@length, @structure = length, structure
end
def weight
return @structure unless @structure.is_a? BinaryMobile
@structure.weight
end
def moment
return @length*@structure unless @structure.is_a? BinaryMobile
@length*@structure.weight
end
end
class BinaryMobile
def initialize(left_branch, right_branch)
@left_branch, @right_branch = left_branch, right_branch
end
def weight
@left_branch.weight + @right_branch.weight
end
def balanced?
if @left_branch.moment == @right_branch.moment
(@left_branch.structure.is_a? BinaryMobile) ? (left = @left_branch.structure.balanced?) : (left = true)
(@right_branch.structure.is_a? BinaryMobile) ? (right = @right_branch.structure.balanced?) : (right = true)
return (left and right)
еnd
false
end
end
Орлин Христов
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Орлин Христов
class Branch
attr_reader :weight, :length
def initialize(length, weight)
throw ArgumentError if length <= 0
@length = length
@weight = weight
end
end
class BinaryMobile
def initialize(left_branch, right_branch)
@left_branch = left_branch
@right_branch = right_branch
end
def weight
@left_branch.weight + @right_branch.weight
end
def balanced?
@left_branch.length * @left_branch.weight == @right_branch.length * @right_branch.weight
end
end
Михаил Жеков
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Михаил Жеков
class Branch
attr_accessor :weight, :length, :balanced
def initialize weight, length
if weight.instance_of? BinaryMobile
@weight = weight.weight
@balanced = weight.balanced?
else
@weight = weight
@balanced = true
end
@length = length
end
end
class BinaryMobile
attr_accessor :left, :right
def initialize left, right
@left = left
@right = right
end
def weight
@left.weight + @right.weight
end
def balanced?
(@left.weight * @left.length) == (@right.weight * @right.length) and @left.balanced and @right.balanced
end
end
Явор Лилянов
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Явор Лилянов
class Branch
attr_reader :branch
def initialize(length, structure)
@branch = make_branch(length, structure)
end
def make_branch(length, structure)
if structure.is_a? BinaryMobile
branch1 = structure.left
branch2 = structure.right
[length, [make_branch(branch1.first, branch1.last), make_branch(branch2.first, branch2.last)]]
else
[length,structure]
end
end
end
class BinaryMobile
def initialize(left, right)
@binary_mobile = [left.branch, right.branch]
end
def left
@binary_mobile.first
end
def right
@binary_mobile.last
end
def weight(binary_mobile = @binary_mobile)
weight_helper(binary_mobile.first) + weight_helper(binary_mobile.last)
end
def weight_helper(binary_mobile)
if binary_mobile.last.is_a? Array
weight(binary_mobile.last)
else
binary_mobile.last
end
end
end
Илиян Величков
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Илиян Величков
class Branch
attr_reader :length
def initialize(length, load)
@length = length
@load = load
end
def weight
simple_weight? ? @load : @load.weight
end
def moment
length * weight
end
def simple_weight?
not @load.class.instance_of? BinaryMobile
end
def balanced?
simple_weight? ? true : @load.balanced?
end
end
class BinaryMobile
def initialize(left, right)
@left = left
@right = right
end
def weight
@left.weight + @right.weight
end
def balanced?
@left.balanced? and @right.balanced? and @left.moment == @right.moment
end
end
Илиян Бобев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Илиян Бобев
class Branch
attr_accessor :length, :structure
def initialize (length, structure)
@length = length
@structure = structure
end
def weight
@structure.respond_to?(:weight) ? @structure.weight : @structure
end
def balanced?
@structure.respond_to?(:balanced?) ? @structure.balanced? : true
end
end
class BinaryMobile
attr_accessor :weight, :balanced
def initialize (b1,b2)
@b1=b1
@b2=b2
@balanced = b1.weight * b1.length == b2.weight * b2.length and b1.balanced? and b2.balanced?
@weight = @b1.weight + @b2.weight
end
def balanced?
@balanced
end
end