From 0469d7fb9cd71644f588226a894ea7a57a9b8713 Mon Sep 17 00:00:00 2001 From: user Date: Sun, 18 Feb 2018 19:13:45 +0300 Subject: [PATCH 01/12] implement hw 1 --- 2018/IvanKarpechanka/1/hw.rb | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 2018/IvanKarpechanka/1/hw.rb diff --git a/2018/IvanKarpechanka/1/hw.rb b/2018/IvanKarpechanka/1/hw.rb new file mode 100644 index 000000000..5287056fa --- /dev/null +++ b/2018/IvanKarpechanka/1/hw.rb @@ -0,0 +1,30 @@ +def fact(n) + (1..n).inject(1, :*) +end + +def binom(n,k) + fact(n) / ( fact(k) * fact(n-k) ) +end + +def pascals_row(row, first_el) + (0..row).map { |line| first_el * binom(row, line) } +end + +puts "Введите глубину дерева:" +three_len = gets.chomp.to_i +puts "Введите базовый номер:" +first_el = gets.chomp.to_i + +number_max_len = pascals_row(three_len, first_el).max.to_s.length #для вывода числа с нулями в начале, если его длинна меньше длинны максимального элемента +console_len = 160 # для центрирования дерева, значение для монитора 15" и разрешения 1366 х 768 +gaps = (" " * (number_max_len)) #для формирования отображения в консоле +help_str = "\/#{gaps}\\#{gaps}" #задание шаблона отображения ветвей дерева +(0..three_len).each do |i| + str = "" #для создания строки вывода на консоль каждой строчки дерева + pascals_row(i, first_el).each do + |elem| str += elem.to_s.rjust(number_max_len, '0') + (gaps + " ") + end + puts "#{" " * three_len.to_s.length}" + (help_str * i).chomp(gaps).center(console_len) #ветви + puts "#{i.to_s.rjust(three_len.to_s.length, '0')}" + str.chomp(gaps + " ").center(console_len, ",") #элементы +end + \ No newline at end of file From 4f1c57bf5006361b44c8bf2bff6b0039fd00a595 Mon Sep 17 00:00:00 2001 From: user Date: Sun, 18 Feb 2018 20:09:58 +0300 Subject: [PATCH 02/12] implement hw 1 fixed --- 2018/IvanKarpechanka/1/hw.rb | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/2018/IvanKarpechanka/1/hw.rb b/2018/IvanKarpechanka/1/hw.rb index 5287056fa..bc6db6d7d 100644 --- a/2018/IvanKarpechanka/1/hw.rb +++ b/2018/IvanKarpechanka/1/hw.rb @@ -2,29 +2,29 @@ def fact(n) (1..n).inject(1, :*) end -def binom(n,k) - fact(n) / ( fact(k) * fact(n-k) ) +def binom(n, k) + fact(n) / (fact(k) * fact(n - k)) end def pascals_row(row, first_el) (0..row).map { |line| first_el * binom(row, line) } end -puts "Введите глубину дерева:" +puts 'Введите глубину дерева:' three_len = gets.chomp.to_i -puts "Введите базовый номер:" +puts 'Введите базовый номер:' first_el = gets.chomp.to_i -number_max_len = pascals_row(three_len, first_el).max.to_s.length #для вывода числа с нулями в начале, если его длинна меньше длинны максимального элемента -console_len = 160 # для центрирования дерева, значение для монитора 15" и разрешения 1366 х 768 -gaps = (" " * (number_max_len)) #для формирования отображения в консоле -help_str = "\/#{gaps}\\#{gaps}" #задание шаблона отображения ветвей дерева -(0..three_len).each do |i| - str = "" #для создания строки вывода на консоль каждой строчки дерева - pascals_row(i, first_el).each do - |elem| str += elem.to_s.rjust(number_max_len, '0') + (gaps + " ") +# to output a number with zeros in the beginning if its length is less than the length of the maximum element +number_max_len = pascals_row(three_len, first_el).max.to_s.length +console_len = 160 +gaps = (' ' * (number_max_len)) +help_str = "\/#{gaps}\\#{gaps}" # Specifying a Tree Display Template +(0..three_len).each do |i| + str = '' # to create an output line on the console of each line of the tree + pascals_row(i, first_el).each do |elem| + str += elem.to_s.rjust(number_max_len, '0') + (gaps + ' ') end - puts "#{" " * three_len.to_s.length}" + (help_str * i).chomp(gaps).center(console_len) #ветви - puts "#{i.to_s.rjust(three_len.to_s.length, '0')}" + str.chomp(gaps + " ").center(console_len, ",") #элементы -end - \ No newline at end of file + puts "#{' ' * three_len.to_s.length}" + (help_str * i).chomp(gaps).center(console_len) + puts "#{i.to_s.rjust(three_len.to_s.length, '0')}" + str.chomp(gaps + ' ').center(console_len, ',') +end \ No newline at end of file From 5864c7603a9c4f8c83ad95fd6435c20ae2402522 Mon Sep 17 00:00:00 2001 From: user Date: Sun, 18 Feb 2018 20:16:04 +0300 Subject: [PATCH 03/12] implement hw 1 fixed1 --- 2018/IvanKarpechanka/1/hw.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/2018/IvanKarpechanka/1/hw.rb b/2018/IvanKarpechanka/1/hw.rb index bc6db6d7d..500d4e920 100644 --- a/2018/IvanKarpechanka/1/hw.rb +++ b/2018/IvanKarpechanka/1/hw.rb @@ -15,16 +15,20 @@ def pascals_row(row, first_el) puts 'Введите базовый номер:' first_el = gets.chomp.to_i -# to output a number with zeros in the beginning if its length is less than the length of the maximum element +# to output a number with zeros in the beginning if its +# length is less than the length of the maximum element number_max_len = pascals_row(three_len, first_el).max.to_s.length console_len = 160 -gaps = (' ' * (number_max_len)) +gaps = (' ' * number_max_len) help_str = "\/#{gaps}\\#{gaps}" # Specifying a Tree Display Template (0..three_len).each do |i| str = '' # to create an output line on the console of each line of the tree pascals_row(i, first_el).each do |elem| - str += elem.to_s.rjust(number_max_len, '0') + (gaps + ' ') + str += elem.to_s.rjust(number_max_len, '0') + (gaps + ' ') end - puts "#{' ' * three_len.to_s.length}" + (help_str * i).chomp(gaps).center(console_len) - puts "#{i.to_s.rjust(three_len.to_s.length, '0')}" + str.chomp(gaps + ' ').center(console_len, ',') -end \ No newline at end of file + help1 = "#{' ' * three_len.to_s.length}" + puts help1 + (help_str * i).chomp(gaps).center(console_len) + + help2 = "#{i.to_s.rjust(three_len.to_s.length, '0')}" + puts help2 + str.chomp(gaps + ' ').center(console_len, ',') +end From 7be5a3e2f831ca5d6cc037f3ed3a0151e8e0fc49 Mon Sep 17 00:00:00 2001 From: user Date: Sun, 18 Feb 2018 23:02:50 +0300 Subject: [PATCH 04/12] implement hw 1 fixed2 --- 2018/IvanKarpechanka/1/hw.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/2018/IvanKarpechanka/1/hw.rb b/2018/IvanKarpechanka/1/hw.rb index 500d4e920..f2f4d80a0 100644 --- a/2018/IvanKarpechanka/1/hw.rb +++ b/2018/IvanKarpechanka/1/hw.rb @@ -14,8 +14,7 @@ def pascals_row(row, first_el) three_len = gets.chomp.to_i puts 'Введите базовый номер:' first_el = gets.chomp.to_i - -# to output a number with zeros in the beginning if its +# to output a number with zeros in the beginning if its # length is less than the length of the maximum element number_max_len = pascals_row(three_len, first_el).max.to_s.length console_len = 160 @@ -26,9 +25,9 @@ def pascals_row(row, first_el) pascals_row(i, first_el).each do |elem| str += elem.to_s.rjust(number_max_len, '0') + (gaps + ' ') end - help1 = "#{' ' * three_len.to_s.length}" + help1 = ' ' * three_len.to_s.length puts help1 + (help_str * i).chomp(gaps).center(console_len) - help2 = "#{i.to_s.rjust(three_len.to_s.length, '0')}" + help2 = i.to_s.rjust(three_len.to_s.length, '0') puts help2 + str.chomp(gaps + ' ').center(console_len, ',') end From 0ebe9db425ac7f42bd98f5379f99b187786daa4d Mon Sep 17 00:00:00 2001 From: user Date: Sun, 18 Feb 2018 23:05:05 +0300 Subject: [PATCH 05/12] implement hw 1 fixed2 --- 2018/IvanKarpechanka/1/hw.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/2018/IvanKarpechanka/1/hw.rb b/2018/IvanKarpechanka/1/hw.rb index f2f4d80a0..3ad63e57f 100644 --- a/2018/IvanKarpechanka/1/hw.rb +++ b/2018/IvanKarpechanka/1/hw.rb @@ -21,13 +21,12 @@ def pascals_row(row, first_el) gaps = (' ' * number_max_len) help_str = "\/#{gaps}\\#{gaps}" # Specifying a Tree Display Template (0..three_len).each do |i| - str = '' # to create an output line on the console of each line of the tree + str = '' # to create an output line on the console of each line of the tree pascals_row(i, first_el).each do |elem| str += elem.to_s.rjust(number_max_len, '0') + (gaps + ' ') end help1 = ' ' * three_len.to_s.length - puts help1 + (help_str * i).chomp(gaps).center(console_len) - + puts help1 + (help_str * i).chomp(gaps).center(console_len) help2 = i.to_s.rjust(three_len.to_s.length, '0') - puts help2 + str.chomp(gaps + ' ').center(console_len, ',') + puts help2 + str.chomp(gaps + ' ').center(console_len, ',') end From b609ff608100b63edc2b5adc149214fe8cdf8a67 Mon Sep 17 00:00:00 2001 From: user Date: Sun, 18 Feb 2018 23:07:57 +0300 Subject: [PATCH 06/12] implement hw 1 fixed2 --- 2018/IvanKarpechanka/1/hw.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/2018/IvanKarpechanka/1/hw.rb b/2018/IvanKarpechanka/1/hw.rb index 3ad63e57f..5080f6ca9 100644 --- a/2018/IvanKarpechanka/1/hw.rb +++ b/2018/IvanKarpechanka/1/hw.rb @@ -21,12 +21,12 @@ def pascals_row(row, first_el) gaps = (' ' * number_max_len) help_str = "\/#{gaps}\\#{gaps}" # Specifying a Tree Display Template (0..three_len).each do |i| - str = '' # to create an output line on the console of each line of the tree - pascals_row(i, first_el).each do |elem| - str += elem.to_s.rjust(number_max_len, '0') + (gaps + ' ') - end - help1 = ' ' * three_len.to_s.length - puts help1 + (help_str * i).chomp(gaps).center(console_len) - help2 = i.to_s.rjust(three_len.to_s.length, '0') - puts help2 + str.chomp(gaps + ' ').center(console_len, ',') +str = '' # to create an output line on the console of each line of the tree +pascals_row(i, first_el).each do |elem| + str += elem.to_s.rjust(number_max_len, '0') + (gaps + ' ') +end +help1 = ' ' * three_len.to_s.length +puts help1 + (help_str * i).chomp(gaps).center(console_len) +help2 = i.to_s.rjust(three_len.to_s.length, '0') +puts help2 + str.chomp(gaps + ' ').center(console_len, ',') end From 095b9702f5bf67744022c83da26f3a52ce6cb760 Mon Sep 17 00:00:00 2001 From: user Date: Sun, 18 Feb 2018 23:10:41 +0300 Subject: [PATCH 07/12] implement hw 1 fixed2 --- 2018/IvanKarpechanka/1/hw.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2018/IvanKarpechanka/1/hw.rb b/2018/IvanKarpechanka/1/hw.rb index 5080f6ca9..cbbcbe8ca 100644 --- a/2018/IvanKarpechanka/1/hw.rb +++ b/2018/IvanKarpechanka/1/hw.rb @@ -21,7 +21,7 @@ def pascals_row(row, first_el) gaps = (' ' * number_max_len) help_str = "\/#{gaps}\\#{gaps}" # Specifying a Tree Display Template (0..three_len).each do |i| -str = '' # to create an output line on the console of each line of the tree + str = '' # to create an output line on the console of each line of the tree pascals_row(i, first_el).each do |elem| str += elem.to_s.rjust(number_max_len, '0') + (gaps + ' ') end From 8ca173770686c959500e32c173d946822097f0d8 Mon Sep 17 00:00:00 2001 From: user Date: Sun, 18 Feb 2018 23:13:28 +0300 Subject: [PATCH 08/12] implement hw 1 fixed2 --- 2018/IvanKarpechanka/1/hw.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2018/IvanKarpechanka/1/hw.rb b/2018/IvanKarpechanka/1/hw.rb index cbbcbe8ca..92e05f698 100644 --- a/2018/IvanKarpechanka/1/hw.rb +++ b/2018/IvanKarpechanka/1/hw.rb @@ -21,7 +21,7 @@ def pascals_row(row, first_el) gaps = (' ' * number_max_len) help_str = "\/#{gaps}\\#{gaps}" # Specifying a Tree Display Template (0..three_len).each do |i| - str = '' # to create an output line on the console of each line of the tree + str = '' # to create an output line on the console of each line of the tree pascals_row(i, first_el).each do |elem| str += elem.to_s.rjust(number_max_len, '0') + (gaps + ' ') end From d06a288f21af1157446bd4ff8769e421ecbbad88 Mon Sep 17 00:00:00 2001 From: user Date: Sun, 18 Feb 2018 23:19:33 +0300 Subject: [PATCH 09/12] implement hw 1 fixed4 --- 2018/IvanKarpechanka/1/hw.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2018/IvanKarpechanka/1/hw.rb b/2018/IvanKarpechanka/1/hw.rb index 92e05f698..3ad49be27 100644 --- a/2018/IvanKarpechanka/1/hw.rb +++ b/2018/IvanKarpechanka/1/hw.rb @@ -15,7 +15,7 @@ def pascals_row(row, first_el) puts 'Введите базовый номер:' first_el = gets.chomp.to_i # to output a number with zeros in the beginning if its -# length is less than the length of the maximum element +# length is less than the length of the maximum element. number_max_len = pascals_row(three_len, first_el).max.to_s.length console_len = 160 gaps = (' ' * number_max_len) From 000478f464a3202fc2337fbaf16bd96f2a046952 Mon Sep 17 00:00:00 2001 From: user Date: Sun, 18 Feb 2018 23:34:05 +0300 Subject: [PATCH 10/12] implement hw 1 fixed4 --- 2018/IvanKarpechanka/1/hw.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/2018/IvanKarpechanka/1/hw.rb b/2018/IvanKarpechanka/1/hw.rb index 3ad49be27..7a45bcc36 100644 --- a/2018/IvanKarpechanka/1/hw.rb +++ b/2018/IvanKarpechanka/1/hw.rb @@ -14,14 +14,12 @@ def pascals_row(row, first_el) three_len = gets.chomp.to_i puts 'Введите базовый номер:' first_el = gets.chomp.to_i -# to output a number with zeros in the beginning if its -# length is less than the length of the maximum element. number_max_len = pascals_row(three_len, first_el).max.to_s.length console_len = 160 gaps = (' ' * number_max_len) -help_str = "\/#{gaps}\\#{gaps}" # Specifying a Tree Display Template +help_str = "\/#{gaps}\\#{gaps}" (0..three_len).each do |i| - str = '' # to create an output line on the console of each line of the tree + str = '' pascals_row(i, first_el).each do |elem| str += elem.to_s.rjust(number_max_len, '0') + (gaps + ' ') end From 77fe7de2878c48deb9bf1186fadb1d353c31919a Mon Sep 17 00:00:00 2001 From: user Date: Sun, 18 Feb 2018 23:36:55 +0300 Subject: [PATCH 11/12] implement hw 1 fixed4 --- 2018/IvanKarpechanka/1/hw.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/2018/IvanKarpechanka/1/hw.rb b/2018/IvanKarpechanka/1/hw.rb index 7a45bcc36..6c0d02098 100644 --- a/2018/IvanKarpechanka/1/hw.rb +++ b/2018/IvanKarpechanka/1/hw.rb @@ -20,11 +20,11 @@ def pascals_row(row, first_el) help_str = "\/#{gaps}\\#{gaps}" (0..three_len).each do |i| str = '' -pascals_row(i, first_el).each do |elem| - str += elem.to_s.rjust(number_max_len, '0') + (gaps + ' ') -end -help1 = ' ' * three_len.to_s.length -puts help1 + (help_str * i).chomp(gaps).center(console_len) -help2 = i.to_s.rjust(three_len.to_s.length, '0') -puts help2 + str.chomp(gaps + ' ').center(console_len, ',') + pascals_row(i, first_el).each do |elem| + str += elem.to_s.rjust(number_max_len, '0') + (gaps + ' ') + end + help1 = ' ' * three_len.to_s.length + puts help1 + (help_str * i).chomp(gaps).center(console_len) + help2 = i.to_s.rjust(three_len.to_s.length, '0') + puts help2 + str.chomp(gaps + ' ').center(console_len, ',') end From 452a5b44e262009eacda282051e5ed6f5bff0c0c Mon Sep 17 00:00:00 2001 From: user Date: Mon, 19 Feb 2018 04:18:02 +0300 Subject: [PATCH 12/12] implement hw 1 fixed4 --- 2018/IvanKarpechanka/1/hw.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2018/IvanKarpechanka/1/hw.rb b/2018/IvanKarpechanka/1/hw.rb index 6c0d02098..84dccff53 100644 --- a/2018/IvanKarpechanka/1/hw.rb +++ b/2018/IvanKarpechanka/1/hw.rb @@ -15,7 +15,7 @@ def pascals_row(row, first_el) puts 'Введите базовый номер:' first_el = gets.chomp.to_i number_max_len = pascals_row(three_len, first_el).max.to_s.length -console_len = 160 +console_len = `tput cols`.to_i gaps = (' ' * number_max_len) help_str = "\/#{gaps}\\#{gaps}" (0..three_len).each do |i|