Search
 
SCRIPT & CODE EXAMPLE
 

RUBY

Backtracking solution in ruby

def exact_sum?(sum, arr)
return true if sum==0
return false if sum < 0 || arr.empty?
# now we are checking both cases
exact_sum?(sum - arr[0], arr[1,arr.length]) || exact_sum?(sum, arr[1,arr.length])
end

puts exact_sum?(12, [1, 2, 3, 4, 5]);
# => true
Comment

PREVIOUS NEXT
Code Example
Ruby :: for loop with condition in ruby 
Ruby :: sequel ruby alias table 
Ruby :: ruby clone vs dup 
Ruby :: ruby array of symbols 
Ruby :: rails active storage get all attachment names 
Ruby :: Validate French phone numbers 
Ruby :: sidekiq configuration file 
Ruby :: symbols used in hashes 
Ruby :: multiple seeds in rails 6 
Ruby :: Replacing consecutive numbers with dash between first and last in a range 
Ruby :: diff between .. and ... in ruby 
Ruby :: rails convert euro to dollar 
Ruby :: difference between is_a and kind_of ruby 
Ruby :: grep routes rails 
Ruby :: difference between Hash.new and Hash.new { |h, k| h[k] = [] } 
Ruby :: logback grails log in different files 
Ruby :: rails 7 
R :: how to fill na values in r 
R :: mode in r 
R :: r optim 
R :: sort dataframe r 
R :: sort dataframe dplyr 
R :: remove rownumbers r 
R :: calculating RMSE, Rsquared with caret in R 
R :: calculating RMSE, MAE, MSE, Rsquared manually in R 
R :: boucle sur r 
R :: Remove specific data frames from R 
R :: how to add columns to a flextable in r 
R :: r change column value conditionally 
R :: r ggplot variable name 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =