Решение на Четвърта задача от Бетина Иванова

Обратно към всички решения

Към профила на Бетина Иванова

Резултати

  • 3 точки от тестове
  • 0 бонус точки
  • 3 точки общо
  • 5 успешни тест(а)
  • 6 неуспешни тест(а)

Код

module UI
module Vertical
def vertical_function(array, border)
help = array.map { |item| first(item) }
size = 0
help.each { |item| size = second(item, size) }
help = help.map { |item| third(item, border, size) }
help.join
end
def first(item)
if item.include? "\n"
item.chop.split("\n")
else
[] << item
end
end
def second(array, size)
array.each do |item|
size = helper(size, item)
end
size
end
def third(array, border, size)
array = array.map do |item|
border + item + (" " * (size - item.length)) + border + "\n"
end
array
end
def helper(size, item)
size < item.length ? item.length : size
end
end
module Help
def function(array, size)
array.size > size ? array.size : size
end
def checker(size, index)
size != 1 || index != size - 1 ? "\n" : ""
end
def get_size(item, size)
item.each { |element| size = helper(size, element) }
size
end
def transform(item, size)
item.map { |element| help(element, size) }
end
def help(item, size)
item.length != size ? item + (" " * (size - item.length)) : item
end
def check(border, string)
string << border if border != nil
end
end
module Horizontal
extend Vertical
extend Help
def self.horizontal_function(array, border)
size = 0
current = array.map { |item| first(item) }
current.each { |item| size = function(item, size) }
processing(current, size, border)
end
def self.processing(array, size, border)
@current = array
string, index = "", 0
size.times do
arrange(border, index, string)
string ,index = string + checker(size, index), index + 1
end
string << "\n"
end
def self.arrange(border, index, string)
check(border, string)
primary(@current, index, string)
check(border, string)
end
def self.primary(array, index, string)
array.each do |item|
size = 0
size = get_size(item, size)
item = transform(item, size)
string << final(index, size, item)
end
end
def self.final(index, size, item)
if (item[index] == nil and (not (item[0].include? "\n")))
" " * item[0].length
elsif
item[index] == nil then " " * size
else
item[index]
end
end
end
class TextScreen
@main_array = []
@array = []
extend Vertical
include Horizontal
def self.label (text:, border: nil, style: nil)
text = text.send(style) if style != nil
if border != nil
@main_array << (border + text + border)
else
@main_array << text
end
@main_array.join
end
def self.vertical(border: nil, style: nil)
Proc.new.call
@array << (@main_array.join("\n") << "\n") unless @main_array.empty?
@main_array = []
if border != nil then vertical_function(@array, border)
else
(@array.join("\n") << "\n").squeeze("\n")
end
end
def self.horizontal(border: nil, style: nil)
Proc.new.call
@array << @main_array.join unless @main_array.empty?
@main_array = []
UI::Horizontal.horizontal_function(@array, border)
end
def self.draw
@array = []
TextScreen.instance_eval(&Proc.new)
end
end
end

Лог от изпълнението

.F.F.F.FFF.

Failures:

  1) Command Line Toolkit adding horizontal group does not change the behavior
     Failure/Error: expect do
       expected #<Proc:0xb958fb38@/tmp/d20141126-26053-sba6rz/spec.rb:30> to render as "      123\n"
     # /tmp/d20141126-26053-sba6rz/spec.rb:30:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) Command Line Toolkit handles complex group nestings
     Failure/Error: expect do
       expected #<Proc:0xb958c4c4@/tmp/d20141126-26053-sba6rz/spec.rb:56> to render as "      123\n        45\n         6\n      7\n"
     # /tmp/d20141126-26053-sba6rz/spec.rb:56:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) Command Line Toolkit handles borders correctly in complex group nestings
     Failure/Error: expect do
       expected #<Proc:0xb9570a94@/tmp/d20141126-26053-sba6rz/spec.rb:99> to render as "      |||1||2|||3|       |||\n      ||      |||4|||5||||||\n      ||      ||   ||6||||||\n      ||7|                 |\n"
     # /tmp/d20141126-26053-sba6rz/spec.rb:99:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) Command Line Toolkit propagates upcase to child components
     Failure/Error: expect do
       expected #<Proc:0xb954e958@/tmp/d20141126-26053-sba6rz/spec.rb:137> to render as "      someveryINTERESTINGget it?\n              TEXTGOES       \n                  HERE       \n"
     # /tmp/d20141126-26053-sba6rz/spec.rb:137:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  5) Command Line Toolkit applies downcase to simple components
     Failure/Error: expect do
       expected #<Proc:0xb9534e7c@/tmp/d20141126-26053-sba6rz/spec.rb:163> to render as "      SOMEvery\n"
     # /tmp/d20141126-26053-sba6rz/spec.rb:163:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  6) Command Line Toolkit propagates downcase to child components
     Failure/Error: expect do
       expected #<Proc:0xb952f080@/tmp/d20141126-26053-sba6rz/spec.rb:172> to render as "      SOMEVERYinterestingGET IT?\n              textgoes       \n                  here       \n"
     # /tmp/d20141126-26053-sba6rz/spec.rb:172:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.02499 seconds
11 examples, 6 failures

Failed examples:

rspec /tmp/d20141126-26053-sba6rz/spec.rb:29 # Command Line Toolkit adding horizontal group does not change the behavior
rspec /tmp/d20141126-26053-sba6rz/spec.rb:55 # Command Line Toolkit handles complex group nestings
rspec /tmp/d20141126-26053-sba6rz/spec.rb:98 # Command Line Toolkit handles borders correctly in complex group nestings
rspec /tmp/d20141126-26053-sba6rz/spec.rb:136 # Command Line Toolkit propagates upcase to child components
rspec /tmp/d20141126-26053-sba6rz/spec.rb:162 # Command Line Toolkit applies downcase to simple components
rspec /tmp/d20141126-26053-sba6rz/spec.rb:171 # Command Line Toolkit propagates downcase to child components

История (1 версия и 0 коментара)

Бетина обнови решението на 26.11.2014 15:39 (преди почти 10 години)

+module UI
+ module Vertical
+ def vertical_function(array, border)
+ help = array.map { |item| first(item) }
+ size = 0
+ help.each { |item| size = second(item, size) }
+ help = help.map { |item| third(item, border, size) }
+ help.join
+ end
+
+ def first(item)
+ if item.include? "\n"
+ item.chop.split("\n")
+ else
+ [] << item
+ end
+ end
+
+ def second(array, size)
+ array.each do |item|
+ size = helper(size, item)
+ end
+ size
+ end
+
+ def third(array, border, size)
+ array = array.map do |item|
+ border + item + (" " * (size - item.length)) + border + "\n"
+ end
+ array
+ end
+
+ def helper(size, item)
+ size < item.length ? item.length : size
+ end
+ end
+
+ module Help
+ def function(array, size)
+ array.size > size ? array.size : size
+ end
+
+ def checker(size, index)
+ size != 1 || index != size - 1 ? "\n" : ""
+ end
+
+ def get_size(item, size)
+ item.each { |element| size = helper(size, element) }
+ size
+ end
+
+ def transform(item, size)
+ item.map { |element| help(element, size) }
+ end
+
+ def help(item, size)
+ item.length != size ? item + (" " * (size - item.length)) : item
+ end
+
+ def check(border, string)
+ string << border if border != nil
+ end
+ end
+
+ module Horizontal
+ extend Vertical
+ extend Help
+ def self.horizontal_function(array, border)
+ size = 0
+ current = array.map { |item| first(item) }
+ current.each { |item| size = function(item, size) }
+ processing(current, size, border)
+ end
+
+ def self.processing(array, size, border)
+ @current = array
+ string, index = "", 0
+ size.times do
+ arrange(border, index, string)
+ string ,index = string + checker(size, index), index + 1
+ end
+ string << "\n"
+ end
+
+ def self.arrange(border, index, string)
+ check(border, string)
+ primary(@current, index, string)
+ check(border, string)
+ end
+
+ def self.primary(array, index, string)
+ array.each do |item|
+ size = 0
+ size = get_size(item, size)
+ item = transform(item, size)
+ string << final(index, size, item)
+ end
+ end
+
+ def self.final(index, size, item)
+ if (item[index] == nil and (not (item[0].include? "\n")))
+ " " * item[0].length
+ elsif
+ item[index] == nil then " " * size
+ else
+ item[index]
+ end
+ end
+ end
+
+ class TextScreen
+ @main_array = []
+ @array = []
+ extend Vertical
+ include Horizontal
+
+ def self.label (text:, border: nil, style: nil)
+ text = text.send(style) if style != nil
+ if border != nil
+ @main_array << (border + text + border)
+ else
+ @main_array << text
+ end
+ @main_array.join
+ end
+
+ def self.vertical(border: nil, style: nil)
+ Proc.new.call
+ @array << (@main_array.join("\n") << "\n") unless @main_array.empty?
+ @main_array = []
+ if border != nil then vertical_function(@array, border)
+ else
+ (@array.join("\n") << "\n").squeeze("\n")
+ end
+ end
+
+ def self.horizontal(border: nil, style: nil)
+ Proc.new.call
+ @array << @main_array.join unless @main_array.empty?
+ @main_array = []
+ UI::Horizontal.horizontal_function(@array, border)
+ end
+
+ def self.draw
+ @array = []
+ TextScreen.instance_eval(&Proc.new)
+ end
+ end
+end