Skip to content

Instantly share code, notes, and snippets.

@sdmx
Last active August 29, 2015 14:07
Show Gist options
  • Save sdmx/34588d94461963bccd0e to your computer and use it in GitHub Desktop.
Save sdmx/34588d94461963bccd0e to your computer and use it in GitHub Desktop.
Ruby code test
#!/usr/bin/ruby
# Step 1 - This is literally terrible.
=begin
def Add(numbers) # Define the method Add, which takes the 'numbers' variable.
adds=numbers.split(",") # Apply the split method to the 'numbers' variable, breaking the string into an array of substrings. Store them in the 'adds' variable.
sum=adds[0].to_i+adds[1].to_i # Convert the substrings of the 'sum' variable to integers. I had problems doing this with a map method, so this is brute force. Store in the 'sum' variable.
puts sum # Present the summed result in the 'sum' variable.
end
=end
# Step 2 - I have used the ruby-lang channel to become less terrible.
=begin
def Add(numbers) # Define the method Add, which takes the 'numbers' variable.
adds=numbers.split(",") # Apply the split method to the 'numbers' variable, breaking the string into an array of substrings. Store them in the 'adds' variable.
sum=adds.compact.map{ |s| s.to_i} # If there are nil elements inside the array, the compact method drops them. The map method then changes all strings to usable integers.
sum=sum.inject(o,:+) # All the elements of the sum array are then totaled using the inject method. In the sole case that no input was provided, the inject method provides a 0.
puts sum # Present the summed result in the 'sum' variable.
end
=end
# Step 3 -
=begin
def Add(numbers) # Define the method Add, which takes the 'numbers' variable.
adds=numbers.split(%r{,|\n}) # Apply the split method to the 'numbers' variable, breaking the string into an array of substrings. Store them in the 'adds' variable.
sum=adds.compact.map{ |s| s.to_i} # If there are nil elements inside the array, the compact method drops them. The map method then changes all strings to usable integers.
sum=sum.inject(0,:+) # All the elements of the sum array are then totaled using the inject method. In the sole case that no input was provided, the inject method provides a 0.
puts sum # Present the summed result in the 'sum' variable.
end
=end
# Step 4 - Who needs efficiency?
=begin
def add(numbers) # Define the method Add, which takes the 'numbers' variable.
delim=numbers.scan(/^..(.)$/m) # Detect the delimiter for the sum by using a regular expression forthe third character of the first line.
adds=numbers.split(/\n/) # Split the argument string into two parts: The delimiter and the data to be analyzed. Store in the Adds variable.
array=adds[1].split(/#{delim}/) # Apply the split method to the 'adds' variable using the delim variable to do so. Store them in the 'array' variable.
sum=array.compact.map{ |s| s.to_i} # If there are nil elements inside the array, the compact method drops them. The map method then changes all strings to usable integers.
sum=sum.inject(0,:+) # All the elements of the sum array are then totaled using the inject method. In the sole case that no input was provided, the inject method provides a 0.
puts sum # Present the summed result in the 'sum' variable.
end
=end
# Step 5 - Home Stretch
#=begin
def add(numbers) # Define the method Add, which takes the 'numbers' variable.
delim=numbers.scan(/^..(.)$/m)
adds=numbers.split(/\n/)
array=adds[1].split(/#{delim}/) # Apply the split method to the 'numbers' variable, breaking the string into an array of substrings. Store them in the 'adds' variable.
positive=array.compact.map{ |s| s.to_i } # If there are nil elements inside the array, the compact method drops them. The map method then changes all strings to usable integers.
if positive.all? { |i| i > 0 }
sum=positive.inject(0,:+) # All the elements of the sum array are then totaled using the inject method. In the sole case that no input was provided, the nil string is converted to the 0 integer.
puts sum # Present the summed result in the 'sum' variable.
else
result=positive.select { |i| i < 0 }
print "Negative integers not allowed: "
puts result.join(', ')
end
end
#=end
add("//;\n1;2;3;4;5")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment