From 9c762c71cec3e71bd6c2685cff91cf6b61dc77e1 Mon Sep 17 00:00:00 2001 From: Richard Nienaber Date: Sun, 21 Feb 2021 11:37:06 +0000 Subject: [PATCH 1/6] missed trapping ctrl+c, neatened up if --- 32 Diamond/ruby/diamond.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/32 Diamond/ruby/diamond.rb b/32 Diamond/ruby/diamond.rb index 1c8493a3..251cee25 100644 --- a/32 Diamond/ruby/diamond.rb +++ b/32 Diamond/ruby/diamond.rb @@ -11,9 +11,7 @@ end def get_facets while true number = gets.chomp - if /^\d+$/.match(number) - return number.to_i - end + return number.to_i if /^\d+$/.match(number) puts "!NUMBER EXPECTED - RETRY INPUT LINE" print "? " end @@ -42,4 +40,6 @@ def main draw_diamonds(lines) end +trap "SIGINT" do puts; exit 130 end + main \ No newline at end of file From 10ef66c0fdf5d3bad969ccd74a0bc83f676785eb Mon Sep 17 00:00:00 2001 From: Richard Nienaber Date: Sun, 21 Feb 2021 11:37:56 +0000 Subject: [PATCH 2/6] neaten up random number generation --- 91 Train/ruby/train.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/91 Train/ruby/train.rb b/91 Train/ruby/train.rb index 772af741..c6850035 100644 --- a/91 Train/ruby/train.rb +++ b/91 Train/ruby/train.rb @@ -27,9 +27,9 @@ def main intro loop do - car_speed = (Random.rand * 25 + 40).to_i - car_time = (Random.rand * 15 + 5).to_i - train_speed = (Random.rand * 19 + 20).to_i + car_speed = rand(25) + 40 + car_time = rand(15) + 5 + train_speed = rand(19) + 20 print " A CAR TRAVELING #{car_speed} MPH CAN MAKE A CERTAIN TRIP IN #{car_time} HOURS LESS THAN A TRAIN TRAVELING AT #{train_speed} MPH. From 5a5f365919018d3a08c6594ac2c8bc2963cf6821 Mon Sep 17 00:00:00 2001 From: Richard Nienaber Date: Sun, 21 Feb 2021 11:49:25 +0000 Subject: [PATCH 3/6] add 33 dice in ruby --- 33 Dice/ruby/dice.rb | 51 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 33 Dice/ruby/dice.rb diff --git a/33 Dice/ruby/dice.rb b/33 Dice/ruby/dice.rb new file mode 100644 index 00000000..58380928 --- /dev/null +++ b/33 Dice/ruby/dice.rb @@ -0,0 +1,51 @@ +def intro + puts " DICE + CREATIVE COMPUTING MORRISTOWN, NEW JERSEY + + + +THIS PROGRAM SIMULATES THE ROLLING OF A +PAIR OF DICE. +YOU ENTER THE NUMBER OF TIMES YOU WANT THE COMPUTER TO +'ROLL' THE DICE. WATCH OUT, VERY LARGE NUMBERS TAKE +A LONG TIME. IN PARTICULAR, NUMBERS OVER 5000. + +" +end + +def get_rolls + while true + number = gets.chomp + return number.to_i if /^\d+$/.match(number) + puts "!NUMBER EXPECTED - RETRY INPUT LINE" + print "? " + end +end + +def dice_roll = rand(6) + 1 # ruby 3, woot! + +def print_rolls(rolls) + values = Array.new(11, 0) + (1..rolls).each { values[dice_roll + dice_roll - 2] += 1 } + puts "\nTOTAL SPOTS NUMBER OF TIMES" + (0..10).each { |k| puts " %-2d %-2d" % [k + 2, values[k]] } +end + +def main + intro + loop do + print "HOW MANY ROLLS? " + rolls = get_rolls + + print_rolls(rolls) + + print "\n\nTRY AGAIN? " + option = (gets || '').chomp.upcase + break unless option == 'YES' + puts + end +end + +trap "SIGINT" do puts; exit 130 end + +main \ No newline at end of file From 300970953b538128c29b81f2ece0c6f21758ac64 Mon Sep 17 00:00:00 2001 From: Richard Nienaber Date: Sun, 21 Feb 2021 12:31:39 +0000 Subject: [PATCH 4/6] fix formatting to make output identical --- 87 3-D Plot/python/3dplot.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/87 3-D Plot/python/3dplot.py b/87 3-D Plot/python/3dplot.py index 99e8c438..2dda44ff 100644 --- a/87 3-D Plot/python/3dplot.py +++ b/87 3-D Plot/python/3dplot.py @@ -9,8 +9,8 @@ import math def equation(input): return 30 * math.exp(-input * input / 100) -print(" " * 32 + "3D PLOT\n") -print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n") +print(" " * 32 + "3D PLOT") +print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n\n") for x in range (-300, 315, 15): x1 = x / 10 @@ -23,4 +23,4 @@ for x in range (-300, 315, 15): if z > l: l = z yPlot[z] = "*" - print("".join(yPlot) + "\n") \ No newline at end of file + print("".join(yPlot)) \ No newline at end of file From 2dcb194c354612caae057f0a8f734796aeca3445 Mon Sep 17 00:00:00 2001 From: Richard Nienaber Date: Sun, 21 Feb 2021 12:32:34 +0000 Subject: [PATCH 5/6] add 87 3d plot in ruby --- 87 3-D Plot/ruby/3dplot.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 87 3-D Plot/ruby/3dplot.rb diff --git a/87 3-D Plot/ruby/3dplot.rb b/87 3-D Plot/ruby/3dplot.rb new file mode 100644 index 00000000..9c4e8394 --- /dev/null +++ b/87 3-D Plot/ruby/3dplot.rb @@ -0,0 +1,28 @@ +def intro + puts " 3D PLOT + CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n\n\n" +end + +def fna(z) = 30 * Math.exp(-z * z / 100) + +def render + (-30..30).step(1.5).each do |x| + l = 0 + y1 = 5 * (Math.sqrt(900 - x * x) / 5).to_i + y_plot = " " * 80 + (y1..-y1).step(-5).each do |y| + z = (25 + fna(Math.sqrt(x * x + y * y)) - 0.7 * y).to_i + next if z <= l + l = z + y_plot[z] = '*' + end + puts y_plot + end +end + +def main + intro + render +end + +main \ No newline at end of file From 946624ec52deabe07f574d328aa7c1e37a4660ef Mon Sep 17 00:00:00 2001 From: Richard Nienaber Date: Sun, 21 Feb 2021 13:06:28 +0000 Subject: [PATCH 6/6] add 78 sinewave in ruby --- 78 Sine Wave/ruby/sinewave.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 78 Sine Wave/ruby/sinewave.rb diff --git a/78 Sine Wave/ruby/sinewave.rb b/78 Sine Wave/ruby/sinewave.rb new file mode 100644 index 00000000..001e2cd8 --- /dev/null +++ b/78 Sine Wave/ruby/sinewave.rb @@ -0,0 +1,15 @@ +def intro + puts " SINE WAVE + CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n\n\n\n" +end + +def main + intro + (0..40).step(0.25).each do |t| + a = (26 + 25 * Math.sin(t)).to_i + text = (t % 0.5) == 0 ? "CREATIVE" : "COMPUTING" + puts " " * a + text + end +end + +main \ No newline at end of file