From 8363ae81fd0d8736d45c4dd44ddd325ee8af4d47 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Tue, 9 Mar 2021 19:03:25 -0800 Subject: [PATCH] Fix syntax issues --- dynamic_programming/coin_change.rb | 49 +++++++++++++++--------------- maths/decimal_to_binary.rb | 4 +-- maths/fibonacci.rb | 4 +-- 3 files changed, 28 insertions(+), 29 deletions(-) diff --git a/dynamic_programming/coin_change.rb b/dynamic_programming/coin_change.rb index 3f01845..0d462fc 100644 --- a/dynamic_programming/coin_change.rb +++ b/dynamic_programming/coin_change.rb @@ -1,30 +1,31 @@ def coin_change_minimum(coins, amount) - dp = Array.new(amount+1,-1) - dp[0] = 0 - - coins.each do |coin| - (coin..amount).each do |i| - if dp[i-coin] != -1 - dp[i] = -1 == dp[i] ? dp[i-coin]+1 : [dp[i],dp[i-coin]+1].min - end + dp = Array.new(amount + 1, -1) + dp[0] = 0 + + coins.each do |coin| + (coin..amount).each do |i| + if dp[i - coin] != -1 + dp[i] = -1 == dp[i] ? dp[i - coin] + 1 : [dp[i], dp[i - coin] + 1].min end end - - dp[amount] end - - def coin_change_combinations(coins, amount) - dp = Array.new(coins.length+1) { Array.new(amount+1,0) } - dp[0][0] = 1 - (1..coins.length).each do |i| - (0..amount).each do |j| - dp[i][j] = dp[i-1][j] + (j < coins[i-1] ? 0 : dp[i][j-coins[i-1]]) - end + + dp[amount] +end + +def coin_change_combinations(coins, amount) + dp = Array.new(coins.length + 1) { Array.new(amount + 1, 0) } + dp[0][0] = 1 + (1..coins.length).each do |i| + (0..amount).each do |j| + dp[i][j] = dp[i - 1][j] + (j < coins[i - 1] ? 0 : dp[i][j - coins[i - 1]]) end - dp[coins.length][amount] end - - coins = Array[2,4,5] - amount = 12 - puts "Number of combinations of getting change for " + amount.to_s + " is "+ coin_change_combinations(coins, amount).to_s + "." - puts "Minimum number of coins required for " + amount.to_s + " is "+ coin_change_minimum(coins, amount).to_s + "." + dp[coins.length][amount] +end + +coins = Array[2, 4, 5] +amount = 12 +puts 'Number of combinations of getting change for ' + amount.to_s + ' is ' + coin_change_combinations(coins, + amount).to_s + '.' +puts 'Minimum number of coins required for ' + amount.to_s + ' is ' + coin_change_minimum(coins, amount).to_s + '.' diff --git a/maths/decimal_to_binary.rb b/maths/decimal_to_binary.rb index fa0110d..c9e92e3 100644 --- a/maths/decimal_to_binary.rb +++ b/maths/decimal_to_binary.rb @@ -34,9 +34,7 @@ def decimal_to_binary(d) return binary if d == 0 return 1.to_s if d == 1 - binary = decimal_to_binary(d / 2).to_s + binary - - binary + decimal_to_binary(d / 2).to_s + binary end puts 'Binary value of 4 is ' + decimal_to_binary(4).to_s diff --git a/maths/fibonacci.rb b/maths/fibonacci.rb index 4515de2..8303ffe 100644 --- a/maths/fibonacci.rb +++ b/maths/fibonacci.rb @@ -4,7 +4,7 @@ # # F(0) = 0, F(1) = 1 # F(n) = F(n - 1) + F(n - 2), for n > 1. -# +# # Given n, calculate F(n). # @@ -20,7 +20,7 @@ # Time complexity: O(1). Constant time complexity since we are using no loops or recursion # and the time is based on the result of performing the calculation using Binet's formula. -# +# # Space complexity: O(1). The space used is the space needed to create the variable # to store the golden ratio formula.