From 26de7da365d2c055f6b34738d36c9bdb39dbced4 Mon Sep 17 00:00:00 2001 From: YuliyaBond Date: Mon, 19 Feb 2018 17:15:01 +0300 Subject: [PATCH] homework 1 new homework 1 corrected homework 1 corrected-new homework 1 corrected-new1 homework 1 corrected-new2 homework 1 corrected-new3 homework 1 corrected-new4 homework 1 corrected-new5 homework 1 corrected-new6 homework 1 corrected-new7 homework 1 right i homework 1 right i homework 1 right i homework 1 corrected commit1 commit2 corrected corrected1 corrected corrected --- 2018/YuliyaBondareva/1/pascal_tree.rb | 44 +++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 2018/YuliyaBondareva/1/pascal_tree.rb diff --git a/2018/YuliyaBondareva/1/pascal_tree.rb b/2018/YuliyaBondareva/1/pascal_tree.rb new file mode 100644 index 000000000..bae0edc29 --- /dev/null +++ b/2018/YuliyaBondareva/1/pascal_tree.rb @@ -0,0 +1,44 @@ +require 'terminfo' + +def tree(number, base_number) + width = TermInfo.screen_size + puts 'Pascal triangle'.center(width[1].to_i) + number.times do |index| + trn = [base_number] + temp = base_number + s = 1 + branch = [' /'] + index.times do + temp = ((temp * (index - s + 1)) / s) + trn.push temp + s += 1 + branch.push ' /' + end + arr_to_str = trn.join(' ') + str_end = branch.join(' \\') + puts "#{index + 1}:" + ' ' + arr_to_str.center(width[1].to_i) + if index != (number - 1) + puts ' ' + "#{str_end} \\".center(width[1].to_i) + end + end +end + +puts 'Depth?' +number = gets.chomp +base_number = ENV['BASE'] +if number.to_i <= 0 + loop do + puts 'depth must be positive. ' + puts 'Depth?' + number = gets.chomp + break if number.to_i > 0 + end +elsif base_number.to_i <= 0 + loop do + puts 'BASE must be positive. ' + puts 'BASE?' + base_number = gets.chomp + break if base_number.to_i > 0 + end +end +tree(number.to_i, base_number.to_i)