From 297385fa213b8bfb3cb5890d3c3b3c2555ec38d8 Mon Sep 17 00:00:00 2001 From: Alexey Moiseenko Date: Tue, 20 Feb 2018 23:55:45 +0300 Subject: [PATCH 01/10] Add homework-1 --- 2018/alexeymoissenko/hm-1/Pascal-tree.rb | 45 ++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 2018/alexeymoissenko/hm-1/Pascal-tree.rb diff --git a/2018/alexeymoissenko/hm-1/Pascal-tree.rb b/2018/alexeymoissenko/hm-1/Pascal-tree.rb new file mode 100644 index 000000000..84689431c --- /dev/null +++ b/2018/alexeymoissenko/hm-1/Pascal-tree.rb @@ -0,0 +1,45 @@ +class Triangle + + def initialize (base_number,deep) + @base_number = base_number + @deep = deep + end + + def fact (a) + (1..a).reduce(:*) + end + + def binomial(n, k) + return @base_number if n-k <= 0 || k <= 0 + fact(n) / ( fact(k) * fact( n - k ) ) * @base_number + end + + def curr_str (n) + (0..n).map { |e| binomial(n, e) } + end + + + def show + max_row = curr_str(@deep) + max_el_size = max_row.max.to_s.size + width = [`tput cols`.to_i,100].max + @deep.times do |i| + string = curr_str(i).map do |el| + el.to_s.center(max_el_size) + end.join(" ").center(width) + puts "#{i + 1}: #{string}" + end + end +end + +puts "Введите начальное значение:" + +base_number = gets.chomp.to_i + +puts "Введите глубину треугольника:" + +deep = gets.chomp.to_i + +triangle = Triangle.new(base_number, deep) + +triangle.show \ No newline at end of file From 1f9d9c55f14ea4a34ff18a89afbed80f914b4342 Mon Sep 17 00:00:00 2001 From: alexeymoiseenko <36601463+alexeymoiseenko@users.noreply.github.com> Date: Wed, 21 Feb 2018 01:15:34 +0300 Subject: [PATCH 02/10] Update Pascal-tree.rb MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Изменения согласно комментариев бота --- 2018/alexeymoissenko/hm-1/Pascal-tree.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/2018/alexeymoissenko/hm-1/Pascal-tree.rb b/2018/alexeymoissenko/hm-1/Pascal-tree.rb index 84689431c..956226688 100644 --- a/2018/alexeymoissenko/hm-1/Pascal-tree.rb +++ b/2018/alexeymoissenko/hm-1/Pascal-tree.rb @@ -1,17 +1,17 @@ class Triangle def initialize (base_number,deep) - @base_number = base_number - @deep = deep + @base_number=base_number + @deep=deep end - def fact (a) + def fact(a) (1..a).reduce(:*) end - def binomial(n, k) + def binomial(n,k) return @base_number if n-k <= 0 || k <= 0 - fact(n) / ( fact(k) * fact( n - k ) ) * @base_number + fact(n) / (fact(k) * fact(n - k)) * @base_number end def curr_str (n) @@ -28,18 +28,18 @@ def show el.to_s.center(max_el_size) end.join(" ").center(width) puts "#{i + 1}: #{string}" + end end - end end -puts "Введите начальное значение:" +puts 'Введите начальное значение:' base_number = gets.chomp.to_i -puts "Введите глубину треугольника:" +puts 'Введите глубину треугольника:' deep = gets.chomp.to_i triangle = Triangle.new(base_number, deep) -triangle.show \ No newline at end of file +triangle.show From c9e2ce1247564d7b16f94e8c82fde7788ce3df2d Mon Sep 17 00:00:00 2001 From: alexeymoiseenko <36601463+alexeymoiseenko@users.noreply.github.com> Date: Wed, 21 Feb 2018 01:35:52 +0300 Subject: [PATCH 03/10] Second update Pascl-tree.rb --- 2018/alexeymoissenko/hm-1/Pascal-tree.rb | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/2018/alexeymoissenko/hm-1/Pascal-tree.rb b/2018/alexeymoissenko/hm-1/Pascal-tree.rb index 956226688..c63477ed9 100644 --- a/2018/alexeymoissenko/hm-1/Pascal-tree.rb +++ b/2018/alexeymoissenko/hm-1/Pascal-tree.rb @@ -1,15 +1,15 @@ -class Triangle - - def initialize (base_number,deep) - @base_number=base_number - @deep=deep +class Triangle + + def initialize (base_number, deep) + @base_number = base_number + @deep = deep end - + def fact(a) (1..a).reduce(:*) end - def binomial(n,k) + def binomial(n, k) return @base_number if n-k <= 0 || k <= 0 fact(n) / (fact(k) * fact(n - k)) * @base_number end @@ -17,17 +17,16 @@ def binomial(n,k) def curr_str (n) (0..n).map { |e| binomial(n, e) } end - def show max_row = curr_str(@deep) max_el_size = max_row.max.to_s.size - width = [`tput cols`.to_i,100].max + width = [`tput cols`.to_i, 100].max @deep.times do |i| - string = curr_str(i).map do |el| + string = curr_str(i).map do |el| el.to_s.center(max_el_size) end.join(" ").center(width) - puts "#{i + 1}: #{string}" + puts "#{i+1}: #{string}" end end end @@ -43,3 +42,4 @@ def show triangle = Triangle.new(base_number, deep) triangle.show + From f605f480291ed823a60b77757d2ee223b1cd8e02 Mon Sep 17 00:00:00 2001 From: alexeymoiseenko <36601463+alexeymoiseenko@users.noreply.github.com> Date: Wed, 21 Feb 2018 11:11:54 +0300 Subject: [PATCH 04/10] Update Pascal-tree.rb --- 2018/alexeymoissenko/hm-1/Pascal-tree.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/2018/alexeymoissenko/hm-1/Pascal-tree.rb b/2018/alexeymoissenko/hm-1/Pascal-tree.rb index c63477ed9..fbca454e3 100644 --- a/2018/alexeymoissenko/hm-1/Pascal-tree.rb +++ b/2018/alexeymoissenko/hm-1/Pascal-tree.rb @@ -1,5 +1,4 @@ class Triangle - def initialize (base_number, deep) @base_number = base_number @deep = deep @@ -10,7 +9,7 @@ def fact(a) end def binomial(n, k) - return @base_number if n-k <= 0 || k <= 0 + return @base_number if n - k <= 0 || k <= 0 fact(n) / (fact(k) * fact(n - k)) * @base_number end @@ -25,8 +24,8 @@ def show @deep.times do |i| string = curr_str(i).map do |el| el.to_s.center(max_el_size) - end.join(" ").center(width) - puts "#{i+1}: #{string}" + end.join(' ').center(width) + puts "#{i + 1}: #{string}" end end end @@ -42,4 +41,3 @@ def show triangle = Triangle.new(base_number, deep) triangle.show - From f92b0ee012bfa06e96d1a9499111fb6f7c875909 Mon Sep 17 00:00:00 2001 From: alexeymoiseenko <36601463+alexeymoiseenko@users.noreply.github.com> Date: Wed, 21 Feb 2018 11:18:50 +0300 Subject: [PATCH 05/10] Update Pascal-tree.rb --- 2018/alexeymoissenko/hm-1/Pascal-tree.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/2018/alexeymoissenko/hm-1/Pascal-tree.rb b/2018/alexeymoissenko/hm-1/Pascal-tree.rb index fbca454e3..0ec79f1de 100644 --- a/2018/alexeymoissenko/hm-1/Pascal-tree.rb +++ b/2018/alexeymoissenko/hm-1/Pascal-tree.rb @@ -1,5 +1,5 @@ class Triangle - def initialize (base_number, deep) + def initialize(base_number, deep) @base_number = base_number @deep = deep end @@ -13,7 +13,7 @@ def binomial(n, k) fact(n) / (fact(k) * fact(n - k)) * @base_number end - def curr_str (n) + def curr_str(n) (0..n).map { |e| binomial(n, e) } end @@ -41,3 +41,4 @@ def show triangle = Triangle.new(base_number, deep) triangle.show + From b33691b3ac726419708bb08ff46f3e1a3958dc66 Mon Sep 17 00:00:00 2001 From: alexeymoiseenko <36601463+alexeymoiseenko@users.noreply.github.com> Date: Wed, 21 Feb 2018 11:36:28 +0300 Subject: [PATCH 06/10] Update Pascal-tree.rb --- 2018/alexeymoissenko/hm-1/Pascal-tree.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/2018/alexeymoissenko/hm-1/Pascal-tree.rb b/2018/alexeymoissenko/hm-1/Pascal-tree.rb index 0ec79f1de..974b40eba 100644 --- a/2018/alexeymoissenko/hm-1/Pascal-tree.rb +++ b/2018/alexeymoissenko/hm-1/Pascal-tree.rb @@ -41,4 +41,3 @@ def show triangle = Triangle.new(base_number, deep) triangle.show - From d1da4153c12cfdf3695013acf12831104f8941d0 Mon Sep 17 00:00:00 2001 From: alexeymoiseenko <36601463+alexeymoiseenko@users.noreply.github.com> Date: Wed, 21 Feb 2018 17:08:50 +0300 Subject: [PATCH 07/10] Final newline fix (?) --- 2018/alexeymoissenko/hm-1/Pascal-tree.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/2018/alexeymoissenko/hm-1/Pascal-tree.rb b/2018/alexeymoissenko/hm-1/Pascal-tree.rb index 974b40eba..0ec79f1de 100644 --- a/2018/alexeymoissenko/hm-1/Pascal-tree.rb +++ b/2018/alexeymoissenko/hm-1/Pascal-tree.rb @@ -41,3 +41,4 @@ def show triangle = Triangle.new(base_number, deep) triangle.show + From d80ad3c8f6e70f133d0a7d4200354f1bd03d5926 Mon Sep 17 00:00:00 2001 From: Alexey Moiseenko Date: Thu, 22 Feb 2018 01:18:38 +0300 Subject: [PATCH 08/10] Fix newline --- 2018/alexeymoissenko/hm-1/Pascal-tree.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/2018/alexeymoissenko/hm-1/Pascal-tree.rb b/2018/alexeymoissenko/hm-1/Pascal-tree.rb index 0ec79f1de..155ab027a 100644 --- a/2018/alexeymoissenko/hm-1/Pascal-tree.rb +++ b/2018/alexeymoissenko/hm-1/Pascal-tree.rb @@ -1,5 +1,5 @@ class Triangle - def initialize(base_number, deep) + def initialize(base_number, deep) @base_number = base_number @deep = deep end @@ -41,4 +41,3 @@ def show triangle = Triangle.new(base_number, deep) triangle.show - From 81a8eb76ab64dfb21b371a7b849e89485c50aafb Mon Sep 17 00:00:00 2001 From: alexeymoiseenko Date: Thu, 15 Mar 2018 03:40:43 +0300 Subject: [PATCH 09/10] Fix variables --- 2018/alexeymoissenko/hm-1/Pascal-tree.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/2018/alexeymoissenko/hm-1/Pascal-tree.rb b/2018/alexeymoissenko/hm-1/Pascal-tree.rb index 155ab027a..d6570b8b1 100644 --- a/2018/alexeymoissenko/hm-1/Pascal-tree.rb +++ b/2018/alexeymoissenko/hm-1/Pascal-tree.rb @@ -4,17 +4,17 @@ def initialize(base_number, deep) @deep = deep end - def fact(a) - (1..a).reduce(:*) + def fact(any_value) + (1..any_value).reduce(:*) end - def binomial(n, k) - return @base_number if n - k <= 0 || k <= 0 - fact(n) / (fact(k) * fact(n - k)) * @base_number + def binomial(line_size, number_in_line) + return @base_number if line_size - number_in_line <= 0 || number_in_line <= 0 + fact(line_size) / (fact(number_in_line) * fact(line_size - number_in_line)) * @base_number end - def curr_str(n) - (0..n).map { |e| binomial(n, e) } + def curr_str(curr_size) + (0..curr_size).map { |e| binomial(curr_size, e) } end def show From be29b4dc9ef1ba825090ee74c16c97fc39caccc4 Mon Sep 17 00:00:00 2001 From: alexeymoiseenko <36601463+alexeymoiseenko@users.noreply.github.com> Date: Thu, 15 Mar 2018 04:04:20 +0300 Subject: [PATCH 10/10] Fix string size MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit сократил имена переменных, т.к. не влазили в допустимое значение символов в строке ln_s (изначально line_size) num_in_ln (изначально number_in_line) --- 2018/alexeymoissenko/hm-1/Pascal-tree.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/2018/alexeymoissenko/hm-1/Pascal-tree.rb b/2018/alexeymoissenko/hm-1/Pascal-tree.rb index d6570b8b1..3b1deaf47 100644 --- a/2018/alexeymoissenko/hm-1/Pascal-tree.rb +++ b/2018/alexeymoissenko/hm-1/Pascal-tree.rb @@ -8,9 +8,9 @@ def fact(any_value) (1..any_value).reduce(:*) end - def binomial(line_size, number_in_line) - return @base_number if line_size - number_in_line <= 0 || number_in_line <= 0 - fact(line_size) / (fact(number_in_line) * fact(line_size - number_in_line)) * @base_number + def binomial(ln_s, num_in_ln) + return @base_number if ln_s - num_in_ln <= 0 || num_in_ln <= 0 + fact(ln_s) / (fact(num_in_ln) * fact(ln_s - num_in_ln)) * @base_number end def curr_str(curr_size)